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

42
Code.ts
View File

@ -16,24 +16,30 @@ 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)
else dest.appendParagraph(element.asParagraph()); dest.insertParagraph(index, element.asParagraph());
else if (element.getType() == DocumentApp.ElementType.TABLE) else dest.appendParagraph(element.asParagraph());
if (index !== undefined) dest.insertTable(index, element.asTable()); break;
else dest.appendTable(element.asTable()); case DocumentApp.ElementType.TABLE:
else if (element.getType() == DocumentApp.ElementType.LIST_ITEM) if (index !== undefined) dest.insertTable(index, element.asTable());
if (index !== undefined) dest.insertListItem(index, element.asListItem()); else dest.appendTable(element.asTable());
else dest.appendListItem(element.asListItem()); break;
else if (element.getType() == DocumentApp.ElementType.INLINE_IMAGE) case DocumentApp.ElementType.LIST_ITEM:
if (index !== undefined) dest.insertImage(index, element.asInlineImage()); if (index !== undefined) dest.insertListItem(index, element.asListItem());
else dest.appendImage(element.asInlineImage()); else dest.appendListItem(element.asListItem());
else break;
throw new Error( case DocumentApp.ElementType.INLINE_IMAGE:
"According to the doc this type couldn't appear in the body: " + if (index !== undefined) dest.insertImage(index, element.asInlineImage());
element.getType() 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) { function copySection(source: DocSection, dest: DocSection, index?: number) {