【问题标题】:adding timestamp on three google sheets onedit with Google Apps Script使用 Google Apps 脚本在三个 google 工作表 onedit 上添加时间戳
【发布时间】:2020-11-06 16:05:16
【问题描述】:

如果在 3 张纸上编辑 colb,我将在 col a 中添加时间戳。

我的触发器在编辑上工作,信息将自动更新。一个人可以正常工作,但对于 3 人来说,我遇到错误并且无法运行脚本。

有没有比我正在做的更简单的方法来在三张纸的 b 列中查找编辑?

function onEdit() {
   var s = SpreadsheetApp.getActiveSheet();
   if (s.getName() == "Igualdad Op Hom Mu") {
      var r = s.getActiveCell();
      if (r.getColumn() == 2) {
         var nextCell = r.offset(0, -1);
         if (nextCell.getValue() === '')
            nextCell.setValue(new Date());
         var s2 = SpreadsheetApp.getActiveSheet();
         if (s2.getName() == "Incl Per Disc") {
            var r2 = s2.getActiveCell();
            if (r2.getColumn() == 2) {
               var nextCell2 = r2.offset(0, -1);
               if (nextCell2.getValue() === '')
                  nextCell2.setValue(new Date());
               var s3 = SpreadsheetApp.getActiveSheet();
               if (s3.getName() == "Empr y Col") {
                  var r3 = s3.getActiveCell();
                  if (r3.getColumn() == 2) {
                     var nextCell3 = r3.offset(0, -1);
                     if (nextCell3.getValue() === '')
                        nextCell3.setValue(new Date());
                  }
               }
            }
         }
      }
   }
}

【问题讨论】:

标签: google-apps-script google-sheets timestamp triggers


【解决方案1】:

试试这个:

function onEdit(e) {
  const sh=e.range.getSheet();
  const shts=['Igualdad Op Hom Mu','Incl Per Disc','Empr y Col']
  if( shts.indexOf(sh.getName())!=-1  && e.range.columnStart==2) { 
    const ts=Utilities.formatDate(new Date,Session.getScriptTimeZone(),"yyyy/MM/dd HH:mm:ss");//time stamp
    const rg=e.range.offset(0,-1);//next cell to the left
    if(rg.getValue()=='') {
      rg.setValue(ts);
    }
  }
}

此函数需要 onedit 触发器事件对象,因此您无法从脚本编辑器运行此脚本,除非您提供事件对象。

【讨论】:

  • 这似乎是在正确的轨道上,但它没有定义第 2 行中的范围......而且我们不必定义 sh。在第 4 行运行?目前它没有运行。
  • 范围是e.range,我把s改成了sh
  • 库珀对不起,当你说范围是 e.range 时,它​​仍然没有定义,当我运行它时我得到一个错误......范围是否应该包括每张纸的两列我会用吗?
  • 你是如何运行它的?
  • 请注意,如果没有事件对象,您将无法运行此类函数,因此除非您提供事件对象,否则您无法从脚本编辑器运行它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多