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

Google Apps Script: How to jump forward and backward between footnote numbers?

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

中的答案与这个问题不完全匹配。

这是执行此任务的脚本的最终变体:

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。您可以在文档中的脚注之间来回跳转。奇怪的是 Google Docs 还没有开箱即用的选项。