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

请求修改Google Sheets脚本:返回灰色填充单元格地址

Google Sheets脚本:找出指定区域内灰色填充单元格的地址

可以直接修改你的现有脚本,实现找出指定区域(C1:Z6)中背景色为#dbd9d9的单元格地址并返回列表的功能,修改后的代码如下:

function getGrayCellAddresses() {
  const targetColor = "#dbd9d9";
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getRange("C1:Z6");
  const backgrounds = range.getBackgrounds();
  const startRow = range.getRow();
  const startCol = range.getColumn();
  const grayCellAddresses = [];

  // 遍历区域内的每一行
  for (let rowIndex = 0; rowIndex < backgrounds.length; rowIndex++) {
    // 遍历当前行的每一列
    for (let colIndex = 0; colIndex < backgrounds[rowIndex].length; colIndex++) {
      // 判断当前单元格背景色是否匹配目标灰色
      if (backgrounds[rowIndex][colIndex] === targetColor) {
        // 转换为A1格式的单元格地址(如C1、E4)
        const cellAddress = sheet.getRange(startRow + rowIndex, startCol + colIndex).getA1Notation();
        grayCellAddresses.push(cellAddress);
      }
    }
  }

  // 返回结果:有匹配则返回地址列表,无匹配则返回提示
  return grayCellAddresses.length > 0 ? grayCellAddresses : ["未找到灰色填充的单元格"];
}

关键说明:

  • targetColor:直接定义要找的灰色Hex值,后续如果要换颜色,修改这里即可
  • startRow和startCol:获取指定区域(C1:Z6)的起始行号和列号,用来把数组索引转换成实际的单元格位置
  • 双层循环:逐个检查区域内每个单元格的背景色,匹配成功就把地址加入列表
  • getA1Notation():把单元格的行号列号转换成大家熟悉的A1格式地址(比如C1、Z6)

使用方法:在Google Sheets的单元格中输入=getGrayCellAddresses(),就能得到所有灰色填充单元格的地址列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 05:08:23