From 5e1948421bbab34e444074380985df4e9ff520b3 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 17 Mar 2020 16:08:32 -0400 Subject: [PATCH] Throw an error when the {{body}} insert point is missing --- Code.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) 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); }