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

需求:统计Google Sheets中人员额外任务的跨工作表总次数

解决Google Sheets跨工作表人员任务次数汇总问题

实现步骤

  1. 准备工作表
    确保你的表格中已有各日期对应的工作表,并新建(或确认存在)名为「total」的汇总工作表。

  2. 添加统计脚本
    打开你的Google Sheet,点击「扩展程序」→「Apps Script」,替换默认代码为以下脚本:

function calculateTaskCounts() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  let totalSheet = ss.getSheetByName('total');
  
  // 若total表不存在则自动创建
  if (!totalSheet) {
    totalSheet = ss.insertSheet('total');
  }
  
  // 清空total表原有数据(保留表头)
  if (totalSheet.getLastRow() > 1) {
    totalSheet.getRange(2, 1, totalSheet.getLastRow() - 1, 2).clearContent();
  }
  
  const nameCounts = {};
  const allSheets = ss.getSheets();
  
  // 遍历所有工作表,跳过total表
  allSheets.forEach(sheet => {
    if (sheet.getName() === 'total') return;
    
    // 获取当前工作表数据,假设姓名在A列(可根据实际调整列索引)
    const sheetData = sheet.getDataRange().getValues();
    
    // 跳过表头(无表头则将i的起始值改为0)
    for (let i = 1; i < sheetData.length; i++) {
      const name = sheetData[i][0];
      // 仅统计非空姓名
      if (name) {
        nameCounts[name] = (nameCounts[name] || 0) + 1;
      }
    }
  });
  
  // 设置表头并写入统计结果
  totalSheet.getRange(1, 1).setValue('人员姓名');
  totalSheet.getRange(1, 2).setValue('任务次数');
  
  let currentRow = 2;
  for (const [name, count] of Object.entries(nameCounts)) {
    totalSheet.getRange(currentRow, 1).setValue(name);
    totalSheet.getRange(currentRow, 2).setValue(count);
    currentRow++;
  }
  
  // 按任务次数升序排序(可选,可删除此行)
  totalSheet.getRange(2, 1, currentRow - 2, 2).sort({column: 2, ascending: true});
}
  1. 运行脚本
  • 保存脚本,给项目命名(比如「任务次数统计」)
  • 返回Google Sheet,点击「扩展程序」→「Apps Script」,选择函数calculateTaskCounts并点击运行
  • 首次运行需完成授权:若提示「Google尚未验证此应用」,点击「高级」→「转到[你的项目名](不安全)」即可继续授权

自定义调整说明

  • 姓名列位置:若姓名不在A列,修改sheetData[i][0]中的数字(A=0,B=1,以此类推)
  • 无表头场景:将循环起始值let i = 1改为let i = 0
  • 取消排序:删除脚本最后一行的排序代码即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 15:35:13