Using .getas to save a chart blob as an image gives Service Error in Google Script

柔情痞子 提交于 2021-01-29 13:05:11

问题


After creating a chart in google sheets I attempt to save that chart as and image. I am using .getAs('image/png'). I get service error: spreadsheet on this line. I have tried a variety of syntax from similar issues online and none are working.

function CheckboxLogic() {

 var ws = SpreadsheetApp.getActiveSpreadsheet(); //Main worksheet 
 ws.insertSheet('QuoteID'); //Create a new sheet using QuoteID for the name
 var ts = ws.getSheetByName('QuoteID'); //Target sheet


  //Create a chart from data copied to target sheet
  var chart = ts.newChart()
  .asTableChart()
  .addRange(ts.getRange('C1:F5'))
  .setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
  .setTransposeRowsAndColumns(false)
  .setNumHeaders(-1)
  .setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
  .setOption('bubble.stroke', '#000000')
  .setPosition(9, 5, 13, 8)
  .build();
  ts.insertChart(chart);

  //save chart as image
  var mychart = ts.getCharts()[0].getAs('image/png');
 //This throws the Service Error  
}

Here is the execution transcript

[19-08-07 13:42:14:205 EDT] Sheet.insertChart([EmbeddedChart]) [0.345 seconds]
[19-08-07 13:42:14:353 EDT] Sheet.getCharts() [0.147 seconds]
[19-08-07 13:42:14:416 EDT] EmbeddedChart.getAs([image/png]) [0.063 seconds]
[19-08-07 13:42:14:427 EDT] Execution failed: Service error: Spreadsheets (line 22, file "Code") [0.974 seconds total runtime]

回答1:


It seems like there is currently a bug for the getAs() method for table charts.

I suggest you to file it on https://issuetracker.google.com/.

In the mean time, consider using other chart types where getAs() works as intended.




回答2:


I ran into this same issue and entered a bug over a year ago. It was working for months and suddenly stopped working for table charts only.

I was adding these charts to emails and my work around was to create an HTML table with the data. In my case the workaround was sufficient. Here is the bug:

https://issuetracker.google.com/issues/79159343



来源:https://stackoverflow.com/questions/57385584/using-getas-to-save-a-chart-blob-as-an-image-gives-service-error-in-google-scri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!