0

I'm completely new to writing app scripts. Have been trying to find an answer to my question online but unfortunately without any success. Therefore I'm reaching out to the community for help.

In my g sheet, I have two tabs one is an input tab (X) and the other one is an output (Y). What I am trying to accomplish is to send an email notification to a given email address every time a cell has been edited in the output tab (Y). The output tab has an editable range from B2:Y61. I have found plenty of stuff online on the "on edit" feature. However, non of them was close to my example mostly because they have been referring to getActivetSheet and not a specific one. The simpler code the better.

For instance, I found the following post very interesting. Unfortunately have no idea how to make it work with a specific tab name. Furthermore, if copied and pasted exactly, the formula returns an error, that row reference has not been declared.

https://spreadsheet.dev/send-email-when-google-sheet-is-edited

Look forward to hearing from you!

Best, D

Ducatia
  • 31
  • 2
  • 8

1 Answers1

1

You can use this:

function onEdit(e) {
  var editedSheet = e.source.getActiveSheet();
  var editedCell = e.range;
  var row = editedCell.getRow();
  var col = editedCell.getColumn();
  
  //Check if B2:Y61 was modified in Sheet "Y" based on its row and column index
  if(row>=2 && row<=61 && col>=2 && col<=25 && editedSheet.getName()=="Y"){
    //Send email
    var subject = "Sheet: "+editedSheet.getName();
    var message = "Cell "+editedCell.getA1Notation()+" was modified from '"+e.oldvalue+"' to '"+e.value+"'";
    MailApp.sendEmail("email address",subject,message);
  }
}

What it does?

  • Using the Google Sheets events, you can get the active sheet that is being modified using e.source, get the cell being modified using e.range and even get the old and the new value of the cell using e.oldValue and e.value
  • You just need to include a condition to check if the cell modified is within the range that you prefer B2:Y61 which should have a min row = 2, max row = 61, min col = 2 and max col = 25 and sheet name should be "Y".
  • Send an email using MailApp.sendEmail() if all the conditions were met.

Note:

You need to create this as an installable trigger since it will use mail service which requires authentication.

To manually create an installable trigger in the script editor, follow these steps:

enter image description here

enter image description here

OUTPUT: enter image description here


(UPDATE)

This code will copy changes done in X_input sheet to Y_input sheet and check if email should be sent when B2:Y61 range was modified.

function onEdit(e) {
  var editedSheet = e.source.getActiveSheet();
  var editedCell = e.range;
  var row = editedCell.getRow();
  var col = editedCell.getColumn();
  
  var ySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Y_output");

  //Check if edit is done in Sheet "X_input"
  if(editedSheet.getName()=="X_input"){
    //copy modified cell to Y_output sheet
    ySheet.getRange(row,col).setValue(e.value);


    //check if modified cell is within B2:Y61 range and send an email
    if(row>=2 && row<=61 && col>=2 && col<=25){
      //Send email
     var subject = "Sheet: "+editedSheet.getName();
      var message = "Cell "+editedCell.getA1Notation()+" was modified from '"+e.oldvalue+"' to '"+e.value+"'";
      MailApp.sendEmail("ronoel@google.com",subject,message);
    }
  }
 
}
Ron M
  • 5,791
  • 1
  • 4
  • 16
  • Ron M, you've just made my day ;) Thank you very much for sharing your script with me, along with additional comments. Everything works just as you described as long as you keep making entries on the "Y_output" sheet. – Ducatia Jan 18 '21 at 14:49
  • It just makes me wonder, how to modify your code so that it still will be working on the "Y_output" sheet while entering data in the "X_input" sheet. In other words "X_input" is the front that is visible to the user, whereas "Y_output" most likely will be hidden I attached below a link to a dummy spreadsheet for your reference. https://docs.google.com/spreadsheets/d/1CynAikVmevjHlhnScPRapS2GpoWTMiEegWBOwUuwkUU/edit?usp=sharing Greetings from Poland! Best, D – Ducatia Jan 18 '21 at 14:49
  • In your original question, you want to send emails whenever Y sheet was modified. But now you want that when X_input sheet was modified it will check if the modification is within range and send an email? at the same time it should be copied to Y_output sheet? I don't see any point why you still need the Y_output sheet? – Ron M Jan 18 '21 at 15:10
  • Hi Ron M, the general logic behind it, is that only the X_input sheet will be visible to the user and not Y. The input sheet will look more like a form than a table, where the user will be entering information and afterward sending it to the Y sheet by submission button. That's why it would be great if the code would work on the Y sheet instead of X. Do you think you can help? – Ducatia Jan 22 '21 at 10:43
  • I think that is possible, but you mentioned that data from input sheet will be in a form type with submit button. However, based on the sample sheet that you provided Y_output is just a mirror copy of X_input. If you want to reflect the changes done in X_input to Y_output using onEdit() and still send an email if modified cell is within B2:Y61 range then see the updated answer. – Ron M Jan 22 '21 at 16:18
  • Hi Ron M, I'm sorry for confusing you with the initial message. You're right, I should have been more specific in my initial message. Thus, thank you very much for all your time and attention dedicated to finding the answer to my question! After your small modifications, now all work just the way it supposed. Once again thanks a ton! – Ducatia Jan 29 '21 at 08:23
  • No problem, if you find this helpful feel free to accept or upvote the answer as reference to others – Ron M Jan 29 '21 at 14:48
  • Ron, I mentioned you in my other post but not sure if you're going to receive a notification so I'm reaching also here. Would you be so kind and look into my issue with your professional eye, please? Regards, D! @Ron M https://stackoverflow.com/questions/66384642/send-data-from-one-google-sheet-to-a-another-google-sheet-with-app-script/66385323#66385323 – Ducatia Mar 03 '21 at 11:19