英文:
Sending attachment in Word format does not work in Google Script
问题
我已经成功发送了 PDF 格式的附件,但是当我尝试发送 Word 格式的附件时,出现了以下错误:
"Request failed for https://docs.google.com returned code 404. Truncated server response: <html".
我尝试了这个网站上建议的几种方法,但都无法使其工作。
非常感谢您对这个主题的任何帮助。我是 Google Script 的新手。
这是我的代码:
//=================================================
// 以 Word 格式发送就业信函,而不是 PDF
//=================================================
function EmailCertAsWordDoc(requestor, name, newCopyID) {
var email = requestor;
var id = DocumentApp.openById(newCopyID);
var subject = "自动生成的就业信函";
var body = "\n\n附件是您为" + name + "创建的就业信函。\n\n\n\n\n ";
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + id + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
var docx = UrlFetchApp.fetch(url).getBlob();
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, {
Body: body,
attachments:[docx]
});
}
英文:
I've been able to send attachment in PDF format successfully but when I try to send attachment in Word format, I get this error:
"Request failed for https://docs.google.com returned code 404. Truncated server response: <!DOCTYPE html><html".
I've tried several ways suggested in this site but I just couldn't make it work.
Will very much appreciate any help on this topic. I'm a newbie in Google Script.
Here's my code:
//=================================================
// Email Employment Letter as Word format, not PDF
//=================================================
function EmailCertAsWordDoc(requestor, name, newCopyID) {
var email = requestor;
var id = DocumentApp.openById(newCopyID);
var subject = "Auto-generated Employment Letter";
var body = "\n\nAttached is the Employment Letter that you created for " + name + ". \n\n\n\n\n ";
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + id + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
var docx = UrlFetchApp.fetch(url).getBlob();
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, {
Body: body,
attachments:[docx]
});
}
答案1
得分: 1
以下是翻译好的部分:
-
要获取导出链接,您需要使用文件 ID,而不是文件本身 - 在您的情况下,它是
newCopyID
。 -
不幸的是,将 Word 文件作为附件发送比 PDF 复杂一些。为了使其工作,您可以在您的驱动器上创建一个 Word 文件,然后将其作为 URL 放在电子邮件正文中。
示例:
function EmailCertAsWordDoc(requestor, name, newCopyID) {
var email = requestor;
var fileName = DocumentApp.openById(newCopyID).getName();
var subject = "自动生成的雇佣信";
var body = "\n\n附上了您为 " + name + " 创建的雇佣信。\n\n\n\n\n ";
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + newCopyID + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
var docx = UrlFetchApp.fetch(url).getBlob();
var file = DriveApp.createFile(docx).setName(fileName).getUrl();
body += " " + file;
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body);
}
请注意,我只翻译了代码中的注释和字符串部分。
英文:
Two things:
-
To obtain the export url, you need to use the file id, no the file itself - in your case it is
newCopyID
-
Sending word files as attachments is unfortunately a little bit more complicated than pdfs. To make it work, you can create a word file on your drive, and then send it as an URL within your email body.
Sample:
function EmailCertAsWordDoc(requestor, name, newCopyID) {
var email = requestor;
var fileName = DocumentApp.openById(newCopyID).getName();
var subject = "Auto-generated Employment Letter";
var body = "\n\nAttached is the Employment Letter that you created for " + name + ". \n\n\n\n\n ";
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + newCopyID + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
var docx = UrlFetchApp.fetch(url).getBlob();
var file=DriveApp.createFile(docx).setName(fileName).getUrl();
body+=" "+file;
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body);
}
答案2
得分: 0
这是我在遵循ziganotschka的建议后的最终工作代码:
//=================================================
// 将就业信以Word格式发送,而不是PDF
//=================================================
function EmailCertAsWordDoc(email, name, newCopyID) {
var subject = "自动生成的就业信";
var body = "\n\n附上了您为" + name + "创建的就业信。\n\n\n\n\n";
var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' + newCopyID + '&exportFormat=docx';
var options = {
headers: {
Authorization: "Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(url, options);
var doc = response.getBlob();
// 在我的Google Drive的TEMP文件夹中创建docx文件并发送
var file = DriveApp.createFile(doc).setName('就业信 - '+ name + '.docx');
DriveApp.getFolderById('<在Google Drive中我的TEMP文件夹的ID>').addFile(file);
var blob = DriveApp.getFileById(file.getId());
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
Body: body,
attachments:[blob]
});
// 从TEMP文件夹中删除docx文件
DriveApp.getFileById(file.getId()).setTrashed(true);
}
英文:
Here's my final working code after following ziganotschka's advice:
//=================================================
// Email Employment Letter as Word format, not PDF
//=================================================
function EmailCertAsWordDoc(email, name, newCopyID) {
var subject = "Auto-generated Employment Letter";
var body = "\n\nAttached is the Employment Letter that you created for " + name + ". \n\n\n\n\n ";
var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' + newCopyID + '&exportFormat=docx';
var options = {
headers: {
Authorization: "Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(url, options);
var doc = response.getBlob();
//Create the docx file in my TEMP folder in Google Drive and send
var file = DriveApp.createFile(doc).setName('Employment Letter - '+ name + '.docx');
DriveApp.getFolderById('<ID of my TEMP folder in Google Drive>').addFile(file);
var blob = DriveApp.getFileById(file.getId());
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
Body: body,
attachments:[blob]
});
//Remove the docx file from the TEMP folder
DriveApp.getFileById(file.getId()).setTrashed(true);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论