是否可以使用 DocumentApp 在正文中查找脚注引用的位置?
editAsText()
使用或findText()
不显示上标脚注标记搜索正文或元素。
例如,在以下文档中:
这是一个带有统计数据的引人入胜的故事!1你也可以在这里看到其他的东西。
body.getText()
返回 '这是一个带有统计数据的引人入胜的故事!你也可以在这里看到其他东西。没有参考,没有1
如果我想替换、编辑或操作脚注引用周围的文本(例如1),我如何找到它的位置?
是否可以使用 DocumentApp 在正文中查找脚注引用的位置?
editAsText()
使用或findText()
不显示上标脚注标记搜索正文或元素。
例如,在以下文档中:
这是一个带有统计数据的引人入胜的故事!1你也可以在这里看到其他的东西。
body.getText()
返回 '这是一个带有统计数据的引人入胜的故事!你也可以在这里看到其他东西。没有参考,没有1
如果我想替换、编辑或操作脚注引用周围的文本(例如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
})
}
您可以使用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]只是在单词统计之后!