Google表格 - 发送电子邮件

时间:2015-04-15 07:22:16

标签: google-apps-script google-sheets

之前我做了这个帖子,但我确实得到了很多预期的帮助。让我借此机会尽量保持清醒。 1.我有这个谷歌表ID 1UJVQGlCpsxtqN-IkRFFgHEnzNaq-HQDaiw-hte5j3LY

Email Address   Name   Mark1    Mark2    Mark3  Remark  Email Sent
abc@gmail.com   John    34      21       34     Fair    
def@gmail.com   Sam     23      9        78     fair    
  1. Tamplate Doc。 ID 15SP7mxyKYF6ptuRfrudNsvtJuezMTOBUL_cRCFbBIcI
  2. 亲爱的keyName

    标记1 MARK2 MARK3 keyMark_1 keyMark_2 keyMark_3

    备注 keyRemark

    1. 代码。

      //在G列中写入电子邮件的行的Email_Sent     //已成功发送。     var EMAIL_SENT =" EMAIL_SENT&#34 ;;

      function sendEmails2() {
        var sheet = SpreadsheetApp.getActiveSheet();
        var startRow = 2;  // First row of data to process
        var numRows = 5;   // Number of rows to process
        var dataRange = sheet.getRange(startRow, 1, numRows, 6)
        var ss = SpreadsheetApp.openById('1UJVQGlCpsxtqN-IkRFFgHEnzNaq-HQDaiw-hte5j3LY'); 
        var sheet = ss.getSheetByName('Email Spreadsheet Data');
        //var dataRange = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
        var copyId = DocsList.getFileById('15SP7mxyKYF6ptuRfrudNsvtJuezMTOBUL_cRCFbBIcI')
          .makeCopy(name)
          .getId();
      
        // Fetch values for each row in the Range.
        var data = dataRange.getValues();
        for (var i = 0; i < data.length; ++i){
        //for (var j = 0; j < startRow; ++j) {
          var row = data[i];
          var emailAddress = row[0];  // First column
          var name = row[1]; // Second column
          var Mark1 = row[2]; // Third column
          var Mark2 = row[3]; // Fourth column
          var Mark3 = row[4]; // 
          var remark = row[5]; // 
          var emailSent = row[6]; // 
          var static_copy = ('goins@mu.ac.ke');
      
          // Open the temporary document
        var copyDoc = DocumentApp.openById(copyId);
        // Get the document’s body section
        var copyBody = copyDoc.getActiveSection();
      
      // Replace place holder keys,in google doc template. Create a blank document with the number "1,2,3....", and it will replace this text with value
        copyBody.replaceText('keyName', name)
        copyBody.replaceText('keyMark_1', Mark1)
        copyBody.replaceText('keyMark_2', Mark2)
        copyBody.replaceText('keyMark_3', Mark3)
        copyBody.replaceText('keyRemark', remark)
      
      // Save and close the temporary document
        copyDoc.saveAndClose();
      
      // Convert temporary document to PDF
        var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
      
      // Attach PDF and send the email
        var subject = "Profile";
        var body = "<HTML><BODY>"
                   + "Dear "+ name +",<br>"
                   + "<br>"
                   + " Regards <br>"
                   + "<br>"
                   + "</HTML></BODY>";
      
        MailApp.sendEmail(emailAddress, subject, body, 
                         {htmlBody: body, attachments: pdf, cc: static_copy,});
       //if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
            //var subject = "Sending emails from a Spreadsheet";
           // MailApp.sendEmail(emailAddress, subject, {htmlBody: body, attachments: pdf, cc: static_copy,});
          //sheet.getRange(startRow + i, 6).setValue(EMAIL_SENT);
         // Delete temp file
        DocsList.getFileById(copyId).setTrashed(true);  
            } // Make sure the cell is updated right away in case the script is interrupted
           SpreadsheetApp.flush();
          }
      
    2. 当我执行上述脚本时,带有John标记的电子邮件附件将发送到adc@gmail.com和efg@gmail.com。 我需要你的帮助才能解决这个问题,这样我就可以给每个学生发送一封带有结果的电子邮件附件。

      请帮助

      塞米。

1 个答案:

答案 0 :(得分:3)

copyId变量应该在for循环中声明;声明copyId时,变量name未定义。

此外,您正在使用一些已弃用的API(例如DocsList)。