Microsoft 365 and Office | Development | Other
Building custom solutions that extend, automate, and integrate Microsoft 365 apps.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to copy a filtered table in office script and paste it in a different sheet?
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)
}