获取为图像生成错误的图像-Google App脚本 [英] Get as Image generates wrong image - Google App Script

查看:93
本文介绍了获取为图像生成错误的图像-Google App脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用带有Google App脚本的Google工作表中的信息来创建Google文档.

My goal is to create google documents using information from a google sheet with google app script.

我可以根据Google表格中的数据生成雷达图.雷达图如下所示:

I can generate a radar chart from the data in the google sheets. The radar chart looks like this:

雷达图看上去很好

问题:当我以图表的形式获取图表并将其粘贴到google文档中时.看起来很不对劲.参见图片和代码.

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.

雷达图看起来不太好

我的代码:

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.

多亏了Tanaike,它现在可以工作了.对于任何想知道的人,这是我用来生成图表并将其插入到Google文档中的脚本.用图表替换文本.

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);

推荐答案

此答案如何?

我认为这可能是一个错误.在这种情况下,作为一种解决方法,我使用了Google幻灯片,该幻灯片可以直接放置使用Google Spreadsheet创建的图表.当从放入Google幻灯片的图表中检索到Blob时,该Blob与Google Spreadsheet上的chrat相同.

I think that this might be a bug. In this case, as a workaround, I use a Google Slides which can directly put the chart created with Google Spreadsheet. When the blob is retrieved from the chart put to the Google Slides, the blob is the same with the chrat on Google Spreadsheet.

修改脚本后,请进行以下修改.

When your script is modified, please modify as follows.

var image = sheet.getCharts()[0].getBlob().getAs('image/png');

到:

const slides = SlidesApp.create("temp");
const imageBlob = slides.getSlides()[0].insertSheetsChartAsImage(chart).getAs("image/png");
DriveApp.getFileById(slides.getId()).setTrashed(true);
DriveApp.createFile(imageBlob);

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆