diff --git a/Code.ts b/Code.ts index e2e3c1d..d39cded 100644 --- a/Code.ts +++ b/Code.ts @@ -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); }