简体   繁体   中英

Google Sheet Email script to email multiple cells of row that was edited in a different cell

I am really struggling with some code I am putting together to email certain cells when a edit takes place on a the same row. The issue I have is I have multiple sheets on the same spreadsheet, and currently it is emailing me when I edit a different sheet. I know i will be doing something stupid but I can not work it out.

I have attached the script, any help would be greatly appreciated.

 function onSheetEdit(e) { var source = SpreadsheetApp.getActiveSpreadsheet();//Specifies the current spreadsheet var source_sheet = source.getSheetByName('SHOP Sites In Progress');//Specifies the sheet to monitor for changes var range = source_sheet.getDataRange();//Gets the data range for the entire sheet var ActiveRow = source_sheet.getActiveRange().getRow();//Identifies the row that the change occurred in var columns = "21";//Specifies how many columns of data your sheet has var data = source_sheet.getRange(ActiveRow,1,1,21).getValues();//(StartRow,StartColumn,NumberofRowstoGet,NumberofColumnstoGet) var emailAddressToNotify = 'test@email.com' var columnA = data[0][0]; var columnB = data[0][1]; var columnC = data[0][2]; var columnD = data[0][3]; var columnE = data[0][4]; var columnF = data[0][5]; var columnG = data[0][6]; var columnH = data[0][7]; var columnI = data[0][8]; var columnJ = data[0][9]; var columnK = data[0][10]; var columnL = data[0][11]; var columnM = data[0][12]; var columnN = data[0][13]; var columnO = data[0][14]; var columnP = data[0][15]; var columnQ = data[0][16]; var columnR = data[0][17]; var columnS = data[0][18]; var columnT = data[0][19]; var columnU = data[0][20]; var ss = SpreadsheetApp.getActiveSpreadsheet(); var emailSubject = 'Shop site in progress ' + 'Customer Name: ' + columnB + ' ' + columnC + ' ' + 'info updated' var sheet = SpreadsheetApp.getActiveSheet(); // var emailBody = 'Person1 has approved the item on row ' + range.getRow() + ' of spreadsheet "' + ss.getName() + '".\n\n'; var emailBody = Session.getActiveUser().getEmail() + ' Has approved the item on row ' +ActiveRow+ ' of spreadsheet "' + sheet.getName() + '".\n\n'; emailBody += '' emailBody += 'Customer PINCODE: ' + columnA + '\n'; emailBody += 'Customer Name: ' + columnB + ' ' + columnC +'\n'; emailBody += 'Business Name: ' + columnD +'\n\n'; emailBody += 'To open the spreadsheet, click this link: ' + ss.getUrl() + '\n\n'; emailBody += '(this is an automatically sent message)'; MailApp.sendEmail(emailAddressToNotify, emailSubject, emailBody); }

Add an if condition to test whether or not you are on the desired sheet

function onSheetEdit(e) {

    var source = SpreadsheetApp.getActiveSpreadsheet(); //Specifies the current spreadsheet

    if (source.getActiveSheet().getSheetName() == 'SHOP Sites In Progress') {

        var source_sheet = source.getSheetByName('SHOP Sites In Progress'); //Specifies the sheet to monitor for changes
        var range = source_sheet.getDataRange(); //Gets the data range for the entire sheet
function onSheetEdit(e) {
  var sh=e.range.getSheet();
  if(sh.getName()=='SHOP Sites In Progress') {
    var data=sh.getRange(e.range.rowStart,1,1,21).getValues()[0];
    var emailAddressToNotify='test@email.com';
    var emailSubject=Utilities.formatString('Shop site in progress Customer Name: %s %s info update',data[1],data[2]);
    var emailBody = Session.getActiveUser().getEmail() + ' Has approved the item on row ' + e.range.rowStart +  ' of spreadsheet "'  + sh.getName() + '".\n\n';
    emailBody  += '';
    emailBody  += 'Customer PINCODE : ' + data[0] + '\n' ;
    emailBody  += 'Customer Name: ' + data[1] + ' ' + data[2] +'\n';
    emailBody  += 'Business Name: ' + data[3] +'\n\n';  
    emailBody += 'To open the spreadsheet, click this link: ' + e.source.getUrl() + '\n\n';
    emailBody += '(this is an automatically sent message)';
    MailApp.sendEmail(emailAddressToNotify, emailSubject, emailBody);
  }
}

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