Google apps script 如何使用GoogleApps脚本读取GoogleDocs中的标题引用

Google apps script 如何使用GoogleApps脚本读取GoogleDocs中的标题引用,google-apps-script,google-docs,google-docs-api,Google Apps Script,Google Docs,Google Docs Api,问:如何阅读谷歌文档中的标题参考(格式为#heading=h.12345) 背景:希望在文档中使用交叉引用。例如 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. 现在,谷歌文档可以做交叉引用

问:如何阅读谷歌文档中的标题参考(格式为#heading=h.12345)

背景:希望在文档中使用交叉引用。例如

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();
  }
}
var-links=[];
var ps=DocumentApp.getActiveDocument().getBody().getPages();
对于(变量i=0;i
可以阅读标题参考吗

可以阅读标题参考吗

绝对可以,至少从目录上看是这样。这些引用位于TOC条目的
属性中。您可以在中看到一个带有脚本的示例

这是您的代码(稍加修改),假设只有一个实例,它可以检测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
}
这是enum引用,这是上面使用的引用(用于稍微不同的用例)