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

Google Sheet脚本修改:指定列更新时生成时间戳

定制Google Apps Script实现指定列触发时间戳生成

核心需求实现脚本

如果只需要满足仅F、J、L、M、S、T、U、W列编辑时生成时间戳的核心需求,用下面的脚本:

function onEdit(e) {
  // 指定生效的工作表名称
  const sheetNames = ["Acads Progress"];
  // 需要触发时间戳的目标列(对应F=6、J=10、L=12、M=13、S=19、T=20、U=21、W=23)
  const targetColumns = [6, 10, 12, 13, 19, 20, 21, 23];
  const timestampCol = 32; // 时间戳写入的列

  const { range } = e;
  const sheet = range.getSheet();
  const editedCol = range.columnStart;

  // 校验:不在指定工作表 或 编辑的是时间戳列 → 直接退出
  if (!sheetNames.includes(sheet.getSheetName()) || editedCol === timestampCol) return;

  // 校验:编辑的列不在目标列数组 → 直接退出
  if (!targetColumns.includes(editedCol)) return;

  // 写入时间戳到对应行的32列
  sheet.getRange(range.rowStart, timestampCol).setValue(new Date());
}

核心修改说明

  • 新增targetColumns数组,把需要触发的列转换成对应的数字列号(比如F列是第6列)
  • 增加列校验逻辑:只有编辑的列在目标数组内,才会生成时间戳
  • 保留原有的工作表名称校验,避免脚本影响其他无关工作表

包含次要需求的完整脚本

如果需要同时满足U列设为"drop-out"时生成最终时间戳,直到U列修改为其他值的次要需求,用下面的完整脚本:

function onEdit(e) {
  // 指定生效的工作表名称
  const sheetNames = ["Acads Progress"];
  // 需要触发时间戳的目标列(对应F=6、J=10、L=12、M=13、S=19、T=20、U=21、W=23)
  const targetColumns = [6, 10, 12, 13, 19, 20, 21, 23];
  const timestampCol = 32; // 时间戳写入的列
  const dropoutValue = "drop-out";
  const dropoutCol = 21; // U列对应的列号

  const { range } = e;
  const sheet = range.getSheet();
  const editedCol = range.columnStart;
  const editedRow = range.rowStart;

  // 基础校验:不在指定工作表 或 编辑的是时间戳列 → 直接退出
  if (!sheetNames.includes(sheet.getSheetName()) || editedCol === timestampCol) return;

  // 列校验:编辑的列不在目标列数组 → 直接退出
  if (!targetColumns.includes(editedCol)) return;

  // 处理次要需求:检查当前行U列状态
  const currentDropoutStatus = sheet.getRange(editedRow, dropoutCol).getValue();
  if (currentDropoutStatus === dropoutValue) {
    // 如果U列是drop-out,只有当这次编辑的是U列且改成了非drop-out值时,才允许更新时间戳
    if (!(editedCol === dropoutCol && e.value !== dropoutValue)) {
      return;
    }
  }

  // 写入时间戳
  sheet.getRange(editedRow, timestampCol).setValue(new Date());
}

次要需求逻辑说明

  • 当U列被修改为drop-out时,会触发写入最终时间戳
  • 之后无论编辑其他目标列,只要U列还是drop-out,时间戳都不会更新
  • 只有当U列被修改为非drop-out值时,才会恢复时间戳的更新功能

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 12:40:55