Office 脚本从表中获取数据、过滤数据并粘贴到新选项卡和新表中

问题描述 投票:0回答:1

我一直在尝试从 Excel 中的“PMs”表中提取过滤后的数据,我需要过滤 D 列以查找等于“WSCH”或“WAPPR”或“INPRG”的状态,并过滤 H 列,其值等于或少于7,应用过滤器后,创建一个新选项卡并将过滤后的数据粘贴到名为PM On Risk的新表中,我已经尝试了几天但无法获取。

非常感谢您的帮助。

测试01

我试试这个:

function main(workbook: ExcelScript.Workbook) {
  // Obtener la tabla "PMs"
  let table: ExcelScript.Table = workbook.getTable("PMs");

  // Filtrar los datos por la columna "Status" igual a "WSCH", "WAPPR" o "INPRG" y la columna "Days till the target end date" menor o igual a 7
  let filteredRange: ExcelScript.Range = table.getRange().getFilteredRange([{
    column: table.getColumnByName("Status").getIndex(),
    operator: "In",
    values: ["WSCH", "WAPPR", "INPRG"]
  }, {
    column: table.getColumnByName("Days till the target end date").getIndex(),
    operator: "LessThanOrEqual",
    values: ["7"]
  }]);

  // Crear una nueva hoja de cálculo llamada "PMRisk"
  let newSheet: ExcelScript.Worksheet = workbook.addWorksheet("PMRisk");

  // Crear una nueva tabla llamada "PM_Notification" en la nueva hoja de cálculo
  let newTable: ExcelScript.Table = newSheet.addTable("PM_Notification", filteredRange, true);

}
javascript javascript-objects office-js office-scripts ms-office-script
1个回答
0
投票
function main(workbook: ExcelScript.Workbook) {
    const dataSheet = workbook.getActiveWorksheet();
    let table: ExcelScript.Table = workbook.getTable("PMs");
    const dataRange = table.getRange();
    // Clear auto filter on table pMs
    const tabFilter = table.getAutoFilter();
    tabFilter.clearCriteria();
    // filter table on Col D and H
    let colIndexD = table.getColumnByName("Status").getIndex(); // Col D
    tabFilter.apply(table.getRange(),
        colIndexD, {
        filterOn: ExcelScript.FilterOn.values, 
        values: ["WSCH", "WAPPR", "INPRG"]
    });
    const colIndexH = table.getColumnByName("Days till the target end date").getIndex(); // Col H
    tabFilter.apply(table.getRange(),
        colIndexH, {
        filterOn: ExcelScript.FilterOn.custom, 
        criterion1: "<=7",
        operator: ExcelScript.FilterOperator.and
    });
    let newSheet: ExcelScript.Worksheet = workbook.getWorksheet("PMRisk");
    if(newSheet){ // clear dest. sheet
        newSheet.getRange().clear(ExcelScript.ClearApplyTo.all);
    }
    else{ // add dest. sheet
        newSheet = workbook.addWorksheet("PMRisk");
    }
    // copy filter table to new sheet
    newSheet.getRange("A1").copyFrom(table.getRange().getSpecialCells(ExcelScript.SpecialCellType.visible) );
    // newSheet.activate();
    // add new table
    const newRange = newSheet.getUsedRange();
    const newTable = newSheet.addTable(newRange.getAddress(), true);
    newTable.setName("PM_Notification");
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.