如何使用Google App脚本获取正确的组合图图像? [英] How to obtain a correct image of a ComboChart with Google App Script?
问题描述
我有一个SpreadSheet模板,其中包含一个ComboChart,一个系列类型列,另一种类型行.
I have a SpreadSheet template that contains a ComboChart inside, one series type column, another type line.
当我尝试使用Google Apps脚本导出图表时,导出的图像没有显示列(显示为一行).
When I try to export the chart with Google Apps Script, the image exported doesn't show the columns (appears as a line).
基本上,我打开工作表,特别是更改范围内的值,更新图表,然后将图表捕获为图像.
Basically, I open the sheet, change the values in a range in particular, update the chart and then capture the chart as image.
我的Google Apps脚本功能是这样的:
My Google Apps Script function is this:
var hardcodedData = {
data: [['Muy Bajo', 0.1, 20],
['Bajo', 0.2, 32],
['Medio Bajo', 0.15, 51],
['Medio', 0.05, 42],
['Medio Alto', 0.07, 30],
['Alto', 0.23, 15],
['Muy Alto', 0.2, 11]],
fileId : 'id-of-spreadsheet-template'
}
function chartBuilder() {
var hdd = hardcodedData.data;
var sp = SpreadsheetApp.openById(hardcodedData.fileId);
var sh = sp.getSheets()[0];
sh.getRange('C2:D8').clearContent();
var clasific = [];
var ndvi = [];
var has = [];
for (var i = 0; i < hdd.length; i++) {
ndvi .push([hdd[i][1]]);
has .push([hdd[i][2]]);
}
sh.getRange(2, 3, 7, 1).setValues(ndvi);
sh.getRange(2, 4, 7, 1).setValues(has);
SpreadsheetApp.flush();
var chart = sh.getCharts()[0];
sh.updateChart(chart);
SpreadsheetApp.flush();
chart = sh.getCharts()[0];
var myimage = chart.getAs('image/gif');
//fill document------------------------------------------
var doc = DocumentApp.openById('id-of-document')
doc.getBody().clear();
var body = doc.getBody();
body.appendParagraph("");
body.insertImage(0, myimage)
}
是否可以像打开电子表格模板一样正确地打开和导出图表图像?
Is there any way to open and export the chart image correctly, the way it's in the spreadsheet template?
很抱歉,背景不佳,我没有发布图片的声誉.
Sorry for the poor context, I don't have the reputation to post images.
推荐答案
我遇到了类似的问题,似乎Chart.getAs(contentType)方法忽略了为该样式设置的某些样式和选项.EmbeddedChart对象.
I've been having a similar problem, it appears as though the Chart.getAs(contentType) method ignores some of the styling and options that are set for the EmbeddedChart objects.
这不是一个完美的解决方案,但是我已经通过Slides Apps Script服务找到了一种解决方法,尽管它需要设置空白的代理演示文稿"以通过图表.
It's not a perfect solution, but I've found a workaround, through the Slides Apps Script service, although it requires having a blank 'proxy presentation' set up to pass charts through.
Slide.insertSheetsChartAsImage(chart)会给您图表正确显示为幻灯片演示文稿中的图像,然后您可以从那里使用图像.
Slide.insertSheetsChartAsImage(chart) will give you the chart correctly displayed as an image within a slides presentation, and you can then use the image from there.
function chartBuilder() {
// ...
var chart = sh.getCharts()[0];
sh.updateChart(chart);
SpreadsheetApp.flush();
chart = sh.getCharts()[0];
// Insert sheets chart into slide as an embedded image
var proxySaveSlide = SlidesApp.openById("my-blank-sheets-presentation").getSlides()[0];
var chartImage = proxySaveSlide.insertSheetsChartAsImage(chart);
// Get image from slides
var myimage = chartImage.getAs('image/png');
//fill document------------------------------------------
var doc = DocumentApp.openById('id-of-document');
doc.getBody().clear();
var body = doc.getBody();
body.appendParagraph("");
body.insertImage(0, myimage);
// Delete image in presentation slide
chartImage.remove();
}
希望这会有所帮助!
这篇关于如何使用Google App脚本获取正确的组合图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!