Throw an error when the {{body}} insert point is missing

This commit is contained in:
Adam Goldsmith 2020-03-17 16:08:32 -04:00
parent 1dcb7f16fe
commit 5e1948421b

16
Code.ts
View File

@ -56,21 +56,21 @@ function test() {
const insert_point = out_doc const insert_point = out_doc
.getBody() .getBody()
.findText('{{test}}') .findText('{{body}}')
?.getElement(); ?.getElement();
if (insert_point) { if (!insert_point) {
throw new Error("Could not find insert point '{{body}}'");
}
// find the parent element that is a direct descendant of the body // find the parent element that is a direct descendant of the body
let parent = insert_point; let parent = insert_point;
while ( while (parent.getParent().getType() != DocumentApp.ElementType.BODY_SECTION) {
parent.getParent().getType() != DocumentApp.ElementType.BODY_SECTION
) {
parent = parent.getParent(); parent = parent.getParent();
} }
const idx = out_doc.getBody().getChildIndex(parent); const index = out_doc.getBody().getChildIndex(parent);
// insert with index 0 is an append, for some reason // insert with index 0 is an append, for some reason
copyBody(source_doc.getBody(), out_doc.getBody(), idx + 1); copyBody(source_doc.getBody(), out_doc.getBody(), index + 1);
out_doc.getBody().removeChild(parent); out_doc.getBody().removeChild(parent);
} }
}