【问题标题】:how to read, with google apps script, the heading reference in a google docs如何使用谷歌应用程序脚本阅读谷歌文档中的标题参考
【发布时间】:2013-09-11 18:28:40
【问题描述】:

问题:如何阅读 google docs doc 中的标题参考(格式为 #heading=h.12345)?

背景:希望在 doc 中使用交叉引用。示例。

1.1 Chapter 1 (i.e. paragraph has heading DocumentApp.ParagraphHeading.HEADING1)

Sample text. For more, see chapter 1.2.

1.2 Chapter 2

Sample text. For more, see chapter 1.1.

现在,谷歌文档可以进行交叉引用(插入链接),但属于“普通”链接,不带有章节号。

因此,方法是: - 插入交叉引用的链接

  • 使用应用程序脚本,构建标题引用和章节编号的索引

  • 还有应用程序脚本,更新参见基于其链接的章节文本

我查看了getLinkUrl没有成功:

var links = [];
var ps = DocumentApp.getActiveDocument().getBody().getParagraphs();
for(var i = 0; i < ps.length; i++) {
  var h = ps[i].getHeading();
  if( h == DocumentApp.ParagraphHeading.HEADING1 ) {
    var t = ps[i].editAsText();
    var u = t.getLinkUrl();
  }
}

是否可以读取标题参考?

【问题讨论】:

    标签: google-apps-script google-docs google-docs-api


    【解决方案1】:

    是否可以读取标题参考?

    当然,至少从目录来看。这些参考在目录条目的attributes 中。您可以在this answer 中查看带有脚本的示例。

    【讨论】:

      【解决方案2】:

      假设只有一个实例,这是您检测 HEADING1 的代码(稍作修改)。它可以适应检测其他标题类型和多次出现。

      function get_some_heading() {
        var ps = DocumentApp.getActiveDocument().getBody()
        var searchType = DocumentApp.ElementType.PARAGRAPH;
        var searchHeading = DocumentApp.ParagraphHeading.HEADING1;
        var searchResult = null;
      
        while (searchResult = ps.findElement(searchType, searchResult)) {
          var par = searchResult.getElement().asParagraph();
          if (par.getHeading() == searchHeading) {
            // Found one, update Logger.log and stop.
            var h = searchResult.getElement().asText().getText();
      
            return h;
          }
        }
      
        //return null or something
      }
      

      这里是枚举 Paragraph Heading 引用,这里是上面使用的 search pattern 引用(用于稍微不同的用例)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        相关资源
        最近更新 更多