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();