简体   繁体   English

使用Google Apps脚本在Google文档中插入段落,列表项

[英]Insert paragraph, listitem in google docs using google apps script

I have a google doc with placeholders in it that need to be filled with actual data using Apps Script. 我有一个带有占位符的Google文档,需要使用Apps Script填充实际数据。 eg of the Google Doc content: 例如Google Doc内容:

This is template text. 这是模板文本。 Replace this {TOKEN1} and this {TOKEN2} and this {TOKEN3} as well using Google Apps Script. 并使用Google Apps脚本替换此{TOKEN1} ,此{TOKEN2}和此{TOKEN3}

The requirement is to replace the tokens and repeat this paragraph multiple times. 要求是替换令牌并多次重复此段。 Each time the paragraph is replaced, a different set of values needs to be placed in the paragraph. 每次替换该段落时,都需要在该段落中放置一组不同的值。 The formatting should not be broken, in this case, the tokens were in bold hence the values also should be in bold as below: 格式不应中断,在这种情况下,标记应以粗体显示,因此值也应以粗体显示,如下所示:

This is template text. 这是模板文本。 Replace this FOO1 and this BAR1 and this FUN1 as well using Google Apps Script. 使用Google Apps脚本替换此FOO1 ,此BAR1和此FUN1

This is template text. 这是模板文本。 Replace this FOO2 and this BAR2 and this FUN2 as well using Google Apps Script. 使用Google Apps脚本替换此FOO2 ,此BAR2和此FUN2

This is template text. 这是模板文本。 Replace this FOO3 and this BAR3 and this FUN3 as well using Google Apps Script. 使用Google Apps脚本替换此FOO3 ,此BAR3和此FUN3

Could you let me know the Apps script approach to achieve this functionality? 您能否让我知道实现此功能的Apps脚本方法?

I was able to achieve the requirement as below: 我能够达到以下要求:

  1. Iterate through all the elements in the document and process paragraphs: 遍历文档和流程段落中的所有元素:

     var elementCount = body.getNumChildren(); for(var i = 0; i < elementCount; i++) { var child = body.getChild(i); if(child.getType() === DocumentApp.ElementType.PARAGRAPH) { replaceTokensInParagraph(element.asParagraph(), i); } } 
  2. Collect all the tokens from the paragraph text 收集段落文本中的所有标记

  3. Remove the paragraph from the doc as we don't need the templated paragraph in the final document 从文档中删除该段落,因为我们在最终文档中不需要模板化的段落

  4. Make a detached copy of the templated paragraph 制作模板段落的分离副本

  5. Replace tokens in the detached copy 替换分离副本中的令牌

Overall, the function looks as below: 总体而言,该函数如下所示:

var replaceTokensInParagraph = function(paragraph, paragraphIndex) {
  paragraph.removeFromParent();
  var tokens = ....;
  for(int i=0; i<tokens.length; i++) {
     var paragraphCopy = paragraph.copy();
     paragraphCopy.replaceText(token, <value>);
     body.insertParagraph(paragraphIndex, paragraphCopy);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Google Apps Script 在 Google Docs 中的段落之前设置一定数量的空格或缩进 - How to set a certain number of spaces or indents before a Paragraph in Google Docs using Google Apps Script 正在寻找一种使用 Google Apps 脚本或 DOCS API 为 Google 文档中的段落添加边框的方法? - Looking for a way to add borders to a paragraph in a google doc using a Google Apps Script or perhaps the DOCS API? Google Docs / Apps Script:插入带有样式的文本 - Google Docs / Apps Script: Insert text with styles 使用 Apps 脚本以编程方式更改 Google 文档的默认段落标题 - Change default paragraph headings programmatically for Google Docs with Apps Script 使用Apps脚本写入Google Docs电子表格 - Writing to Google Docs Spreadsheets using Apps Script 使用 Apps 脚本将 Google 表格中的链接表插入 Google 文档 - Insert linked table from Google Sheets into Google Docs using Apps Script Apps 脚本 - Google 文档 - RangeBuilder - Apps Script - Google Docs - RangeBuilder 适用于 Google 文档的 Apps 脚本 findText() - Apps Script findText() for Google Docs 使用 Google Apps Script 将 HTML 注入 Google Docs - Inject HTML into Google Docs using Google Apps Script 在 google 应用程序脚本中使用 google docs api - using google docs api from within google apps script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM