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.
This commit is contained in:
Adam Goldsmith 2020-03-18 14:33:06 -04:00
parent ebf76d2653
commit c8840a817a

11
Code.ts
View File

@ -13,16 +13,16 @@ function copyElement(
const element = source_element.copy(); const element = source_element.copy();
// based on https://stackoverflow.com/questions/6783819/google-app-script-copy-document-page // based on https://stackoverflow.com/questions/6783819/google-app-script-copy-document-page
if (element.getType() == DocumentApp.ElementType.PARAGRAPH) 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 dest.appendParagraph(element.asParagraph());
else if (element.getType() == DocumentApp.ElementType.TABLE) 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 dest.appendTable(element.asTable());
else if (element.getType() == DocumentApp.ElementType.LIST_ITEM) 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 dest.appendListItem(element.asListItem());
else if (element.getType() == DocumentApp.ElementType.INLINE_IMAGE) 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 dest.appendImage(element.asInlineImage());
else else
throw new Error( throw new Error(
@ -104,8 +104,7 @@ function generateForCurrentRow() {
} }
const index = out_doc.getBody().getChildIndex(parent); 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);
copyBody(source_doc.getBody(), out_doc.getBody(), index + 1);
out_doc.getBody().removeChild(parent); out_doc.getBody().removeChild(parent);
out_doc.saveAndClose(); out_doc.saveAndClose();