Google脚本可自动起草电子邮件

时间:2018-12-20 17:31:23

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

我的目标是从Google表格列表中自动创建电子邮件草稿。一切正常,我唯一的问题是电子邮件中的消息仅显示[object Object]。

主题很好,电子邮件地址很好,可以让它再次发送给同一个人的时间可以是个好习惯。

我不确定是什么问题。...但是我猜想它始于//构建电子邮件。

有什么建议吗?

我有以下内容:

var EMAIL_DRAFTED = "EMAIL DRAFTED";

function draftPastDueEmails() {
  var sheet = SpreadsheetApp.getActiveSheet(); // Use data from the active sheet
  var startRow = 2;                            // First row of data to process
  var numRows = sheet.getLastRow() - 1;        // Number of rows to process
  var lastColumn = sheet.getLastColumn();      // Last column
  var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn) // Fetch the data range of the active sheet
  var data = dataRange.getValues();            // Fetch values for each row in the range
  
  // Work through each row in the spreadsheet
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];  
    // Assign each row a variable
    var clientName = row[12];               // Client name
    var clientEmail = row[14];              // Client email
    var reference = row[1];                 // Account Reference
    var invNum = row[0];                    // Invoice number
    var emailStatus = row[20];              // Email Status
    
    // Prevent from drafing duplicates and from drafting emails without a recipient
    if (emailStatus !== EMAIL_DRAFTED && clientEmail) {  
    
      // Build the email message
    var emailBody =  '<p>Hello ' +clientName+ ',</p>';
        emailBody += '<p>I hope all is well.</p>';
        emailBody += '<p>I noticed that invoice <strong>' +invNum+ '</strong> is currently past due.</p>';
        emailBody += '<p>Could you please take a moment to check your records to see if a payment has been made and provide a receipt to allow me to track it in my system.</p>';
        emailBody += '<p>If no payment has been made for whatever reason, please do so as soon as possible so I can post it to your account.</p>';
        emailBody += '<p>Please let me know if you have any questions.</p>';
        emailBody += '<p>Best,</p>';
      
      // Create the email draft
      GmailApp.createDraft(
        clientEmail,            // Recipient
        'Past due account - '+reference+ // Subject
        '',
        {
        htmlBody: emailBody    // Options: Body (HTML)
        }
      );
      
      sheet.getRange(startRow + i, lastColumn).setValue(EMAIL_DRAFTED); // Update the last column with "EMAIL_DRAFTED"
      SpreadsheetApp.flush(); // Make sure the last cell is updated right away
    }
  }
}

1 个答案:

答案 0 :(得分:0)

问题似乎出在这里:

      // Create the email draft
      GmailApp.createDraft(
        clientEmail,            // Recipient
        'Past due account - '+reference+ // Subject
        '',
        {
        htmlBody: emailBody    // Options: Body (HTML)
        }
      );

该方法需要3个字符串(收件人,主题和正文),后跟带有“ htmlBody”参数的选项。上面的代码仅提供收件人和主题。请尝试以下方法:

      // Create the email draft
      GmailApp.createDraft(
        clientEmail,            // Recipient
        'Past due account - '+reference, // Subject
        '', // Plaintext body
        {
        htmlBody: emailBody    // Options: Body (HTML)
        }
      );