我把我的电子邮件保存为PDF格式。它工作得很好。由于出现错误,它在过去几天内停止工作
异常:从text/html到application/pdf的转换失败
这里的某个人也报道了:
It just stopped working - google-app-script google-drive-api createFile
function saveGmailAsPDF() {
var gmailLabels = "TESTING";
var driveFolder = "TEST2";
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>";
}
/* Convert the Email Thread into a PDF File */
//The PDF craeted is damaged by the below code
//var blob = Utilities.newBlob(html, "application/pdf").setName("mail" + ".pdf");
//var theFolderId = '{FOLDER ID}';
//var parentFolder = DriveApp.getFolderById(theFolderId);
//folder.createFile(blob);
// PDF is damaged by the below code
//var html = "<h1>Hello world</h1>";
//var blob = Utilities.newBlob(html, "application/pdf", "text.pdf");
//DriveApp.createFile(blob);
//the below code was working well.
var tempFile = DriveApp.createFile("temp.html", html, "text/html");
//the bellow line is giving the error for the last few days
//Error : Exception: Conversion from text/html to application/pdf failed
folder.createFile(tempFile.getAs("application/pdf")).setName(subject + ".pdf");
tempFile.setTrashed(true);
}
}
}发布于 2021-08-12 12:43:35
这似乎是一个持续的bug。似乎已经向Google Issue Tracker报告了。转到the issue并单击左上角的白色星号(☆) (就在问题ID的左侧)以提高其优先级。
https://stackoverflow.com/questions/68755288
复制相似问题