From c8840a817a1643b45fb4c185ca3c0ef392a2f421 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 18 Mar 2020 14:33:06 -0400 Subject: [PATCH] Fix index 0 being treated as append due to incorrect conditional 0 is falsey, so `if (index) insert(index...) else append()` was appending, instead of inserting at index 0. Wow that was stupid. --- Code.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Code.ts b/Code.ts index 6d99526..6c9e29b 100644 --- a/Code.ts +++ b/Code.ts @@ -13,16 +13,16 @@ function copyElement( 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) dest.insertParagraph(index, element.asParagraph()); + if (index !== undefined) dest.insertParagraph(index, element.asParagraph()); else dest.appendParagraph(element.asParagraph()); else if (element.getType() == DocumentApp.ElementType.TABLE) - if (index) dest.insertTable(index, element.asTable()); + if (index !== undefined) dest.insertTable(index, element.asTable()); else dest.appendTable(element.asTable()); else if (element.getType() == DocumentApp.ElementType.LIST_ITEM) - if (index) dest.insertListItem(index, element.asListItem()); + if (index !== undefined) dest.insertListItem(index, element.asListItem()); else dest.appendListItem(element.asListItem()); else if (element.getType() == DocumentApp.ElementType.INLINE_IMAGE) - if (index) dest.insertImage(index, element.asInlineImage()); + if (index !== undefined) dest.insertImage(index, element.asInlineImage()); else dest.appendImage(element.asInlineImage()); else throw new Error( @@ -104,8 +104,7 @@ function generateForCurrentRow() { } 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); + copyBody(source_doc.getBody(), out_doc.getBody(), index); out_doc.getBody().removeChild(parent); out_doc.saveAndClose();