简体   繁体   中英

How to time stamp more than one cell on Google Sheets?

In my Gooogle sheet document at the moment is have 4 cells that regularly get updated. Each time these are updated i want to time stamp a separate cell.

I can get the attached script to work for 1 cell but when i duplicate the script to get it working for another cell it stops the earlier one from working.

function onEdit(event)
{ 
  var timezone = "GMT+1";
  var timestamp_format = "dd-MM-yyyy hh:mm:ss"; 
  var updateColName = "Development Team Comments";
  var timeStampColName = "To update select cell";
  var sheet = event.source.getSheetByName('WIP'); 


  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) { 
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
    cell.setValue(date);
  }
}

I have also tried this code which doesn't seem to work for the subsequent cells:

function onEdit(event)
{ 
  var timezone = "GMT+1";
  var timestamp_format = "dd-MM-yyyy hh:mm:ss"; // Timestamp Format. 
  var updateColName = "Development Team Comments";
  var timeStampColName = "To update select cell";
  var sheet = event.source.getSheetByName('WIP'); //Name of the sheet where you want to run this script.

  var responseArray = ["o update select cell", "Bupa time stamp","Date Stamp Move Live"];
  var questionArray = ["Development Team Comments", "BDC proofing comments", "Approved Y"];


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

What i expect i should be able to do is to be able to update any of the cells and for them to present the current time as the script above does. Do i need to duplicate this or is there a way of inserting in the cell references.

Looks like your script is pretty complicated for what you actually need, it's checking all of the headers etc when really all you need is a few setValue() based on if statements, throw this inside an onEdit(e) trigger and you're done!

function onEdit(e) {
  var sheet = e.source.getSheetByName('WIP');
  var timezone = 'GMT+1';
  var format = 'dd-MM-yyyy hh:mm:ss';

  var cell = e.range;
  var row = cell.getRow();
  var col = cell.getColumn();

  var stamp = Utilities.formatDate(new Date(), timezone, format);

  if (col === 1) {                          //if edited cell is in column A
    sheet.getRange(row, 4).setValue(stamp); //set timestamp in column D
  }
  if (col === 2) {                          //if edited cell is in column B
    sheet.getRange(row, 5).setValue(stamp); //set timestamp in column E    
  }
  if (col === 3) {                          //if edited cell is in column C
    sheet.getRange(row, 6).setValue(stamp); //set timestamp in column F
  }
}

This script will work up to column C, if you want to add more you can just adjust the numbers in the if statements and getRange() to fit your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM