通过谷歌表发送电子邮件,行完全完成

时间:2017-06-09 16:31:31

标签: google-sheets sendmail

我正在尝试修改我的代码,以便我可以根据指定行中的值发送电子邮件。

问题:在执行销售时,有几个人会同时使用此工作表,并且脚本要求发送包含一组不包含email_sent的“if”值的行的电子邮件。我想插入一行或只会发送已填写所有条件的行的电子邮件。我还想在发送后保护单元格,以便不能错误地编辑或删除它们。最后,我希望没有谷歌帐户的用户能够触发脚本。

    `// This constant is written in column L for rows for which an email
 `// has been sent successfully.`
`var EMAIL_SENT = "EMAIL_SENT";`

`function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:K2
  var dataRange = sheet.getRange(2,1,3,12)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0];  // First column
    var message  = Utilities.formatDate(row[1], "GMT" , "MM/dd/yyyy" );
    var message1 = row[2];    // third column
    var message2 = row[3];
    var message3 = row[4];
    var message4 = row[5];
    var message5 = row[6];
    var message6 = row[7];
    var message7 = row[8];
    var message8 = row[9];
    var message9 = Utilities.formatDate(row[10], "GMT" , "MM/dd/yyyy" );
    var emailSent = row[12];    // 12th column
    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      var subject = "Sending emails from a Spreadsheet";
      MailApp.sendEmail(emailAddress, subject, "\nSale date: " + message + "\nCustomer: " + message1 + "\nJob: " + message2 + "\nLender: " + message3 + "\nAged inventory: " + message4 + "\nreplacing CAN: " + message5 + "\nChange option: " + message6 + "\nSource: " + message7 + "\nRealtor: " + message8 + "\nClosing: " + message9);
      sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}`

1 个答案:

答案 0 :(得分:0)

可以使用IF和&amp;&amp ;;在一行中完成但维护它会很笨重而且耗时。

相反,添加一个新函数来测试该行。

for循环周围添加以下三个新行:

var bool;
for (var i = 0; i < data.length; ++i) {
   bool = testData(data[i])
   if(bool){

并且还在函数的结束行添加括号。

将以下功能添加到您的脚本文件中。

function testData(data) {
    var bool = true;
    for (var x = 0; x < data.length; x++) {
        if (!data[x]) {
            bool = false;
            break;
        }
    }
    return bool;
}

通过将行传递给测试函数来工作。如果任何单元格为空,则返回false,然后function sendEmails2()将跳过该行并移至下一行。

更新完整的工作代码

  // This constant is written in column C for rows for which an email
  // has been sent successfully.
  var EMAIL_SENT = "EMAIL_SENT";

  function sendEmails2() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var startRow = 2;  // First row of data to process
    var numRows = 2;   // Number of rows to process
    // Fetch the range of cells A2:K2
    var dataRange = sheet.getRange(2,1,3,12)
    // Fetch values for each row in the Range.
    var data = dataRange.getValues();

  for (var i = 0; i < data.length; ++i) {
     if(testData(data[i])){ // Pass the row of data to the testData function (testData will return true or false)
      var row = data[i];
      var emailAddress = row[0];  // First column
      var message  = Utilities.formatDate(row[1], "GMT" , "MM/dd/yyyy" );
      var message1 = row[2];    // third column
      var message2 = row[3];
      var message3 = row[4];
      var message4 = row[5];
      var message5 = row[6];
      var message6 = row[7];
      var message7 = row[8];
      var message8 = row[9];
      var message9 = Utilities.formatDate(row[10], "GMT" , "MM/dd/yyyy" );
      var emailSent = row[11];    // 12th column
      if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
        var subject = "Sending emails from a Spreadsheet";
        MailApp.sendEmail(emailAddress, subject, "\nSale date: " + message + "\nCustomer: " + message1 + "\nJob: " + message2 + "\nLender: " + message3 + "\nAged inventory: " + message4 + "\nreplacing CAN: " + message5 + "\nChange option: " + message6 + "\nSource: " + message7 + "\nRealtor: " + message8 + "\nClosing: " + message9);
        sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
        // Make sure the cell is updated right away in case the script is interrupted
        SpreadsheetApp.flush();
      }
    }
   }
  }


  function testData(data) { // Function to test the data 
      for (var x = 0; x < data.length - 1; x++) { // Loop through each cell (length -1 because we don't want to check the email sent cell as that should be empty)
          if (!data[x]) { // If a cell is empty return false
              return false;
          }
      }
      return true; // If no empty cells are found return true
  }