简体   繁体   中英

I'm trying to get a chart from my spreadsheet to send in an email using google script, but the getAs() function isn't working

Whenever I've looked into how to email a chart, all the answers involve some variation on using the getAs function to save the chart as an image and then either inlining it or attaching it to the email. But I'm struggling to save the chart as an image in order to attach it. When I run the following code:

var chart = demandLastWeekSheet.getCharts().getAs("image/png");

I get this error:

TypeError: demandLastWeekSheet.getCharts(...).getAs is not a function (line 8, file "Code")

In fact, I get the same error if I try.getBlob(), .modify() or any other functions from here: https://developers.google.com/apps-script/reference/spreadsheet/embedded-chart.html

Surely these functions have not been deprecated? Am I doing something wrong/stupid? New to the world of apps script, any pointers welcome, Sorry also if this is a badly phrased question: also I am new to stack overflow :)

Any suggestions??

getCharts() returns EmbeddedChart[] . In your script, the method of getAs is used for an array. I think that this is the reason of your issue. In order to retrieve the blob of the chart using the method of getAs , please modify as follows.

From:

var chart = demandLastWeekSheet.getCharts().getAs("image/png");

To:

var chart = demandLastWeekSheet.getCharts()[0].getAs("image/png");
  • In this modification, the 1st chart in the sheet of demandLastWeekSheet is retrieved. If you want to retrieve the 2nd chart, please modify from [0] to [1] .

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM