Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter a table - copy the filtered results #758

Open
JohnR471 opened this issue Aug 21, 2024 · 2 comments
Open

Filter a table - copy the filtered results #758

JohnR471 opened this issue Aug 21, 2024 · 2 comments
Assignees
Labels
Status: under investigation Issue is being investigated Type: doc bug Problem with the documentation (e.g., doc is out of date, unclear, confusing, or broken)

Comments

@JohnR471
Copy link

Type of issue

Code doesn't work

Feedback

On Page https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/table-samples, under the heading "Filter a table" there is a "Tip" box which offers the following tip "Copy the filtered information across the workbook by using Range.copyFrom. Add the following line to the end of the script to create a new worksheet with the filtered data." (my emphasis) with the following Typescript code workbook.addWorksheet().getRange("A1").copyFrom(table.getRange()); . This code, however, does not copy only the filtered set of table data, rather it copies the entire (un-filtered) table data. It would be good if the code were corrected to achieve the stated objective.

Page URL

https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/table-samples

Content source URL

https://github.com/OfficeDev/office-scripts-docs/blob/main/docs/resources/samples/table-samples.md

Author

@o365devx

Document Id

b772ec99-a2e5-eb1c-8b87-2179f24664f4

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP label Aug 21, 2024
@alison-mk alison-mk self-assigned this Aug 26, 2024
@alison-mk alison-mk added Needs: attention 👋 Waiting on Microsoft to provide feedback Type: doc bug Problem with the documentation (e.g., doc is out of date, unclear, confusing, or broken) and removed Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP labels Aug 26, 2024
@alison-mk
Copy link
Contributor

Hi @JohnR471, thank you for bringing this issue to our attention! I'm working on a solution. I'll report back here once I have a solution.

Thanks,
Alison

@alison-mk alison-mk added Status: under investigation Issue is being investigated and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Sep 10, 2024
@alison-mk
Copy link
Contributor

Hi @JohnR471,

To copy the filtered table data to a new worksheet, you can use the following code:

    const tableData = table.getRangeBetweenHeaderAndTotal();

    let x: ExcelScript.Range[] = [];
    const stationData = stationColumn.getRangeBetweenHeaderAndTotal();
    const stationRowCount = stationData.getRowCount();

    for (let i = 0; i < stationRowCount; i++) {
      if (!stationData.getRow(i).getRowHidden()) {
        x.push(tableData.getRow(i));
      }
    }

    x.unshift(table.getHeaderRowRange());

    const newSheet = workbook.addWorksheet();
    let rowNumber = 0;

    x.forEach((row) => {
      newSheet.getRangeByIndexes(rowNumber, 0, 1, row.getColumnCount()).copyFrom(row);
      rowNumber++;
    });

I'm still working on an update for the article, so I'll leave this issue open in the interim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: under investigation Issue is being investigated Type: doc bug Problem with the documentation (e.g., doc is out of date, unclear, confusing, or broken)
Projects
None yet
Development

No branches or pull requests

2 participants