无法将多个Blob对象作为附件附加到MailApp.sendEmail中 [英] Can't attach multiple Blob objects as attachments in MailApp.sendEmail
问题描述
我正在创建一个可转换文档并发送电子邮件的google-app-script.
我创建了两个功能,一个用于将Google Doc转换为Microsoft Word,另一个用于将Google表格转换为Microsoft Excel.这两个函数均成功返回Blob对象.
我已经能够使用记录在案的MailApp.sendEmail函数发送单个Blob.但是,当我尝试在同一封电子邮件中发送两个Blob时,我一直遇到无效的参数错误.
我的脚本是:
//Convert Google Sheet page to Microsoft Excel Document, return BLOB
function convert2Excel(docID) {
var file = DriveApp.getFileById(docID);
var url =
"https://docs.google.com/spreadsheets/d/"+docID+"/export?format=xlsx";
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var fileName = file.getName() + '.xlsx';
return [response.getBlob().setName(fileName)];
};
//Convert Google Doc page to Microsoft Word Document, return BLOB for emailing
function convert2Word(docID){
var file = DriveApp.getFileById(docID);
var url = "https://docs.google.com/document/d/"+docID+"/export?format=docx";
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var fileName = file.getName() + '.docx';
return [response.getBlob().setName(fileName)];
};
//Send email with attachments
function sendEmail(att1,att2){
MailApp.sendEmail('email@email.com', 'The Subject', 'A Message', {attachments: [att1, att2]});
};
var att1=convert2Excel('sheetID string');
var att2=convert2Word('docID string');
sendEmail(att1,att2);
根据文档,这应该可以工作,但是我无法使多个Blob对象正常工作.
您的代码无法使用2个附件的原因是,您要将数组数组传递给.sendMail
()的attachments
参数>
有两种方法可以使代码正常工作.行:return [response.getBlob().setName(fileName)];
通过在方括号内返回blob,实际上是在返回包含blob的数组.您可以在调试器中看到以下内容:
从return [response.getBlob().setName(fileName)];
行中删除方括号,以返回实际的blob.在调试器中:
或者,您可以保留方括号,然后在MailApp.sendEmail('email@email.com', 'The Subject', 'A Message', {attachments: [att1, att2]});
};
行上将[0]
添加到att1
和att2
,如下所示:MailApp.sendEmail('email@email.com', 'The Subject', 'A Message', {attachments: [att1[0], att2[0]]});
};
I'm creating an google-app-script that converts documents and sends an email.
I have created two functions, one for converting a Google Doc to Microsoft Word and one for converting Google Sheets to Microsoft Excel. Both of these functions return a Blob object successfully.
I have been able to use the documented MailApp.sendEmail function to send an individual blob. But I keep running into an invalid argument error when I try to send two blobs in the same email.
My script is:
//Convert Google Sheet page to Microsoft Excel Document, return BLOB
function convert2Excel(docID) {
var file = DriveApp.getFileById(docID);
var url =
"https://docs.google.com/spreadsheets/d/"+docID+"/export?format=xlsx";
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var fileName = file.getName() + '.xlsx';
return [response.getBlob().setName(fileName)];
};
//Convert Google Doc page to Microsoft Word Document, return BLOB for emailing
function convert2Word(docID){
var file = DriveApp.getFileById(docID);
var url = "https://docs.google.com/document/d/"+docID+"/export?format=docx";
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var fileName = file.getName() + '.docx';
return [response.getBlob().setName(fileName)];
};
//Send email with attachments
function sendEmail(att1,att2){
MailApp.sendEmail('email@email.com', 'The Subject', 'A Message', {attachments: [att1, att2]});
};
var att1=convert2Excel('sheetID string');
var att2=convert2Word('docID string');
sendEmail(att1,att2);
According to the documentation, this should work but I can't get multiple blob objects to work.
The reason your code is not working with 2 attachments is that you are passing an array of arrays to the attachments
argument of .sendMail
()
There are 2 ways to get your code working. The line: return [response.getBlob().setName(fileName)];
By returning the blob inside the square bracket you are actually returning an array containing a blob. You can see this in the debugger:
Remove the square brackets from the line return [response.getBlob().setName(fileName)];
to return the actual blob instead. In the debugger:
Alternatively you could leave the square brackets and add [0]
to att1
and att2
on the line MailApp.sendEmail('email@email.com', 'The Subject', 'A Message', {attachments: [att1, att2]});
};
Like this : MailApp.sendEmail('email@email.com', 'The Subject', 'A Message', {attachments: [att1[0], att2[0]]});
};
这篇关于无法将多个Blob对象作为附件附加到MailApp.sendEmail中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!