Javascript 在Google工作表中自动为多列添加时间戳

Javascript 在Google工作表中自动为多列添加时间戳,javascript,google-apps-script,google-sheets,timestamp,Javascript,Google Apps Script,Google Sheets,Timestamp,我正试图使用我改编的代码,在输入Google Sheets文件的问答文本时自动为单元格添加时间戳。当我只对一列使用代码时,它可以工作,但当我复制代码并试图使其在两列上工作时,它就不工作了 这样不行。您不会复制该函数,并期望它会突然工作。 您必须了解以下概念: 索引 格特罗 getColumn getRange if条件块是代码的重要部分 话虽如此,我已经编辑了您的代码,希望您能对其进行审查,以便根据需要进行修改: function onEdit(event) { var timezo

我正试图使用我改编的代码,在输入Google Sheets文件的问答文本时自动为单元格添加时间戳。当我只对一列使用代码时,它可以工作,但当我复制代码并试图使其在两列上工作时,它就不工作了


这样不行。您不会复制该函数,并期望它会突然工作。 您必须了解以下概念:

  • 索引
  • 格特罗
  • getColumn
  • getRange
  • if条件块是代码的重要部分
话虽如此,我已经编辑了您的代码,希望您能对其进行审查,以便根据需要进行修改:

function onEdit(event)
{ 
  var timezone = "GMT-5";
  var timestamp_format = "MM-dd-yyyy"; // Timestamp Format. 
  var updateColName = "Address";
  var timeStampColName = "Date Sent";
  var sheet = event.source.getSheetByName('Sheet1'); //Name of the sheet where you want to run this script.

  var responseArray = ["Response1", "Response2","Response3"];
  var questionArray = ["Question1", "Question2", "Question3"];

  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRow();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();//get values and place them inside array 
  var dateCol = headers[0].indexOf(timeStampColName); //get index position inside the array

 for(var i = 0; i < questionArray.length ; i ++){   
      if (headers[0].indexOf(responseArray[1]) > -1 && index > 1 && editColumn == (headers[0].indexOf(questionArray[i])+1) ) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!

          var cell = sheet.getRange(index, (headers[0].indexOf(responseArray[i])+1) ); 
          var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
          cell.setValue(date); 
      }
   }
}
函数onEdit(事件)
{ 
var timezone=“GMT-5”;
var timestamp_format=“MM dd yyyy”//时间戳格式。
var updateColName=“地址”;
var timeStampColName=“发送日期”;
var sheet=event.source.getSheetByName('Sheet1');//要运行此脚本的工作表的名称。
var responseArray=[“Response1”、“Response2”、“Response3”];
变量questionArray=[“问题1”、“问题2”、“问题3”];
var actRng=event.source.getActiveRange();
var editColumn=actRng.getColumn();
var index=actRng.getRow();
var headers=sheet.getRange(1,1,1,sheet.getLastColumn()).getValues();//获取值并将其放入数组中
var dateCol=headers[0].indexOf(timeStampColName);//获取数组中的索引位置
对于(var i=0;i-1&&index>1&&editColumn==(headers[0].indexOf(questionArray[i])+1)){//如果“Last Updated”头存在,则只有时间戳,但不在头行本身!
var cell=sheet.getRange(index,(headers[0].indexOf(responseArray[i])+1));
var date=Utilities.formatDate(新日期(),时区,时间戳_格式);
单元格设置值(日期);
}
}
}

在这种情况下,我们可以非常确信event.range将只包含最后一个编辑的单元格(请参阅),这应该可以:

function onEdit(event) { 
  var timezone = "GMT-4";
  var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format
  var date = Utilities.formatDate(new Date(), timezone, timestamp_format);

  var range = event.range; // this will be the last cell edited
  var column = range.getColumn(); // use this to check the cell edited was in a relevant column
  var rangeValue = range.getValue(); // use this to check something was actually entered into the last cell edited
  var newRange  = range.offset(0,1); // this is one cell to the right of the last cell edited

  if ((column == 2 || column == 4) && rangeValue) { //if the last cell edited was in a relevant column, and there is a value in it
    newRange.setValue(date);
  } else if ((column == 2 || column == 4) && !rangeValue) { // there was no question or no response, so make sure there is no timestamp
    newRange.setValue("");
  }
}
如果您保护工作表中的范围,您不希望人们能够编辑(标题和两个时间戳列),这应该可以

function onEdit(event) { 
  var timezone = "GMT-4";
  var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format
  var date = Utilities.formatDate(new Date(), timezone, timestamp_format);

  var range = event.range; // this will be the last cell edited
  var column = range.getColumn(); // use this to check the cell edited was in a relevant column
  var rangeValue = range.getValue(); // use this to check something was actually entered into the last cell edited
  var newRange  = range.offset(0,1); // this is one cell to the right of the last cell edited

  if ((column == 2 || column == 4) && rangeValue) { //if the last cell edited was in a relevant column, and there is a value in it
    newRange.setValue(date);
  } else if ((column == 2 || column == 4) && !rangeValue) { // there was no question or no response, so make sure there is no timestamp
    newRange.setValue("");
  }
}