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

Google Sheets编辑B列时多列自动生成时间戳与日期的脚本问题

问题根因

同一Google Apps Script项目中不允许存在两个同名的onEdit触发函数,脚本运行时只会调用最后声明的那一个,这就是你其中一个功能无法生效的核心原因。

解决方案

将两个功能的逻辑合并到同一个onEdit函数内即可,合并后的代码如下:

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  // 仅处理Guests工作表的编辑事件
  if( s.getName() == "Guests" ) {
    var r = s.getActiveCell();
    // 仅响应B列的编辑操作
    if( r.getColumn() == 2 ) {
      // 处理C列时间戳写入
      var timeCell = r.offset(0,1);
      if( timeCell.getValue() === '' ) {
        timeCell.setValue(new Date());
        // 如果需要固定时间戳显示格式,可添加下面这行代码,格式可按需调整
        // timeCell.setNumberFormat('yyyy-MM-dd HH:mm:ss');
      }
      // 处理G列日期写入
      var dateCell = r.offset(0,5);
      if( dateCell.getValue() === '' ) {
        dateCell.setValue(new Date(new Date().setHours(0,0,0,0))).setNumberFormat('dd/MM/yyyy');
      }
    }
  }
}

替换原有代码后保存即可生效,B列有内容录入时会同时完成C列时间戳、G列日期的写入,且仅会在对应列单元格为空的时候写入,不会覆盖已有内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 13:48:03