在Google Apps脚本中的文本前插入换行符

地亚哥

我需要在Google文档中的某些文本之前插入一些换行符。

尝试过这种方法,但会出错:

var body = DocumentApp.getActiveDocument().getBody();
var pattern = "WORD 1";
var found = body.findText(pattern);
var parent = found.getElement().getParent();
var index = body.getChildIndex(parent);
// or parent.getChildIndex(parent);
body.insertParagraph(index, "");

有关如何执行此操作的任何想法?

感谢帮助!

Tanaike

例如,作为一个简单的修改,如何修改上一个问题中https://stackoverflow.com/a/65745933脚本因此,请将此视为几种方法之一。

在这种情况下,将使用InsertTextRequest代替InsertPageBreakRequest。

修改后的脚本:

请复制以下脚本并将其粘贴到Google文档的脚本编辑器中,然后设置searchPattern。并且,请在高级Google服务中启用Google Docs API

function myFunction() {
  const searchText = "WORD 1";  // Please set text. This script inserts the pagebreak before this text.
  
  // 1. Retrieve all contents from Google Document using the method of "documents.get" in Docs API.
  const docId = DocumentApp.getActiveDocument().getId();
  const res = Docs.Documents.get(docId);
  
  // 2. Create the request body for using the method of "documents.batchUpdate" in Docs API.
  let offset = 0;
  const requests = res.body.content.reduce((ar, e) => {
    if (e.paragraph) {
      e.paragraph.elements.forEach(f => {
        if (f.textRun) {
          const re = new RegExp(searchText, "g");
          let p = null;
          while (p = re.exec(f.textRun.content)) {
            ar.push({insertText: {location: {index: p.index + offset},text: "\n"}});
          }
        }
      })
    }
    offset = e.endIndex;
    return ar;
  }, []).reverse();
  
  // 3. Request the request body to the method of "documents.batchUpdate" in Docs API.
  Docs.Documents.batchUpdate({requests: requests}, docId);
}

结果:

使用上述脚本时,可获得以下结果。

从:

在此处输入图片说明

至:

在此处输入图片说明

注意:

  • 如果您不想像上一个问题那样直接使用高级Google服务,请按如下所示修改https://stackoverflow.com/a/65745933的第二个脚本

    •   ar.push({insertPageBreak: {location: {index: p.index + offset}}});
      
    •   ar.push({insertText: {location: {index: p.index + offset},text: "\n"}});
      

参考文献:

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Apps脚本日历说明-换行符

来自分类Dev

Google Apps脚本:模板化HTML scriptlet中的换行符

来自分类Dev

使用Google文档中的应用脚本删除换行符

来自分类Dev

使用Google表格中的输入来换行(Google Apps脚本)

来自分类Dev

为什么Google Apps脚本仅在脚本执行结束后才将分页符和数据插入表中?

来自分类Dev

使用Cat将换行符插入文本输出中

来自分类Dev

如何在插入的文本中包含换行符

来自分类Dev

在Google表格中添加(在字符串末尾删除换行符)

来自分类Dev

如何替换使用Google表格API下载的CSV文件中的换行符?

来自分类Dev

使用Google Apps脚本从Google云端硬盘或URL将图像插入Google表格中

来自分类Dev

通过Google Apps脚本中的https调用获取状态文本

来自分类Dev

Google Apps脚本->将文档中的文本写入表格

来自分类Dev

如何使用脚本在Google文档中插入文本并使其居中

来自分类Dev

从Google表单获取表单响应时,请在Google表格单元格中包含换行符

来自分类Dev

融合表格通过Google Apps脚本插入

来自分类Dev

使用Google Apps脚本将Google表格中的图表正确插入文档中

来自分类Dev

Google Apps 脚本中的 Google Drive 通知

来自分类Dev

Firefox在contenteditable中插入换行符

来自分类Dev

在DisplayName属性中插入换行符

来自分类Dev

在Java中插入换行符

来自分类Dev

在块元素中插入“换行符”

来自分类Dev

Google Apps脚本:如何在Google文档中查找listItem对象并向其中插入项目?

来自分类Dev

使用Google Apps脚本在Google电子表格中插入单元格

来自分类Dev

脚本标签中的换行符

来自分类Dev

Perl脚本中的换行符

来自分类Dev

接受 bash 脚本中的换行符

来自分类Dev

Google Apps脚本:从.txt文件读取文本

来自分类Dev

细长文本中的换行符

来自分类Dev

使用Google Apps脚本从Google文档中提取文本

Related 相关文章

  1. 1

    Google Apps脚本日历说明-换行符

  2. 2

    Google Apps脚本:模板化HTML scriptlet中的换行符

  3. 3

    使用Google文档中的应用脚本删除换行符

  4. 4

    使用Google表格中的输入来换行(Google Apps脚本)

  5. 5

    为什么Google Apps脚本仅在脚本执行结束后才将分页符和数据插入表中?

  6. 6

    使用Cat将换行符插入文本输出中

  7. 7

    如何在插入的文本中包含换行符

  8. 8

    在Google表格中添加(在字符串末尾删除换行符)

  9. 9

    如何替换使用Google表格API下载的CSV文件中的换行符?

  10. 10

    使用Google Apps脚本从Google云端硬盘或URL将图像插入Google表格中

  11. 11

    通过Google Apps脚本中的https调用获取状态文本

  12. 12

    Google Apps脚本->将文档中的文本写入表格

  13. 13

    如何使用脚本在Google文档中插入文本并使其居中

  14. 14

    从Google表单获取表单响应时,请在Google表格单元格中包含换行符

  15. 15

    融合表格通过Google Apps脚本插入

  16. 16

    使用Google Apps脚本将Google表格中的图表正确插入文档中

  17. 17

    Google Apps 脚本中的 Google Drive 通知

  18. 18

    Firefox在contenteditable中插入换行符

  19. 19

    在DisplayName属性中插入换行符

  20. 20

    在Java中插入换行符

  21. 21

    在块元素中插入“换行符”

  22. 22

    Google Apps脚本:如何在Google文档中查找listItem对象并向其中插入项目?

  23. 23

    使用Google Apps脚本在Google电子表格中插入单元格

  24. 24

    脚本标签中的换行符

  25. 25

    Perl脚本中的换行符

  26. 26

    接受 bash 脚本中的换行符

  27. 27

    Google Apps脚本:从.txt文件读取文本

  28. 28

    细长文本中的换行符

  29. 29

    使用Google Apps脚本从Google文档中提取文本

热门标签

归档