Reputation: 23
I have a general script that will send email notification of changes made to a live roster in Google sheets and have set up a trigger for the event to occur 'On change'. All data in the sheet is retrieved via IMPORTRANGE. This script is performing as expected, however I wish to make a couple of changes that am not sure how to go about. Here is a sample sheet: link to sheet
Wish to only send email notification when changes concern the name 'Craig'. For example my name is either added or taken off a rostered day. The script currently sends emails for all changes made across the sheet.
ID the row that this change occurs so as to reference the date and venue in the email body.
Thanks for any help and suggestions.
Full script:
function onEdit() {
var getProps = PropertiesService.getUserProperties();
var lenProp = getProps.getProperty('Len');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Roster');
var data = sheet.getRange('E4:ACB562').getValues().toString().length;
if (data != lenProp) {
getProps.setProperty('Len', data );
MailApp.sendEmail('[email protected]', 'Changes have been made to your live roster', 'Previous value: ' + lenProp + ' New value: ' + data);
}
}
New revised script:
function installedOnEdit(e) {
var range = e.range;
var value = range.getValue();
if (value == "Craig" || (e.oldValue == "Craig" && value == "")) {
var rowNumber = range.getRow(); // You can retrieve the row number of the editor cell.
console.log(rowNumber)
var [colA, colB] = e.source.getActiveSheet().getRange(rowNumber, 1, 1, 2).getValues()[0];
var getProps = PropertiesService.getUserProperties();
var lenProp = getProps.getProperty('Len');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Roster_LIVE');
var data = sheet.getRange('E4:ACB562').getValues().toString().length;
if (data != lenProp) {
getProps.setProperty('Len', data);
MailApp.sendEmail('[email protected]', 'Changes have been made to your roster!', 'Your live roster has been edited. These changes concern ' + colB + ' on ' + colA);
}
}
}
Upvotes: 0
Views: 925
Reputation: 18751
This sheet does not get edited, but rather changes occur through IMPORTRANGE.
No events get triggered when a formula result changes, so I do not think that you can get an automatic notification when values get updated in the imported data.
To make it work, you will have to use a trigger in the source spreadsheet rather than the target spreadsheet.
Upvotes: 1
Reputation: 201348
I believe your goal as follows.
Craig
is put to a cell.Craig
is removed.I have a general script that will send email notification of changes made to a live roster in Google sheets and have set up a trigger for the event to occur 'On change'.
, I understood that you are using the installable OnChange trigger. If it's so, in your situation, I would like to recommend to use the installable OnEdit trigger.onEdit
. If the installable OnEdit trigger installs to onEdit
, when a cell is edited, onEdit
is run 2 times by the simple trigger and the installable trigger with the asynchronous process. Ref So, please rename the function name from onEdit
to other and reinstall the installable OnEdit trigger to the renamed function name.When above points are reflected to your script, it becomes as follows.
After the script was modified, please install the OnEdit trigger to the function of installedOnEdit
. In this script, when you put the value of Craig
to a cell, the script below the if statement is run.
function onEdit() {
var getProps = PropertiesService.getUserProperties();
var lenProp = getProps.getProperty('Len');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Roster');
var data = sheet.getRange('E4:ACB562').getValues().toString().length;
if (data != lenProp) {
getProps.setProperty('Len', data );
MailApp.sendEmail('[email protected]', 'Changes have been made to your live roster', 'Previous value: ' + lenProp + ' New value: ' + data);
}
}
To:
function installedOnEdit(e) {
var range = e.range;
var value = range.getValue();
if (value == "Craig" || (e.oldValue == "Craig" && value == "")) {
var rowNumber = range.getRow(); // You can retrieve the row number of the editor cell.
console.log(rowNumber)
var getProps = PropertiesService.getUserProperties();
var lenProp = getProps.getProperty('Len');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Roster');
var data = sheet.getRange('E4:ACB562').getValues().toString().length;
if (data != lenProp) {
getProps.setProperty('Len', data);
MailApp.sendEmail('[email protected]', 'Changes have been made to your live roster', 'Previous value: ' + lenProp + ' New value: ' + data);
}
}
}
Craig
is put to a cell, the script below the if statement is run. If you want to also check the column and row, please tell me.Upvotes: 1