Hi,
You can modify your code as per the code below-
function main(workbook: ExcelScript.Workbook) {
let firstSheet = workbook.getWorksheet("Details");
let sheetToPaste = workbook.getWorksheet("Sheet3");
let table1 = workbook.getTable("Details");
// Clear auto filter on selectedSheet
firstSheet.getAutoFilter().clearCriteria();
// Apply values filter on selectedSheet
firstSheet.getAutoFilter().apply(firstSheet.getAutoFilter().getRange(), 9, { filterOn: ExcelScript.FilterOn.values, values: ["PM"] });
const filteredRange = firstSheet.getAutoFilter().getRange().getSpecialCells(ExcelScript.SpecialCellType.visible);
// Copy the filtered data into the other sheet.
sheetToPaste.getRange("A1").copyFrom(filteredRange, ExcelScript.RangeCopyType.values, true, false);
}
The getSpecialCells
method with SpecialCellType.visible
retrieves only the visible cells within the filtered range. By using this filtered range, you can copy only the visible data into the other sheet.
Make sure to replace "Details" and "Sheet3" with the actual names of your worksheets in the getWorksheet
calls.
Give this updated code a try.
Best Regards.