From df130c5d038a69ae3e6d186eb93aba5f17f6b26c Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 18 Mar 2020 15:18:45 -0400 Subject: [PATCH] Change copyElement to a switch/case --- Code.ts | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/Code.ts b/Code.ts index 3f7aa29..ec5c893 100644 --- a/Code.ts +++ b/Code.ts @@ -16,24 +16,30 @@ function copyElement( index?: number ) { const element = source_element.copy(); - // based on https://stackoverflow.com/questions/6783819/google-app-script-copy-document-page - if (element.getType() == DocumentApp.ElementType.PARAGRAPH) - if (index !== undefined) dest.insertParagraph(index, element.asParagraph()); - else dest.appendParagraph(element.asParagraph()); - else if (element.getType() == DocumentApp.ElementType.TABLE) - if (index !== undefined) dest.insertTable(index, element.asTable()); - else dest.appendTable(element.asTable()); - else if (element.getType() == DocumentApp.ElementType.LIST_ITEM) - if (index !== undefined) dest.insertListItem(index, element.asListItem()); - else dest.appendListItem(element.asListItem()); - else if (element.getType() == DocumentApp.ElementType.INLINE_IMAGE) - if (index !== undefined) dest.insertImage(index, element.asInlineImage()); - else dest.appendImage(element.asInlineImage()); - else - throw new Error( - "According to the doc this type couldn't appear in the body: " + - element.getType() - ); + switch (element.getType()) { + case DocumentApp.ElementType.PARAGRAPH: + if (index !== undefined) + dest.insertParagraph(index, element.asParagraph()); + else dest.appendParagraph(element.asParagraph()); + break; + case DocumentApp.ElementType.TABLE: + if (index !== undefined) dest.insertTable(index, element.asTable()); + else dest.appendTable(element.asTable()); + break; + case DocumentApp.ElementType.LIST_ITEM: + if (index !== undefined) dest.insertListItem(index, element.asListItem()); + else dest.appendListItem(element.asListItem()); + break; + case DocumentApp.ElementType.INLINE_IMAGE: + if (index !== undefined) dest.insertImage(index, element.asInlineImage()); + else dest.appendImage(element.asInlineImage()); + break; + default: + throw new Error( + "According to the doc this type couldn't appear in the body: " + + element.getType() + ); + } } function copySection(source: DocSection, dest: DocSection, index?: number) {