Auto-timestamp multiple columns in Google Sheets

2019-06-14 14:44发布

I'm trying to use this code I adapted from http://www.internetgeeks.org/tech/add-timestamp-time-stamp-google-docs-spreadsheet/ to automatically timestamp cells upon entering question and answer text for a Google Sheets file. When I only use the code for one column it works, but when I duplicate and try to get it to work on two columns, it doesn't work.

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条回答
成全新的幸福
2楼-- · 2019-06-14 15:29

It doesn't work that way. You don't duplicate the onEdit trigger function and expect it to work all of a sudden. You have to understand the concepts of:

  • indexOf
  • getRow
  • getColumn
  • getRange
  • the if condition block which is the vital part of the code

Having said that, I've edited your code and hope you review it so you can modify it according to your need:

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

查看更多
Root(大扎)
3楼-- · 2019-06-14 15:30

Given that in this case we can be pretty confident that the the event.range will contain just the last cell edited (see https://developers.google.com/apps-script/guides/triggers/events) this should work:

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

If you protect the ranges in the sheet you don't want people to be able to edit (the headers, and the two timestamp columns) this should work ok.

查看更多
登录 后发表回答