I'm looking for a script which move the cursor, step by step (clicking on a "next button"), to each footnote superscripts in the text body? Your support will be well appreciate. Thanks.
The answer in Can Google App Scripts access the location of footnote superscripts programmatically? doesn't match exactly this question.
Here is the final variant of the script that does the job:
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);
}
It creates custom menu Footnotes
and two commands within: Next
and Previous
. You can jump back and forth between footnotes in document. It's strange that Google Docs has no such option out of the box yet.