From b153373fed243d3c221e7f31443b834a98130df2 Mon Sep 17 00:00:00 2001 From: Lucas Bergeron Date: Tue, 15 Mar 2022 17:46:09 +0100 Subject: [PATCH 1/3] Create document from template --- .../create-file-from-template.mjs | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 components/google_drive/actions/create-file-from-template/create-file-from-template.mjs diff --git a/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs new file mode 100644 index 0000000000000..58fa4b591a1b7 --- /dev/null +++ b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs @@ -0,0 +1,109 @@ +import googleDrive from "../../google_drive.app.mjs"; +import Mustaches from "google-docs-mustaches"; + +const MODE_GOOGLE_DOC = 0; +const MODE_PDF = 1; +const MODE_GOOGLE_DOC_PDF = 2; + +export default { + key: "google_drive-create-file-from-template", + name: "Create New File From Template", + description: "Create a new google doc file from template", + version: "0.0.1", + type: "action", + props: { + googleDrive, + templateId: { + propDefinition: [ + googleDrive, + "fileId", + ], + description: + "Id of the file you want to use as a template.", + }, + folderId: { + propDefinition: [ + googleDrive, + "folderId", + ], + description: + "Folder id of the newly created google doc and pdf.", + }, + name: { + propDefinition: [ + googleDrive, + "fileName", + ], + description: + "Name of the file you want to create (eg. `myFile` will create a google doc called `myFile` and a pdf called `myFile.pdf`)", + }, + mode: { + type: "string", + label: "Mode", + description: "Select if you want to create the google doc, the pdf or both files.", + async options() { + return [ + { + label: "Google doc", + value: 0, + }, + { + label: "Pdf", + value: 1, + }, + { + label: "Google doc & Pdf", + value: 2, + }, + ]; + }, + }, + replaceValues: { + type: "object", + label: "Replace values", + description: "Substrings to replace in the document. (eg. `{{ key }}` in the document will be replace by the value.", + optional: true, + }, + }, + async run() { + const result = { + folderId: this.folderId, + name: this.name, + mode: this.mode, + }; + + const client = new Mustaches.default({ + token: () => this.googleDrive.$auth.oauth_access_token + }); + + /* CREATE THE GOOGLE DOC */ + + var googleDocId = await client.interpolate({ + source: this.templateId, + destination: this.folderId, + name: this.name, + data: this.replaceValues, + }); + result["googleDocId"] = googleDocId; + + /* CREATE THE PDF */ + + if(this.mode != MODE_GOOGLE_DOC) { + var pdfId = await client.export({ + file: googleDocId, + mimeType: 'application/pdf', + name: this.name, + destination: this.folderId, + }); + result["pdfId"] = pdfId; + } + + /* REMOVE THE GOOGLE DOC */ + + if(this.mode == MODE_PDF) { + await this.googleDrive.deleteFile(googleDocId); + } + + return result; + }, +}; From 6c7ca2a0b87abbc5552fdaf7feb4df44c2e4a663 Mon Sep 17 00:00:00 2001 From: Lucas Bergeron Date: Tue, 15 Mar 2022 21:00:23 +0100 Subject: [PATCH 2/3] Fix suggestions --- .../create-file-from-template.mjs | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs index 58fa4b591a1b7..c52695545cb46 100644 --- a/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs +++ b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs @@ -1,14 +1,14 @@ import googleDrive from "../../google_drive.app.mjs"; import Mustaches from "google-docs-mustaches"; -const MODE_GOOGLE_DOC = 0; -const MODE_PDF = 1; -const MODE_GOOGLE_DOC_PDF = 2; +const MODE_GOOGLE_DOC = "Google Doc"; +const MODE_PDF = "Pdf"; +const MODE_GOOGLE_DOC_PDF = "Google Doc & Pdf"; export default { key: "google_drive-create-file-from-template", name: "Create New File From Template", - description: "Create a new google doc file from template", + description: "Create a new google doc file from template. [See documentation](https://www.npmjs.com/package/google-docs-mustaches)", version: "0.0.1", type: "action", props: { @@ -43,18 +43,9 @@ export default { description: "Select if you want to create the google doc, the pdf or both files.", async options() { return [ - { - label: "Google doc", - value: 0, - }, - { - label: "Pdf", - value: 1, - }, - { - label: "Google doc & Pdf", - value: 2, - }, + MODE_GOOGLE_DOC, + MODE_PDF, + MODE_GOOGLE_DOC_PDF, ]; }, }, @@ -65,11 +56,11 @@ export default { optional: true, }, }, - async run() { + async run({ $ }) { const result = { folderId: this.folderId, name: this.name, - mode: this.mode, + mode: this.mode, }; const client = new Mustaches.default({ @@ -78,7 +69,7 @@ export default { /* CREATE THE GOOGLE DOC */ - var googleDocId = await client.interpolate({ + const googleDocId = await client.interpolate({ source: this.templateId, destination: this.folderId, name: this.name, @@ -88,8 +79,8 @@ export default { /* CREATE THE PDF */ - if(this.mode != MODE_GOOGLE_DOC) { - var pdfId = await client.export({ + if (this.mode != MODE_GOOGLE_DOC) { + const pdfId = await client.export({ file: googleDocId, mimeType: 'application/pdf', name: this.name, @@ -100,10 +91,11 @@ export default { /* REMOVE THE GOOGLE DOC */ - if(this.mode == MODE_PDF) { + if (this.mode == MODE_PDF) { await this.googleDrive.deleteFile(googleDocId); } + $.export("$summary", "New file successfully created"); return result; }, }; From 13f34f7b9df9c40449b3df6c1bf4a3e1915d3b93 Mon Sep 17 00:00:00 2001 From: Lucas Bergeron Date: Wed, 16 Mar 2022 00:17:23 +0100 Subject: [PATCH 3/3] Fix suggestions --- .../create-file-from-template.mjs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs index c52695545cb46..a52220b9f200b 100644 --- a/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs +++ b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs @@ -3,7 +3,6 @@ import Mustaches from "google-docs-mustaches"; const MODE_GOOGLE_DOC = "Google Doc"; const MODE_PDF = "Pdf"; -const MODE_GOOGLE_DOC_PDF = "Google Doc & Pdf"; export default { key: "google_drive-create-file-from-template", @@ -36,18 +35,16 @@ export default { ], description: "Name of the file you want to create (eg. `myFile` will create a google doc called `myFile` and a pdf called `myFile.pdf`)", - }, + optional: false, + }, mode: { - type: "string", + type: "string[]", label: "Mode", description: "Select if you want to create the google doc, the pdf or both files.", - async options() { - return [ - MODE_GOOGLE_DOC, - MODE_PDF, - MODE_GOOGLE_DOC_PDF, - ]; - }, + options: [ + MODE_GOOGLE_DOC, + MODE_PDF, + ], }, replaceValues: { type: "object", @@ -79,7 +76,7 @@ export default { /* CREATE THE PDF */ - if (this.mode != MODE_GOOGLE_DOC) { + if (this.mode.includes(MODE_PDF)) { const pdfId = await client.export({ file: googleDocId, mimeType: 'application/pdf', @@ -91,7 +88,7 @@ export default { /* REMOVE THE GOOGLE DOC */ - if (this.mode == MODE_PDF) { + if (!this.mode.includes(MODE_GOOGLE_DOC)) { await this.googleDrive.deleteFile(googleDocId); }