Google表格电子邮件脚本可通过电子邮件发送在不同单元格中编辑过的多个行单元格

时间:2020-04-05 10:42:28

标签: email google-apps-script google-sheets

当在同一行进行编辑时,我拼凑了一些代码以通过电子邮件发送某些单元格,我真的很挣扎。我的问题是我在同一电子表格上有多个工作表,目前我在编辑其他工作表时通过电子邮件发送给我。我知道我会做一些愚蠢的事情,但我无法解决。

我已经附上了脚本,任何帮助将不胜感激。

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

2 个答案:

答案 0 :(得分:1)

添加一个if条件,以测试您是否在所需的工作表上

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

答案 1 :(得分:0)

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