Google Apps脚本:如何在脚注数字之间前后跳跃? [英] Google Apps Script: How to jump forward and backward between footnote numbers?
本文介绍了Google Apps脚本:如何在脚注数字之间前后跳跃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Can Google App Scripts access the location of footnote superscripts programmatically?中的答案与此问题不完全匹配。
推荐答案
以下是执行该工作的脚本的最终变体:
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
和其中的两个命令:Next
和Previous
。您可以在文档中的脚注之间来回跳转。奇怪的是,Google Docs现在还没有这样的选项。
这篇关于Google Apps脚本:如何在脚注数字之间前后跳跃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文