[英]How to append table after specific text in google docs using google apps script?
I want to append table to google doc after specific text.我想在特定文本之后将表格附加到谷歌文档。 Here is my attempt.
这是我的尝试。
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
body.appendTable(data);
Here data is some multidimensional array.这里的数据是一些多维数组。 This works fine.
这工作正常。 But table gets appended at the end of body.
但是 table 被附加到 body 的末尾。 I want to append table after '#PLACEHOLDER#'.
我想在“#PLACEHOLDER#”之后附加表格。
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
range.appendTable(data);
This doesn't work.这不起作用。 How to get position of range (text: #PLACEHOLDER#) and append table after it?
如何获取范围的位置(文本:#PLACEHOLDER#)并在其后附加表格?
#PLACEHOLDER#
in Google Document.#PLACEHOLDER#
的文本后插入一个表格。
#PLACEHOLDER#
is put to a paragraph, and the paragraph includes only the text of #PLACEHOLDER#
.#PLACEHOLDER#
放在一个段落中,并且该段落只包含#PLACEHOLDER#
的文本。 If my understanding is correct, how about this sample script?如果我的理解是正确的,这个示例脚本怎么样?
When there is only one text of #PLACEHOLDER#
in the Google Document, how about the following modification?当Google Document中
#PLACEHOLDER#
只有一个文本时,下面的修改如何?
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
// I added below script.
var ele = range.getElement();
if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BODY_SECTION) {
var offset = body.getChildIndex(ele.getParent());
body.insertTable(offset + 1, data);
}
When there is multiple texts of #PLACEHOLDER#
in the Google Document, how about the following modification?当Google文档中
#PLACEHOLDER#
有多个文本时,下面的修改如何?
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
// I added below script.
while (range) {
var ele = range.getElement();
if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BODY_SECTION) {
var offset = body.getChildIndex(ele.getParent());
body.insertTable(offset + 1, data);
}
range = body.findText("#PLACEHOLDER#", range);
}
data
is table
or 2 dimensional array.data
是table
或二维数组。 If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。 At that time, can you provide a sample Document you want to use?
那个时候能不能提供一个你要使用的示例文档? By this, I would like to confirm the issue.
通过这个,我想确认这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.