0

我在 Google 电子表格中有一个列(C 列),我想通过电子邮件发送我在例如单元格 D3 中放置了一个“X”。

我在 D3 中放置 X 时设法发送了一封电子邮件,但内容不正确,我最终收到 C 列中每一行的不同邮件,而不是一封包含 C 列所有内容的邮件

任何帮助将非常感激,

编辑:添加了代码,我知道非常业余,我不知道这些东西的分配..

var EMAIL_SENT = "EMAIL_SENT";  //text to put when mail has been sent

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1;  // First row of data to process
  var numRows = 999;   // Number of rows to process
  var dataRange = sheet.getRange(1,1, numRows, 3)
  //start on row 1 untill numRows
  //and 3 columns wide
  var data = dataRange.getValues(); 
  //get content of cell A1
  for (var i = 0; i < data.length; ++i) {
    //start loop to search for data
  var row = data[i];
  var emailSent = sheet.getRange("F3").getValue(); 
  var readytosend = sheet.getRange("D3").getValue();
    if (emailSent != EMAIL_SENT && readytosend == "X") {

      MailApp.sendEmail({
        to: "mail@mail.com",
        subject:  "test",
        body: row[0],       
        });
      //sends the email

      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      //sets email_sent value and increases i to check next row
      SpreadsheetApp.flush();
    }
    }

山姆

4

1 回答 1

0

在您的代码中,您为每个单元格发送一封电子邮件。如果您想发送一次电子邮件,请保存每行的值并在最后发送。下面的示例可以做到这一点。

var EMAIL_SENT = "EMAIL_SENT";  //text to put when mail has been sent

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1;  // First row of data to process
  var numRows = sheet.getMaxRows();   // Number of rows to process
  var dataRange = sheet.getRange("C" + startRow + ":C" + numRows);
  var data = dataRange.getValues();
  var htmlBody = "";
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailSent = sheet.getRange("F3").getValue(); 
    var readytosend = sheet.getRange("D3").getValue();
    if (emailSent != EMAIL_SENT && readytosend == "X") {
      htmlBody += row[0] + "<br/>";
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      SpreadsheetApp.flush();
    }
  }

  if (htmlBody && htmlBody.length) {
    MailApp.sendEmail({
        to: "user@gmail.com",
        subject:  "Subject",
        body: "Email body",
        name: "Name",
        htmlBody: htmlBody
    });
  }
}
于 2013-09-06T21:19:55.447 回答