电子邮件打印问题无限循环

时间:2017-08-11 00:17:10

标签: pdf google-apps-script gmail

尝试使此代码正常运行时遇到问题。看起来它在谷歌驱动器中生成的pdf陷入了循环。每次脚本运行时,它都会添加到pdf而不是创建新的pdf。我不是那么先进的代码,所以我似乎无法解决这个问题。我回到原始代码,它仍在发生。对我来说,似乎要么像缓存问题,要么临时html文件没有被删除。

function AUTOPRINTV3() { 

var gmailLabels  = "AUTOPRINTV3";  
var driveFolder  = "Incoming Orders 2";

var threads = GmailApp.search("in:" + gmailLabels, 0, 5);  

if (threads.length > 0) {

/* Google Drive folder where the Files would be saved */
var folders = DriveApp.getFoldersByName(driveFolder);
var folder = folders.hasNext() ? 
    folders.next() : DriveApp.createFolder(driveFolder);

/* Gmail Label that contains the queue */
var label = GmailApp.getUserLabelByName(gmailLabels) ?     
    GmailApp.getUserLabelByName(gmailLabels) : GmailApp.createLabel(driveFolder);

for (var t=0; t<threads.length; t++) {

  threads[t].removeLabel(label);
  var msgs = threads[t].getMessages();

  var html = "";
  var attachments = [];

  var subject = threads[t].getFirstMessageSubject();

  /* Append all the threads in a message in an HTML document */
  for (var m=0; m<msgs.length; m++) {

    var msg = msgs[m];

    html += "From: " + msg.getFrom() + "<br />";  
    html += "To: " + msg.getTo() + "<br />";
    html += "Date: " + msg.getDate() + "<br />";
    html += "Subject: " + msg.getSubject() + "<br />"; 
    html += "<hr />";
    html += msg.getBody().replace(/<img[^>]*>/g,"");
    html += "<hr />";

    var atts = msg.getAttachments();
    for (var a=0; a<atts.length; a++) {
      attachments.push(atts[a]);
    }
  }

  /* Save the attachment files and create links in the document's footer */
  if (attachments.length > 0) {
    var footer = "<strong>Attachments:</strong><ul>";
    for (var z=0; z<attachments.length; z++) {
      var file = folder.createFile(attachments[z]);
      footer += "<li><a href='" + file.getUrl() + "'>" + file.getName() + "</a></li>";
    }
    html += footer + "</ul>";
  }

  /* Conver the Email Thread into a PDF File */
  var tempFile = DriveApp.createFile("temp.html", html, "text/html");
  folder.createFile(tempFile.getAs("application/pdf")).setName(subject + ".pdf");
  tempFile.setTrashed(true);

     }
   }
}  

0 个答案:

没有答案