Change copyElement to a switch/case

This commit is contained in:
Adam Goldsmith 2020-03-18 15:18:45 -04:00
parent 594a0b41d7
commit df130c5d03

20
Code.ts
View File

@ -16,25 +16,31 @@ function copyElement(
index?: number index?: number
) { ) {
const element = source_element.copy(); const element = source_element.copy();
// based on https://stackoverflow.com/questions/6783819/google-app-script-copy-document-page switch (element.getType()) {
if (element.getType() == DocumentApp.ElementType.PARAGRAPH) case DocumentApp.ElementType.PARAGRAPH:
if (index !== undefined) 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) break;
case DocumentApp.ElementType.TABLE:
if (index !== undefined) 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) break;
case DocumentApp.ElementType.LIST_ITEM:
if (index !== undefined) 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) break;
case DocumentApp.ElementType.INLINE_IMAGE:
if (index !== undefined) dest.insertImage(index, element.asInlineImage()); if (index !== undefined) dest.insertImage(index, element.asInlineImage());
else dest.appendImage(element.asInlineImage()); else dest.appendImage(element.asInlineImage());
else break;
default:
throw new Error( throw new Error(
"According to the doc this type couldn't appear in the body: " + "According to the doc this type couldn't appear in the body: " +
element.getType() element.getType()
); );
} }
}
function copySection(source: DocSection, dest: DocSection, index?: number) { function copySection(source: DocSection, dest: DocSection, index?: number) {
const totalElements = source.getNumChildren(); const totalElements = source.getNumChildren();