自动为Google表格中的多个列添加时间戳

时间:2017-06-30 14:44:45

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

我正在尝试使用我从http://www.internetgeeks.org/tech/add-timestamp-time-stamp-google-docs-spreadsheet/改编的代码,在输入Google表格文件的问答文本时自动为单元格添加时间戳。当我只使用一列的代码时它可以工作,但是当我复制并尝试让它在两列上工作时,它不起作用。

Picure of what I want to do.

function onEdit(event)
{ 
   var timezone = "GMT-4";
   var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format. 
   var updateColName = "Question/Comment";
   var timeStampColName = "Date/Time Sent";
   var sheet = event.source.getSheetByName('ArtsVision Questions/Comments'); //Name of the sheet where you want to run this script.

   var actRng = event.source.getActiveRange();
   var editColumn = actRng.getColumn();
   var index = actRng.getRowIndex();
   var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
   var dateCol = headers[0].indexOf(timeStampColName);
   var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
   if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
       var cell = sheet.getRange(index, dateCol + 1);
       var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
       cell.setValue(date);
   }
}

function onEdit(event)
{ 
   var timezone = "GMT-4";
   var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format. 
   var updateColName = "Response";
   var timeStampColName = "Date/Time Answered";
   var sheet = event.source.getSheetByName('ArtsVision Questions/Comments'); //Name of the sheet where you want to run this script.

   var actRng = event.source.getActiveRange();
   var editColumn = actRng.getColumn();
   var index = actRng.getRowIndex();
   var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
   var dateCol = headers[0].indexOf(timeStampColName);
   var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
   if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
      var cell = sheet.getRange(index, dateCol + 1);
      var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
      cell.setValue(date);
   }
}

2 个答案:

答案 0 :(得分:0)

它不起作用。您不会复制onEdit trigger函数,并希望它能够突然发挥作用。 你必须理解以下的概念:

  • 的indexOf
  • 的getRow
  • 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); 
      }
   }
}

enter image description here

答案 1 :(得分:0)

鉴于在这种情况下,我们可以非常确信event.range将仅包含编辑的最后一个单元格(请参阅https://developers.google.com/apps-script/guides/triggers/events)这应该有效:

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("");
  }
}

如果您保护工作表中的范围,您不希望人们能够编辑(标题和两个时间戳列),这应该可以正常工作。