我正在尝试通过电子邮件一起发送图像和电子表格图表,但我只能发送其中一个。例如,我只能发送图像,图表将不会出现,如果我发送图表,图像将不会出现。我也是这个新手,谢谢您的帮助。
{
function emailCharts(sheet,emails,emailSubject){
DriveApp.getRootFolder()
var targetspreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Active spreadsheet of the key file
var sheet = targetspreadsheet.getSheetByName('Test'); // Change the sheet name Eg.'IPQC Overall Tracker' to your sheet name
var emailSubject = 'Scratches Awareness Program Test';
var emails = 'example@gmail.com'; // your email ID
var charts = sheet.getCharts();
if(charts.length==0){
MailApp.sendEmail({
to: emails,
subject: "ERROR:"+emailSubject,
htmlBody: "No charts in the spreadsheet"});
return;
}
var emailStarting = "<br>@@@@@@@@TEST@@@@@@@@@@<br>"
var emailEnding = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<br>"
var emailSignature = "<br>Best Regards, <br>@@@@@@@@@@@@<br><br> This is an automated generated email. No signature is required."
var chartBlobs=new Array(charts.length);
var emailBody="Hi Everyone,<br>" + emailStarting;
var emailImages={};
for(var i=0;i<charts.length;i++){
var builder = charts[i].modify();
builder.setOption('vAxis.format', '#');
var newchart = builder.build();
chartBlobs[i]= newchart.getAs('image/png');
emailBody= emailBody + "<p align='center'><img src='cid:chart"+i+"'></p>" ;
emailImages["chart"+i]= chartBlobs[i];
}
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
var requestData = {"method": "GET",
"headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
var googleLogoUrl = "https://i.imgur.com/vO6IJVG.png";
var youtubeLogoUrl =
"https://i.imgur.com/xMqvjHf.jpg";
var googleLogoBlob = UrlFetchApp
.fetch(googleLogoUrl)
.getBlob()
.setName("Scratches Alert");
var youtubeLogoBlob = UrlFetchApp
.fetch(youtubeLogoUrl)
.getBlob()
.setName("Scratches Dashboard");
MailApp.sendEmail({
to: emails,
subject: emailSubject,
htmlBody: emailBody + emailEnding + emailSignature,
inlineImages:emailImages, attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}],googleLogo: googleLogoBlob,youtubeLogo: youtubeLogoBlob});
}
}
答案 0 :(得分:2)
googleLogoBlob
和youtubeLogoBlob
作为内嵌图像,并要发送Excel文件(xlsx格式)作为附件文件。我可以这样理解。如果我的理解是正确的,那么该修改如何?请认为这只是几个答案之一。
format=xlsx
用于将Google Spreadsheet转换为Excel。
{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}
用作附件文件。{fileName:sheetName+".xlsx", content:contents, mimeType: MimeType.MICROSOFT_EXCEL}
。googleLogo: googleLogoBlob,youtubeLogo: youtubeLogoBlob
直接用作MailApp.sendEmail(object)
的对象。并且googleLogo
和youtubeLogo
不包括在内嵌图像中。
当以上几点反映到您的脚本中时,它如下所示。
请按如下所示修改脚本。
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
var googleLogoUrl = "https://i.imgur.com/vO6IJVG.png";
var youtubeLogoUrl =
"https://i.imgur.com/xMqvjHf.jpg";
var googleLogoBlob = UrlFetchApp
.fetch(googleLogoUrl)
.getBlob()
.setName("Scratches Alert");
var youtubeLogoBlob = UrlFetchApp
.fetch(youtubeLogoUrl)
.getBlob()
.setName("Scratches Dashboard");
MailApp.sendEmail({
to: emails,
subject: emailSubject,
htmlBody: emailBody + emailEnding + emailSignature,
inlineImages:emailImages, attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}],googleLogo: googleLogoBlob,youtubeLogo: youtubeLogoBlob});
}
}
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx"; // Modified
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
var googleLogoUrl = "https://i.imgur.com/vO6IJVG.png";
var youtubeLogoUrl = "https://i.imgur.com/xMqvjHf.jpg";
var googleLogoBlob = UrlFetchApp.fetch(googleLogoUrl).getBlob().setName("Scratches Alert");
var youtubeLogoBlob = UrlFetchApp.fetch(youtubeLogoUrl).getBlob().setName("Scratches Dashboard");
emailBody += "<img src='cid:googleLogo'><img src='cid:youtubeLogo'>"; // Added
emailImages.googleLogo = googleLogoBlob; // Added
emailImages.youtubeLogo = youtubeLogoBlob; // Added
MailApp.sendEmail({ // Modified
to: emails,
subject: emailSubject,
htmlBody: emailBody + emailEnding + emailSignature,
inlineImages: emailImages,
attachments: [{fileName: sheetName+".xlsx", content: contents, mimeType: MimeType.MICROSOFT_EXCEL}],
});
}
googleLogo
和youtubeLogo
,请根据您的实际情况修改<img src='cid:googleLogo'><img src='cid:youtubeLogo'>
。答案 1 :(得分:1)
您的代码很好,您遇到的唯一问题是构建MailApp.sendEmail()
的方式。这样做:
var emails = "example@gmail.com";
var emailSubject = "Test Subject";
var body = emailBody + emailEnding + emailSignature;
var spreadsheetObj = {
fileName:sheetName+".xls",
content:contents,
mimeType:"application//xls"
};
MailApp.sendEmail({
to: emails,
subject: emailSubject,
htmlBody: body,
attachments: [ spreadsheetObj, googleLogoBlob, youtubeLogoBlob]
});
附件 必须是一个数组,您以错误的方式构建它。正如Docs所说:
附件BlobSource []与电子邮件一起发送的文件数组(请参见示例)
如果您附加了多个文件,则电子邮件可能需要一些时间才能送达。