Google Apps脚本:如何在脚注数字之间向前和向后跳转?



我正在寻找一个移动光标的脚本,一步一步(单击"下一个按钮"),到文本正文中的每个脚注上标?非常感谢您的支持。谢谢。

在谷歌应用程序脚本可以访问脚注上标编程的位置的答案?不完全符合这个问题

下面是完成此工作的脚本的最后一个变体:

function onOpen() {
DocumentApp.getUi()
.createMenu('Footnotes')
.addItem('Next', 'jump_to_next_footnote')
.addItem('Previous', 'jump_to_prev_footnote')
.addToUi();
}


function jump_to_next_footnote() {

var doc = DocumentApp.getActiveDocument();
var footnotes = doc.getFootnotes();

var cursor = doc.getCursor(); 

// get index of paragraph where the cursor is placed
try { var cursor_container = cursor.getElement().asParagraph() }
catch(e) { var cursor_container = cursor.getElement().getParent().asParagraph() } 
var cursor_pgf = doc.getBody().getChildIndex(cursor_container);

// get offset for the cursor inside its paragraph
var cursor_offset = cursor.getSurroundingTextOffset();

var n = 0; // footnote num

// function to get index of paragraph of footnote with given number
const ftn_pgf = n => doc.getBody().getChildIndex(footnotes[n].getParent());

// function to get offset inside its paragraph fo foonote with given number
const ftn_offset = n => doc.newPosition(footnotes[n].getParent().asParagraph(), 1).getSurroundingTextOffset();

while (cursor_pgf > ftn_pgf(n)) n++;

if (n >= footnotes.length) return;

if (cursor_pgf == ftn_pgf(n)) while (n < footnotes.length && cursor_offset > ftn_offset(n) && cursor_pgf == ftn_pgf(n)) n++;

if (n >= footnotes.length) return;

var position = doc.newPosition(footnotes[n].getParent().asParagraph().getChild(0), ftn_offset(n));

doc.setCursor(position);
}

function jump_to_prev_footnote() {

var doc = DocumentApp.getActiveDocument();
var footnotes = doc.getFootnotes();

var cursor = doc.getCursor(); 

try { var cursor_container = cursor.getElement().asParagraph() }
catch(e) { var cursor_container = cursor.getElement().getParent().asParagraph() } 
var cursor_pgf = doc.getBody().getChildIndex(cursor_container);

var cursor_offset = cursor.getSurroundingTextOffset();

var n = footnotes.length-1;

const ftn_pgf = n => doc.getBody().getChildIndex(footnotes[n].getParent());
const ftn_offset = n => doc.newPosition(footnotes[n].getParent().asParagraph(), 1).getSurroundingTextOffset();

while (cursor_pgf < ftn_pgf(n)) n--;

if (n < 0) return;

if (cursor_pgf == ftn_pgf(n)) while (n >= 0 && cursor_offset < ftn_offset(n) && cursor_pgf == ftn_pgf(n)) n--;

if (n < 0) return;

var position = doc.newPosition(footnotes[n].getParent().asParagraph().getChild(0), ftn_offset(n));

doc.setCursor(position);
}

它创建自定义菜单Footnotes和两个命令:NextPrevious。您可以在文档中的脚注之间来回跳转。奇怪的是,谷歌文档还没有这样的选项。

相关内容

  • 没有找到相关文章

最新更新