I’m working on a Google Apps Script to replace placeholders in a Google Docs template.
Unexpected error while getting the method or property getChildIndex on object DocumentApp.Body.
Here is the relevant code section:
const elementType = element.getType();
/** @type {GoogleAppsScript.Document.Text} */
let textElement;
if (elementType === DocumentApp.ElementType.PARAGRAPH ||
elementType === DocumentApp.ElementType.TABLE_CELL ||
elementType === DocumentApp.ElementType.LIST_ITEM ||
elementType === DocumentApp.ElementType.TEXT) {
textElement = element.editAsText();
}
if (textElement) {
const parent1 = textElement.getParent();
const parentType = parent1.getType();
try {
let index = parent1.getChildIndex(textElement);
//further code below, but crashing at getChildindex in the line aboveā¦
} catch (error) {
console.error(`Error processing placeholder: ${error.message}`);
throw error;
}
}
My Understanding:
parent.getType()
returnsBODY_SECTION
, I guess this means thatparent
should be of type Body? But the Body class supports a function getChildIndex?- More general the return type of getParent() on type Text should return the type ContainerElement that supports getChildIndex.
Any help would be appreciated!