【问题标题】:Can Google App Scripts access the location of footnote superscripts programmatically?Google Apps 脚本能否以编程方式访问脚注上标的位置?
【发布时间】:2016-03-06 05:33:47
【问题描述】:

是否可以使用 DocumentApp 来查找正文中脚注引用的位置?

使用editAsText()findText() 搜索正文或元素不会显示上标脚注标记。

例如,在以下文档中:

这是一个带有统计数据的引人入胜的故事!1您也可以在这里看到其他内容。

body.getText() 返回“这是一个带有统计数据的引人入胜的故事!你也可以在这里看到其他东西。没有参考,没有1

如果我想替换、编辑或操作脚注引用周围的文本(例如 1 ),如何找到它的位置?

【问题讨论】:

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


    【解决方案1】:

    事实证明,脚注引用在 Doc 中被索引为子项。因此,您可以获取脚注引用的索引,在该索引处插入一些文本,然后从其父级中删除脚注。

    function performConversion (docu) {
    
      var footnotes = docu.getFootnotes() // get the footnote
    
      var noteText = footnotes.map(function (note) {
        return '((' + note.getFootnoteContents() + ' ))' // reformat text with parens and save in array
      })
    
      footnotes.forEach(function (note, index) {
        var paragraph = note.getParent() // get the paragraph
    
        var noteIndex = paragraph.getChildIndex(note) // get the footnote's "child index"
    
        paragraph.insertText(noteIndex, noteText[index]) // insert formatted text before footnote child index in paragraph
    
        note.removeFromParent() // delete the original footnote
      })
    }
    

    【讨论】:

    • 好!如果是您的问题的答案,则将此您的的答案标记为正确,它可以帮助其他用户找到答案。
    【解决方案2】:

    您可以使用getFootnotes() 编辑脚注。 getFootnotes() 返回一个对象数组,你需要遍历它们。

    您可以按以下方式列出Logger.log() 中脚注的位置(即父段落):

        function getFootnotes(){
          var doc = DocumentApp.openById('...');
          var footnotes = doc.getFootnotes();
          var textLocation = {};
    
      for(var i in footnotes ){
          textLocation = footnotes[i].getParent().getText();
          Logger.log(textLocation);    
    
      }    
    }
    

    将段落截断到脚注上标。您可以使用:

    textLocation = footnotes[i].getPreviousSibling().getText();

    在你的情况下它应该返回:这是一个有统计数据的引人入胜的故事!只有这一部分,因为[1]就在statistics这个词之后!

    【讨论】:

    • 谢谢老兄。萨扬。我认为我的问题令人困惑,因为我不知道如何在 Markdown 中格式化上标。我已经修改了我的问题以更好地反映我的预期含义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多