My goal is to create google documents using information from a google sheet with google app script.
I can generate a radar chart from the data in the google sheets. The radar chart looks like this:
radar chart looking all good
The issue: when I get the chart as a image and paste it to a google document. It looks all wrong. See picture and code.
radar chart not so good looking
My code :
var chart = sheet.newChart()
.setChartType(Charts.ChartType.RADAR)
.setPosition(8,2,0,0)
.setNumHeaders(1)
.setOption('title', title)
.addRange(sheet.getRange("A1:G1"))
.addRange(sheet.getRange("A2:G2"))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS)
.setTransposeRowsAndColumns(true)
.setOption('vAxes',{0: {viewWindow: {min: 0, max: 100}}})
.setOption('series', {0: {lineWidth: 4, color: '#FFA500'}})
.build();
sheet.insertChart(chart);
var image = sheet.getCharts()[0].getBlob().getAs('image/png');
The most recent similar post I found was this one: Automatting Radar Chart Generation in Google Doc using Google Apps Script
But no answer was given there. Hope someone can help.
EDIT:
Thanks to Tanaike it's now working. For anyone wondering, this is the script I use to generate the chart and insert it into a google document. Replacing text with the chart.
var chart = sheet.newChart()
.setChartType(Charts.ChartType.RADAR)
.setPosition(8,2,0,0)
.setNumHeaders(1)
.setOption('title', title)
.addRange(sheet.getRange("A1:G1"))
.addRange(sheet.getRange("A2:G2"))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS)
.setTransposeRowsAndColumns(true)
.setOption('vAxes',{0: {viewWindow: {min: 0, max: 100}}})
.setOption('series', {0: {lineWidth: 4, color: '#FFA500'}})
.build();
sheet.insertChart(chart);
// workaround by Tanaike
const slides = SlidesApp.create("temp");
const image = slides.getSlides()[0].insertSheetsChartAsImage(chart).getAs("image/png");
DriveApp.getFileById(slides.getId()).setTrashed(true);
var item = doc.getBody().findText("<text-to-replace>");
var r = item.getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, image);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…