[英]Geo Charts in Emails | Service Spreadsheets failed while accessing document with id | Google Sheets
I have a Google Sheets' spreadsheet with the following data:我有一个包含以下数据的 Google 表格电子表格:
I have used the data above to create a geo chart (I did this by following the steps: "Insert chart" -> select one option from "maps" in dropdown).我已经使用上面的数据创建了一个地理图表(我按照以下步骤执行此操作:“插入图表”-> select 下拉菜单中的“地图”中的一个选项)。 The geochart I created is shown below:
我创建的地理图表如下所示:
I want to get a daily email that contains a png version of this map.我想要一份包含此 map 的 png 版本的每日 email。 I have used the following script in google app scripts to do so:
我在 google app 脚本中使用了以下脚本:
function myFunction() {
// get references to the sheet and the charts
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
const charts = sheet.getCharts();
// set up some variables for our email
const chartBlobs = new Array();
const emailImages = {};
var titles = ["<h1>Geographical Distribution</h1><br>"]
let emailBody = "<br>";
// iterate for each loaded chart
var counter = 0
charts.forEach(function(chart,i){ // this is line 14
chartBlobs[i] = chart.getAs("image/png"); // this is line 15
emailBody += titles[i]+"<p align='center'><img src='cid:chart"+i+"'></p>"; // Aligning the chart to the center of the body in the email
emailImages["chart"+i] = chartBlobs[i];
counter++;
});
// send email
MailApp.sendEmail({
to: "some_email@gmail.com",
subject:"trial",
htmlBody: emailBody,
inlineImages: emailImages
})
}
However, when I run the script, I get the following error:但是,当我运行脚本时,出现以下错误:
Note that I commented, in the script, where lines 14 and 15 are.请注意,我在脚本中注释了第 14 行和第 15 行所在的位置。 I have covered the id of the document I used for privacy reasons
出于隐私原因,我已经涵盖了我使用的文档的 ID
This error only appears when I use a Geo Chart.此错误仅在我使用地理图表时出现。 This error does not appear when I use any other chart.
当我使用任何其他图表时,不会出现此错误。
What would you recommend I do to fix this error?你会建议我做什么来解决这个错误?
When I saw your error message, I thought that when Google Slides is used, your issue might be able to be removed.当我看到你的错误信息时,我认为当使用谷歌幻灯片时,你的问题可能会被删除。 So, how about the following modification?
那么,下面的修改呢?
function myFunction() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
const charts = sheet.getCharts();
const chartBlobs = new Array();
const emailImages = {};
var titles = ["<h1>Geographical Distribution</h1><br>"]
let emailBody = "<br>";
var counter = 0
const temp = SlidesApp.create("temp"); // Added
const temps = temp.getSlides()[0]; // Added
charts.forEach(function (chart, i) {
chartBlobs[i] = temps.insertSheetsChartAsImage(chart).getBlob(); // Modified
emailBody += titles[i] + "<p align='center'><img src='cid:chart" + i + "'></p>";
emailImages["chart" + i] = chartBlobs[i];
counter++;
});
MailApp.sendEmail({
to: "some_email@gmail.com",
subject: "trial",
htmlBody: emailBody,
inlineImages: emailImages
});
DriveApp.getFileById(temp.getId()).setTrashed(true); // Added
}
From Daniel's comment来自丹尼尔的评论
To elaborate a bit more, using Slides as intermediary is a workaround.
更详细地说,使用幻灯片作为中介是一种解决方法。 The cause of the error seems to be a bug that affects some chart types.
错误的原因似乎是影响某些图表类型的错误。 It can be found in Google's issue tracker .
它可以在 Google 的问题跟踪器中找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.