我正在尝试发送附有xlsx
文件的电子邮件,但我遇到的问题是我下载时该文件以PDF格式打开
这是我的代码:
function sendMail() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(['test value']);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssID = ss.getId();
var file = DriveApp.getFileById(ssID);
var blob = file.getBlob();
blob.setName("test.xlsx");
GmailApp.sendEmail(
'xxxxx@gmail.com',
'Test',
'test.',
{
attachments: blob
}
);
}
如果我尝试直接从电子邮件中打开文件,则说:
Google Docs encountered an error. Please try reloading this page, or coming back to it in a few minutes.
To learn more about the Google Docs editors, please visit our help center.
答案 0 :(得分:1)
以下修改如何?
DriveApp.getFileById(ssID).getBlob()
将电子表格转换为blob时,电子表格会自动转换为PDF。
attachments: [blob]
。上面反映的修改后的脚本如下。
function sendMail() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(['test value']);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssID = ss.getId();
// Added
var url = "https://docs.google.com/spreadsheets/d/" + ssID + "/export?format=xlsx";
var params = {
method: "get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}
};
var blob = UrlFetchApp.fetch(url, params).getBlob().setName("test.xlsx");
// Added
GmailApp.sendEmail(
'xxxxx@gmail.com',
'Test',
'test.',
{
attachments: [blob] // Modified
}
);
}
如果我误解了你的问题,我很抱歉。