Google表格的电子邮件脚本

时间:2015-01-12 17:26:32

标签: google-apps-script google-sheets

我需要帮助我在网上找到的电子邮件脚本。如果你想给B栏中的每个人发电子邮件,我发现这个脚本有用。我在A栏中创建了一个部分,询问我是否要通过电子邮件发送给那个人(因为我不打算给所有人发电子邮件),但我不是确定如何更改编码以识别它。我仍在努力学习/理解JavaScript。任何帮助都会很棒 谢谢。

您可以在下面查看我的表格:

https://docs.google.com/spreadsheets/d/1-lcCUN7ROBAhWNFsqura5M7LvhsQnzBBqE06jxxQkFM/edit?usp=sharing

    function sendEmails() {
   var sheet = SpreadsheetApp.getActiveSheet();
   var range = sheet.getRange(1,5);   // Fetch the range of cells E1:E1
   var CC = range.getValues();
   var range = sheet.getRange(2,5);  // Fetch the range of cells E2:E2
   var subject = range.getValues();   // Fetch value for subject line from above range
   var range = sheet.getRange(2, 9);  // Fetch the range of cells I2:I2
   var numRows = range.getValues();   // Fetch value for number of emails from above range
   var startRow = 5;                  // First row of data to process
   var dataRange = sheet.getRange(startRow, 2, numRows,8 ) // Fetch the range of cells B5:I_
   var data = dataRange.getValues();  // Fetch values for each row in the Range.
   for (i in data) {
      var row = data[i];
      var emailAddress = row[0];      // 1st column
      var message = row[7];           // 8th column
       MailApp.sendEmail({
         to: emailAddress,
         cc: CC,
         subject: subject,
         htmlBody: message,

       })}
}
}

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

  function sendEmails() {
   var sheet = SpreadsheetApp.getActiveSheet();
   var range = sheet.getRange(1,5);   // Fetch the range of cells E1:E1
   var CC = range.getValues();
  var ccEmail = CC[0][0];
    var range = sheet.getRange(2,5);  // Fetch the range of cells E2:E2
   var subject = range.getValues();   // Fetch value for subject line from above range
    var emailSubject = subject[0][0];
   var range = sheet.getRange(2, 9);  // Fetch the range of cells I2:I2
   var numRows = range.getValues();   // Fetch value for number of emails from above range
   var startRow = 5;                  // First row of data to process
    var endRow = numRows[0][0];
   var dataRange = sheet.getRange(startRow, 2, endRow,8 ) // Fetch the range of cells B5:I_
   var data = dataRange.getValues();  // Fetch values for each row in the Range.

   for (i in data) {
      var row = data[i];
      var sendEmail = row[0];
      if (sendEmail == "Yes"){
        var emailAddress = row[1];      // 2nd column
        var message = row[7];           // 8th column
        MailApp.sendEmail({  to: emailAddress,   cc: ccEmail , subject: emailSubject, htmlBody: message});

      }
}
}

编辑代码,因为getRange()方法获取二维数组中的值,您必须从数组中获取第一个值。 希望有所帮助!