通过Google Spreadsheet的邮件发送整个专栏

时间:2013-09-06 20:01:07

标签: javascript google-apps-script google-sheets google-docs

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

我设法在将D3放入D3时发送了一封电子邮件,但我没有得到正确的内容,我最终收到了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();
    }
    }

萨姆

1 个答案:

答案 0 :(得分: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
    });
  }
}