Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用工作表中的图表替换文本占位符_Javascript_Google Apps Script_Google Sheets_Google Docs - Fatal编程技术网

Javascript 用工作表中的图表替换文本占位符

Javascript 用工作表中的图表替换文本占位符,javascript,google-apps-script,google-sheets,google-docs,Javascript,Google Apps Script,Google Sheets,Google Docs,我有一个脚本,它应该用谷歌表单中的各种值填充谷歌文档正文中的一系列占位符。我希望合并到模板中的一个对象是EmbeddedChartBuilder对象: var chart = sheet.newChart() .addRange(range) .setChartType(Charts.ChartType.BAR) .setPosition(i, 6, 0, 0)

我有一个脚本,它应该用谷歌表单中的各种值填充谷歌文档正文中的一系列占位符。我希望合并到模板中的一个对象是
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),只是将文档中的占位符替换为“EmbeddedChartBuilder”


是否有办法将图表作为图像或其他内容插入占位符?如果是,怎么做?

首先,在定义
图表
变量的链的末尾有
.build()
更符合逻辑。毕竟,您希望插入的是图表,而不是图表生成器

当然,尝试插入带有replaceText函数的图表会将其强制为“EmbeddedChart”字符串。相反,在适当的元素上使用或。我将使用前者,因为它更简单:它只是将图像附加到应用它的元素的末尾

var chart = sheet.newChart()........build();
var found = body.findText('{chart}');
if (found) {
  found.getElement().getParent().appendInlineImage(chart);
  body.replaceText('{chart}', '');
}
这里,
findText
检索指向{chart}的RangeElement;然后我们得到包含该字符串的元素(文本),然后是它的父元素(可能是段落,但也可能是ListItem,等等)。图像被附加到父对象,最后使用
replaceText
删除字符串{chart}


这假设字符串{chart}位于其元素的末尾(很可能位于其自身的段落中)

多谢各位。在问了这个问题之后,我意识到了很多这些事情,就在我即将回答这个问题的时候,但是你的措辞可能比我的要好!