3

I'm using a Google Spreadsheets and have a script that updates the "last email" column with a date stamp when an employee enters an email address into the prior column. The problem occurs when my employee pastes multiple rows of email addresses at a time. When he does this the script only puts a date stamp in the top row of what was pasted. The code I'm currently using is:

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Master Sheet" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 10 ) { //checks the column
    if( r.getValue() === '' ) {//don't update the timestamp if the cell is edited to a blank vaue
    }
     else
     {
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' ) {// only update the timestamp if it has not already been updated
     //Format the current date into datetime format (adjust to display how you want)
      var dateTime = Utilities.formatDate(new Date(), "EST-8", "MM-dd-yyyy");
      // Set the cell
      nextCell.setValue(dateTime);}
    }
  }
}
}

Is there a way to correct so that when he pastes multiple rows at once the date stamp is updated on all rows?

3

onEdit can be written to take an event parameter that among other things contains the range that was edited. Changing the function declaration to take a parameter 'event' and changing r to be event.range instead of the activecell gets the exact behavior I believe you want.

function onEdit(event) {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Master Sheet"" ) { //checks that we're on the correct sheet
    var r = event.range;
    if( r.getColumn() == 10 ) { //checks the column
    if( r.getValue() === '' ) {//don't update the timestamp if the cell is edited to a blank vaue
    }
     else
     {
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' ) {// only update the timestamp if it has not already been updated
     //Format the current date into datetime format (adjust to display how you want)
      var dateTime = Utilities.formatDate(new Date(), "EST-8", "MM-dd-yyyy");
      // Set the cell
      nextCell.setValue(dateTime);}
    }
  }
}
}
1

That's not possible via the onEdit trigger in Google Apps Script (I hope @TomHorwood proves me wrong). The first cell that's being used to paste from, is only used to trigger the onEdit, as you have described yourself.

I can think of the following two options, that might help:

  1. time based trigger
  2. manual trigger via a menu option

The first option will automatically update the dates, throughout whatever range you set it to be. The second option will allow you to paste multiple values at once, and trigger the selected range afterwards.

In both cases, you need to re-write your code. If you need help with that, just re-ask.

  • Jacob, I like the idea of a time based trigger. Any chance I could talk you into helping me out with the code for something like that? I'm a rookie. – Jon B Jones Jan 16 '14 at 19:24
  • See the answer by Jared Pinkham – Rubén May 31 '18 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.