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

Google Sheets提取单元格批注及关联数据并按宠物类型统计需求

高效提取并统计Google Sheets批注关联数据

方案一:用Google Apps Script批量处理

这个脚本能一次性遍历D列所有带批注的单元格,自动关联对应行的B列宠物类型,最后把统计结果直接写入G、H列,无需手动逐个操作。

  1. 打开目标Google表格,点击顶部菜单栏「扩展程序」→「Apps脚本」
  2. 清空编辑器默认代码,粘贴以下脚本:
function extractAndStatsComments() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const allComments = sheet.getDataRange().getComments();
  const stats = {};

  // 遍历所有行,筛选D列带批注的行
  for (let rowIndex = 0; rowIndex < allComments.length; rowIndex++) {
    const dColComment = allComments[rowIndex][3]; // D列对应索引为3(从0开始计数)
    if (dColComment) {
      const petType = sheet.getRange(rowIndex + 1, 2).getValue(); // 取对应行B列内容
      // 统计销售状态,自动去重同一宠物的重复状态
      if (!stats[petType]) stats[petType] = [];
      if (!stats[petType].includes(dColComment)) {
        stats[petType].push(dColComment);
      }
    }
  }

  // 整理结果为可直接写入表格的二维数组
  const result = [];
  for (const pet in stats) {
    stats[pet].forEach(status => result.push([pet, status]));
  }

  // 将结果写入G、H列(起始位置为G1,需修改的话调整最后一行的7和1即可)
  if (result.length > 0) {
    sheet.getRange(1, 7, result.length, 2).setValues(result);
  }
}
  1. 点击编辑器的「运行」按钮,首次运行需按提示完成权限授权
  2. 返回表格后,G、H列会自动生成按宠物类型分组的销售状态列表

方案二:自定义函数+数组公式(无需脚本授权)

如果不想进行脚本授权,可通过自定义函数结合数组公式实现批量提取与统计:

  1. 打开Apps脚本编辑器,粘贴以下自定义函数代码:
function GET_COMMENT(cellAddr) {
  return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(cellAddr).getComment();
}
  1. 保存后回到表格,在F2单元格输入以下公式,批量提取D列对应行的批注:
=ARRAYFORMULA(IF(ISBLANK(B2:B), "", IF(GET_COMMENT("D"&ROW(B2:B))<>"", GET_COMMENT("D"&ROW(B2:B)), "")))
  1. 在G1单元格输入统计公式,自动生成分组结果:
=QUERY(B2:F, "SELECT B, F WHERE F <> '' GROUP BY B, F", 1)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 09:25:17