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