Create footnote
This snippet demonstrates the ability to create new footnotes by referencing the document body. Note, you will need to find the position index in the body before to insert a footnote.
- See full code
- Leave a comment
- Create script from the snippet *See how to use scrviz for clone Apps Script project
index.js
/**
*
*/
/**
* Runs the sample.
* Insert a footnote to specific position
*/
function run() {
const reply = createFootnoteWithText_('Aha!', 1);
console.log(JSON.stringify(reply, null, ' '));
}
/**
* Create a footnote with the text
*
* @param {string} text
* @param {number} bodyIndex
* @param {string} id
* @returns Result of batch updates or an issue reply
*/
function createFootnoteWithText_(
text,
bodyIndex,
id = DocumentApp.getActiveDocument().getId()
) {
const createFootnoteReplies = createFootnote_(bodyIndex, id);
if (createFootnoteReplies && createFootnoteReplies.replies.length) {
const reply = createFootnoteReplies.replies[0];
return appendTextTo_(text, reply.createFootnote.footnoteId, id);
}
return { issue: 'no replies', createFootnoteReplies };
}
/**
* Insert an empty footnote to body index
* @param {number} bodyIndex
*/
function createFootnote_(
bodyIndex,
id = DocumentApp.getActiveDocument().getId()
) {
return Docs.Documents.batchUpdate(
{
requests: {
createFootnote: {
location: {
index: bodyIndex,
},
},
},
},
id
);
}
/**
* Append a text to the segment
* @param {string} text
* @param {string} segmentId
*/
function appendTextTo_(
text,
segmentId = undefined,
id = DocumentApp.getActiveDocument().getId()
) {
return Docs.Documents.batchUpdate(
{
requests: {
insertText: {
text,
endOfSegmentLocation: {
segmentId,
},
},
},
},
id
);
}
{
"timeZone": "Europe/Moscow",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Docs",
"serviceId": "docs",
"version": "v1"
}]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}