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

如何用Google AppScript按姓名/邮箱汇总对应时长?

Google AppScript 实现姓名/邮箱时长汇总

以下是实现需求的代码,能按姓名/邮箱汇总总时长并打印日志:

function calculateTotalDuration() {
  // 模拟表格数据,实际使用时可替换为表格读取逻辑
  const tableData = [
    ['Suzy', 5],
    ['Suzy', 2],
    ['Michael', 2],
    ['Michael', 6],
    ['Brandon', 1]
  ];

  // 存储汇总结果的对象
  const totalDuration = {};

  // 遍历数据完成求和
  tableData.forEach(row => {
    const name = row[0];
    const duration = row[1];
    totalDuration[name] = (totalDuration[name] || 0) + duration;
  });

  // 打印汇总结果到日志
  Object.entries(totalDuration).forEach(([name, total]) => {
    Logger.log(`${name}: ${total}`);
  });
}

代码说明

  • 替换真实数据:如果要读取表格中的实际数据,把tableData替换成以下代码即可(需替换Sheet1为你的工作表名称):
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
    const tableData = sheet.getDataRange().getValues();
    
  • 核心逻辑:用对象totalDuration的键存储姓名/邮箱,值存储对应总时长;遍历数据时,通过(totalDuration[name] || 0)处理首次出现的姓名,避免undefined导致的计算错误
  • 查看日志:执行函数后,点击AppScript编辑器右上角的「查看日志」按钮,就能看到汇总结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 12:27:45