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?

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,720 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Petra Ronald 31 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)
    }
    
    6 people found this answer helpful.