简体   繁体   English

电子邮件中的地理图表 | 访问具有 ID 的文档时服务电子表格失败 | 谷歌表格

[英]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?那么,下面的修改呢?

Modified script:修改后的脚本:

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
}
  • When this script is run, the chart is inserted to Google Slides.运行此脚本时,图表会插入到 Google 幻灯片中。 And, the image blob is retrieved from the chart on Google Slides.并且,从 Google 幻灯片上的图表中检索图像 blob。

Note:笔记:

  • 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 的问题跟踪器中找到。

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 例外:访问带有 id google sheet 的文档时服务电子表格失败 - Exception: Service Spreadsheets failed while accessing document with id google sheet 服务电子表格在访问 ID 为 xxx 的文档时失败 - Service Spreadsheets failed while accessing document with ID xxx 异常:服务电子表格在访问具有 id 的文档时失败 - Exception: Service Spreadsheets failed while accessing document with id 例外:我认为访问带有 id *** / getsheetname() 错误的文档时服务电子表格失败 - Exception: Service Spreadsheets failed while accessing document with id *** / getsheetname()error I think Google Sheet 错误:访问带有 id 的文档时服务电子表格超时 - Google Sheet Error: Service Spreadsheets timed out while accessing document with id 异常:服务电子表格在访问 ID 为 XXX 的文档时超时 - Exception: Service Spreadsheets timed out while accessing document with id XXX 错误消息:访问 ID 为 1HjEecpeXGdR3iF_0W6CdLIm-hE-W9tbuxmvtKlte1OY 的文档时服务电子表格失败 - Error Msg: Service Spreadsheets failed while accessing document with id 1HjEecpeXGdR3iF_0W6CdLIm-hE-W9tbuxmvtKlte1OY 错误异常:访问具有 ID 的文档时服务电子表格失败 - Error Exception: Service Spreadsheet failed while accessing document with id “访问时服务电子表格失败”(看似)简单的代码 - “Service Spreadsheets failed while accessing” on (seemingly) simple code DocumentApp 服务抛出“服务文档在访问带有 id 的文档时失败”错误 - DocumentApp service throws a “Service Documents failed while accessing document with id” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM