commit 1dcb7f16fe59dcf665f90e8680a11eaef9c7e8fa Author: Adam Goldsmith Date: Tue Mar 17 15:38:37 2020 -0400 Initial Commit, basic tranclusion working diff --git a/.clasp.json b/.clasp.json new file mode 100644 index 0000000..78095af --- /dev/null +++ b/.clasp.json @@ -0,0 +1 @@ +{"scriptId":"1KlR8cKW5jd9Fu-soBwK1dljmLi0XcHMIoYeUbnLXFZKMme-EaMqubaIk"} diff --git a/.claspignore b/.claspignore new file mode 100644 index 0000000..41db8e9 --- /dev/null +++ b/.claspignore @@ -0,0 +1 @@ +.#* \ No newline at end of file diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..81ded28 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +# EditorConfig is awesome: https://EditorConfig.org + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..936e5c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/package-lock.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..ee6d53c --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +trailingComma: es5 +singleQuote: true +jsxBracketSameLine: true diff --git a/Code.ts b/Code.ts new file mode 100644 index 0000000..e2e3c1d --- /dev/null +++ b/Code.ts @@ -0,0 +1,76 @@ +function onOpen() { + const ui = SpreadsheetApp.getUi(); + ui.createMenu('test') + .addItem('test1', 'test') + .addToUi(); +} + +function copyElement( + source_element: GoogleAppsScript.Document.Element, + dest: GoogleAppsScript.Document.Body, + 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) dest.insertParagraph(index, element.asParagraph()); + else dest.appendParagraph(element.asParagraph()); + else if (element.getType() == DocumentApp.ElementType.TABLE) + if (index) 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()); + else dest.appendListItem(element.asListItem()); + else if (element.getType() == DocumentApp.ElementType.INLINE_IMAGE) + if (index) 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() + ); +} + +function copyBody( + source: GoogleAppsScript.Document.Body, + dest: GoogleAppsScript.Document.Body, + index?: number +) { + var totalElements = source.getNumChildren(); + for (var j = 0; j < totalElements; ++j) { + if (index) copyElement(source.getChild(j), dest, index + j); + else copyElement(source.getChild(j), dest); + } +} + +function test() { + const cell = SpreadsheetApp.getActive().getCurrentCell(); + const template = DocumentApp.openById( + '1V0uMuM80BGpjpdt1AmuzlU97tDI_u-y2rOfdl4tkqmc' + ); + + const source_doc = DocumentApp.openById( + '1tB9--ilbfDixuQAEMk0_D6KqNFToQzs-au6NiotCH5M' + ); + const out_doc = template; // TODO: make a copy of template, and write to that + + const insert_point = out_doc + .getBody() + .findText('{{test}}') + ?.getElement(); + + if (insert_point) { + // find the parent element that is a direct descendant of the body + let parent = insert_point; + while ( + parent.getParent().getType() != DocumentApp.ElementType.BODY_SECTION + ) { + parent = parent.getParent(); + } + const idx = out_doc.getBody().getChildIndex(parent); + + // insert with index 0 is an append, for some reason + copyBody(source_doc.getBody(), out_doc.getBody(), idx + 1); + out_doc.getBody().removeChild(parent); + } +} diff --git a/appsscript.json b/appsscript.json new file mode 100644 index 0000000..3cf1d24 --- /dev/null +++ b/appsscript.json @@ -0,0 +1,7 @@ +{ + "timeZone": "America/New_York", + "dependencies": { + }, + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8" +} \ No newline at end of file