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

提取条件格式值脚本报错:仅识别绿色且存在列不匹配等错误

问题:提取条件格式值时的功能限制与报错
  • 采用@BryanMonterrosa提供的代码提取条件格式值,仅能识别绿色标记的条件格式值,无法提取其他颜色对应值
  • 执行代码时出现报错:

Exception: The number of columns in the data does not match the number of columns in the range. The data has 0 but the range has 1

  • 已确认目标工作表(示例为Sheet1)的所有条件格式均位于A列,调整代码参数后问题仍未解决
  • 部分场景下还会触发另一个报错:

TypeError: Cannot read properties of null (reading "getCriteriaType")

所用代码

const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Sheet1");
const resultSheet = ss.getSheetByName("Result");

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Solution')
    .addItem('ExtractValues', 'extractValues')
    .addToUi();
}

function extractValues() {
  const conditionalFormatRules = sheet.getConditionalFormatRules();
  if (conditionalFormatRules.length > 0) {
    const colors = {};
    conditionalFormatRules.forEach(rule => {
      var color = rule.getBooleanCondition().getBackground();
      if (!(color in colors)) {
        colors[color] = [];
      }
      colors[color].push(rule.getBooleanCondition().getCriteriaValues());
    })
    for (var i = 0; i < Object.keys(colors).length; i++) {
      color = Object.keys(colors)[i];
      resultSheet.getRange(1, i + 1).setValue(color).setBackground(color);
      resultSheet.getRange(2, i + 1, colors[color].length).setValues(colors[color]).setBackground(color);
    }
  }
}

示例数据截图

示例数据截图

另外,若方便请告知可上传示例表格的平台。

内容的提问来源于stack exchange,提问作者James 'Mook' Pite

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 00:42:12