简体   繁体   English

如何使用谷歌应用程序脚本在谷歌文档中的特定文本后附加表格?

[英]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#)并在其后附加表格?

  • You want to insert a table after the text of #PLACEHOLDER# in Google Document.您想在 Google 文档中#PLACEHOLDER#的文本后插入一个表格。
    • From your question, I thought that #PLACEHOLDER# is put to a paragraph, and the paragraph includes only the text of #PLACEHOLDER# .从你的问题来看,我认为#PLACEHOLDER#放在一个段落中,并且该段落只包含#PLACEHOLDER#的文本。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本来实现这一点。

If my understanding is correct, how about this sample script?如果我的理解是正确的,这个示例脚本怎么样?

Pattern 1:模式一:

When there is only one text of #PLACEHOLDER# in the Google Document, how about the following modification?当Google Document中#PLACEHOLDER#只有一个文本时,下面的修改如何?

Modified script:修改后的脚本:

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

Pattern 2:模式2:

When there is multiple texts of #PLACEHOLDER# in the Google Document, how about the following modification?当Google文档中#PLACEHOLDER#有多个文本时,下面的修改如何?

Modified script:修改后的脚本:

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

Note:笔记:

  • These modified scripts suppose that data is table or 2 dimensional array.这些修改后的脚本假设datatable或二维数组。

References:参考:

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.

相关问题 如何使用谷歌应用程序脚本在谷歌文档中的特定文本之后从工作表插入表? - How to insertTable from sheet after specific text in google docs using google apps script? 如何使用Google Apps脚本删除Google文档的特定部分 - How to delete specific parts of a google docs using google Apps script Google Apps 脚本:如何在选择后在文档中附加表格 - Google Apps Script :How to append a table in Document after a selection 使用 Google Apps 脚本为 Google 文档中的表格设置“左缩进” - Set “Left Indent” for a table in Google Docs using Google Apps Script 是否可以使用Apps脚本在Google文档中添加归因于特定文本的注释? - Is it possible to add a comment attributed to specific text within Google Docs using Apps Script? 如何使用Google文档中的Google Apps脚本设置表格中某行的背景颜色? - How can I set the background color of a row in a table using google apps script in Google Docs? 是否可以使用Apps Script在Google文档中用建议替换文本? - Is there a way to replace text with a suggestion in Google Docs using Apps Script? 如何使用脚本在Google文档中设置文本格式 - How to format text in Google Docs using script Google Docs / Apps Script:插入带有样式的文本 - Google Docs / Apps Script: Insert text with styles 如何使用Google Apps脚本加粗特定文字? - How to bold specific text using Google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM