You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Office Scripts实现Excel指定年月过滤并批量填充列的问题求助

Office Scripts 脚本修改需求

我正在使用Microsoft Excel的Office Scripts功能,希望按指定年份(如2023年)和月份(如3月、4月)过滤表格行,并仅为符合条件的行在「Quarter 2」列中填充值「Y」。但当前使用的脚本仅能过滤单个月份,且无法完成值的填充操作。

现有脚本如下:

function main(workbook: ExcelScript.Workbook) {
  // Define the table name and column name you're interested in
  let tableName = "Table1";
  let columnName = "Date";

  // Get the table by name
  let table = workbook.getTable(tableName);

  // Get the column by name
  let column = table.getColumnByName(columnName);
  column.getFilter().applyDynamicFilter(ExcelScript.DynamicFilterCriteria.thisYear);

  // column.getFilter().applyValuesFilter(["SALESMAN SAMPLE"]);
  column.getFilter().applyDynamicFilter(ExcelScript.DynamicFilterCriteria.allDatesInPeriodMarch);

  
  // Get the target column by name
  let targetColumnName = "Quarter 2";
  let targetColumn = table.getColumnByName(targetColumnName);

  // Get the data you want to add (adjust this based on your data source)
  let newData = ["Y"];

  // Get the range of the target column
  let targetColumnRange = targetColumn.getRange();

  // Get the values from the target range
  let targetValues = targetColumnRange.getValues();

  // Get the values from the filter column
  let filterValues = column.getRange().getValues();

  // Loop through the values and update where necessary
  for (let i = 0; i < targetValues.length; i++) {
    // Check if the row meets the filter criteria
    if (filterValues[i][0] === true) {
      targetValues[i][0] = newData[0];
    }
  }

  // Set the modified values back to the target range
  targetColumnRange.setValues(targetValues);

  // Clear the filter
  // column.getFilter().clear();

}

恳请指导修改脚本以实现预期的过滤及值填充功能。


内容的提问来源于stack exchange,提问作者Kavinda_404

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 09:15:56