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

28
Code.ts
View File

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