用 sheet 中的图表替换文本占位符
Replacing a text placeholder with a chart from a sheet
我有一个脚本应该用 Google Sheet 中的各种值填充 Google 文档正文中的一系列占位符。我希望合并到模板中的对象之一是 EmbeddedChartBuilder
对象:
var chart = sheet.newChart()
.addRange(range)
.setChartType(Charts.ChartType.BAR)
.setPosition(i, 6, 0, 0)
.setOption('legend.position', 'none')
.setOption('height', 50)
.setOption('width', 700)
.setOption('colors', ['black', 'white'])
.setOption('hAxis.minValue', 0)
.setOption('hAxis.maxValue', 10)
.setOption('backgroundColor.fill', 'white')
.setOption('hAxis.gridlines.color', 'white')
.setOption('hAxis.gridlines.count', 0);
sheet.insertChart(chart.build());
合并代码如下:
body.replaceText('{name}', company.name);
body.replaceText('{score}', company.score);
body.replaceText('{applyTime}', company.applyTime);
body.replaceText('{confEmail}', company.confEmail);
body.replaceText('{mobiFriendly}', company.mobiFriendly);
body.replaceText('{chart}', chart);
最后一行,body.replaceText('{chart}', chart);
,当然只是简单的将 Doc 中的占位符替换为 "EmbeddedChartBuilder"。
有没有办法将图表作为图像或其他内容插入占位符?如果是,怎么做?
首先,在定义 chart
变量的链的末尾使用 .build()
更符合逻辑。毕竟,您想要插入图表,而不是图表生成器。
当然,尝试使用 replaceText 函数插入图表会将其强制转换为 "EmbeddedChart" 字符串。相反,在适当的元素上使用 appendInlineImage
or insertInlineImage
。我将使用前者,因为它更简单:它只是将图像附加到应用它的元素的末尾。
var chart = sheet.newChart()........build();
var found = body.findText('{chart}');
if (found) {
found.getElement().getParent().appendInlineImage(chart);
body.replaceText('{chart}', '');
}
此处,findText
检索指向 {chart} 的 RangeElement;然后我们得到包含该字符串的元素(文本),然后是它的父元素(可能是段落,但也可能是 ListItem 等)。图像附加到 Parent,最后使用 replaceText
删除字符串 {chart}。
这假定字符串 {chart} 在其元素的末尾(很可能,它在自己的段落中)。
我有一个脚本应该用 Google Sheet 中的各种值填充 Google 文档正文中的一系列占位符。我希望合并到模板中的对象之一是 EmbeddedChartBuilder
对象:
var chart = sheet.newChart()
.addRange(range)
.setChartType(Charts.ChartType.BAR)
.setPosition(i, 6, 0, 0)
.setOption('legend.position', 'none')
.setOption('height', 50)
.setOption('width', 700)
.setOption('colors', ['black', 'white'])
.setOption('hAxis.minValue', 0)
.setOption('hAxis.maxValue', 10)
.setOption('backgroundColor.fill', 'white')
.setOption('hAxis.gridlines.color', 'white')
.setOption('hAxis.gridlines.count', 0);
sheet.insertChart(chart.build());
合并代码如下:
body.replaceText('{name}', company.name);
body.replaceText('{score}', company.score);
body.replaceText('{applyTime}', company.applyTime);
body.replaceText('{confEmail}', company.confEmail);
body.replaceText('{mobiFriendly}', company.mobiFriendly);
body.replaceText('{chart}', chart);
最后一行,body.replaceText('{chart}', chart);
,当然只是简单的将 Doc 中的占位符替换为 "EmbeddedChartBuilder"。
有没有办法将图表作为图像或其他内容插入占位符?如果是,怎么做?
首先,在定义 chart
变量的链的末尾使用 .build()
更符合逻辑。毕竟,您想要插入图表,而不是图表生成器。
当然,尝试使用 replaceText 函数插入图表会将其强制转换为 "EmbeddedChart" 字符串。相反,在适当的元素上使用 appendInlineImage
or insertInlineImage
。我将使用前者,因为它更简单:它只是将图像附加到应用它的元素的末尾。
var chart = sheet.newChart()........build();
var found = body.findText('{chart}');
if (found) {
found.getElement().getParent().appendInlineImage(chart);
body.replaceText('{chart}', '');
}
此处,findText
检索指向 {chart} 的 RangeElement;然后我们得到包含该字符串的元素(文本),然后是它的父元素(可能是段落,但也可能是 ListItem 等)。图像附加到 Parent,最后使用 replaceText
删除字符串 {chart}。
这假定字符串 {chart} 在其元素的末尾(很可能,它在自己的段落中)。