使用Google Apps脚本在Google Doc中自动生成雷达图 [英] Automatting Radar Chart Generation in Google Doc using Google Apps Script

查看:124
本文介绍了使用Google Apps脚本在Google Doc中自动生成雷达图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Google Apps脚本 Google文档中插入雷达图。该图表位于 Google表格中,但在Google文档中却很难看。

I am trying to insert a radar chart in Google Docs using Google Apps Script. The chart is in Google Sheets but it turned out to be very ugly in Google Docs.

我在SO上找到了一篇帖子,建议直接使用Apps Script创建图表,但这对我也不起作用。

I found a post on SO that suggested to create the chart using Apps Script directly but that does not really worked for me either.

如果有人可以指向进一步的 setOptions(),我可以使用它来达到预期的效果,那就太好了。

If any one can point to further setOptions() that I can use to achieve the desired result that would be great.

这是我要在Google文档中实现的目标:

这是我的代码:

function createChart() {
var sheet = SpreadsheetApp.openById("SPREADSHEET_ID").getSheetByName("Sheet1");

var chart = sheet.newChart();

chart
.addRange(sheet.getRange("K2:K7"))
.addRange(sheet.getRange("G2:G7"))
.addRange(sheet.getRange("I2:J7"))
.setChartType(Charts.ChartType.RADAR)
.setNumHeaders(1)
.setOption("useFirstColumnAsDomain", true)
.setOption("colors",["#e0296c", "#0000ff", "#ff9900"])
.setOption('width', 700)
.setOption('height', 600)
.setOption('legend', {position: 'bottom', textStyle: {fontSize: 12}})
.setPosition(1,1,0,0)
.setOption('vAxis.gridlines.count', 4)
.setOption('series', {  0: { lineWidth: 1, pointSize: 5},
                        1: { lineDashStyle: [4, 4] },
                        2: { lineDashStyle: [4, 4] }
                        })
.setOption('hAxis.format',{format: 'percent'})

chart = chart.build();

sheet.insertChart(chart);

var doc = DocumentApp.openById("DOC_ID");
var body = doc.getBody();

var chartBlob = chart.getAs('image/png').copyBlob();
body.appendImage(chartBlob);

}

这是我到目前为止所取得的成就:


在GDOCS中

in GDOCS

推荐答案

使用内置的Google文档功能链接Google的图表床单。详细步骤在官方G Suite文档编辑器文档中,但核心步骤是:

Use the built-in Google Docs feature link a chart from Google Sheets. The detailed steps are on the official G Suite Document Editors documentation, but the core step is this:


点击插入 >,然后依次图表来自表格

上述原因是因为您已经发现,将图表转换为PNG的图像看起来并不相同。另一方面,电子表格服务包括类嵌入式图表

The above because resulting image of converting a chart to PNG, as you already found, doesn't look the same. By the other hand, the Spreadsheet Service include Class Embedded Chart but the Document Service don't.

您可以尝试使用 EmdeddedObject 使用新的文档高级服务

You could try to use EmdeddedObject from the Docs API by using the new Documents Advanced Service.

Ref

将图表,表格或幻灯片链接到Google文档或幻灯片

这篇关于使用Google Apps脚本在Google Doc中自动生成雷达图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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