How to copy a filtered table in office script and paste it in a different sheet?

Vignesh Babu Sundararajan 46 Reputation points
2021-08-16T06:11:11.93+00:00

How to copy a filtered table in office script and paste it in a different sheet?

Microsoft 365 and Office | Development | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Petra Ronald 36 Reputation points
    2021-09-02T18:45:10.41+00:00

    The following code only takes what is visible in the filtered table and copies it to another sheet. Hope this helps!

    function main(workbook: ExcelScript.Workbook) {
     let firstSheet = workbook.getWorksheet("Sheet1");
     let table = firstSheet.getTables()[0]
     let visibleTableRange = table.getRange().getVisibleView();
     let visibleTableRangeValues = visibleTableRange.getValues();
     let sheetToPaste = workbook.getWorksheet("Paste")
     let pastedValues = sheetToPaste.getRangeByIndexes(0, 0, 
          visibleTableRange.getRowCount(), visibleTableRange.getColumnCount());
     pastedValues.setValues(visibleTableRangeValues);
     let newTable = sheetToPaste.addTable(pastedValues.getAddress(), true)
    }
    
    7 people found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.