Skip to content

Commit 7ec32e1

Browse files
authored
[ACTION] Google Drive - Create document from template (#2436)
* Create document from template * Fix suggestions * Fix suggestions
1 parent 97efef9 commit 7ec32e1

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import googleDrive from "../../google_drive.app.mjs";
2+
import Mustaches from "google-docs-mustaches";
3+
4+
const MODE_GOOGLE_DOC = "Google Doc";
5+
const MODE_PDF = "Pdf";
6+
7+
export default {
8+
key: "google_drive-create-file-from-template",
9+
name: "Create New File From Template",
10+
description: "Create a new google doc file from template. [See documentation](https://www.npmjs.com/package/google-docs-mustaches)",
11+
version: "0.0.1",
12+
type: "action",
13+
props: {
14+
googleDrive,
15+
templateId: {
16+
propDefinition: [
17+
googleDrive,
18+
"fileId",
19+
],
20+
description:
21+
"Id of the file you want to use as a template.",
22+
},
23+
folderId: {
24+
propDefinition: [
25+
googleDrive,
26+
"folderId",
27+
],
28+
description:
29+
"Folder id of the newly created google doc and pdf.",
30+
},
31+
name: {
32+
propDefinition: [
33+
googleDrive,
34+
"fileName",
35+
],
36+
description:
37+
"Name of the file you want to create (eg. `myFile` will create a google doc called `myFile` and a pdf called `myFile.pdf`)",
38+
optional: false,
39+
},
40+
mode: {
41+
type: "string[]",
42+
label: "Mode",
43+
description: "Select if you want to create the google doc, the pdf or both files.",
44+
options: [
45+
MODE_GOOGLE_DOC,
46+
MODE_PDF,
47+
],
48+
},
49+
replaceValues: {
50+
type: "object",
51+
label: "Replace values",
52+
description: "Substrings to replace in the document. (eg. `{{ key }}` in the document will be replace by the value.",
53+
optional: true,
54+
},
55+
},
56+
async run({ $ }) {
57+
const result = {
58+
folderId: this.folderId,
59+
name: this.name,
60+
mode: this.mode,
61+
};
62+
63+
const client = new Mustaches.default({
64+
token: () => this.googleDrive.$auth.oauth_access_token
65+
});
66+
67+
/* CREATE THE GOOGLE DOC */
68+
69+
const googleDocId = await client.interpolate({
70+
source: this.templateId,
71+
destination: this.folderId,
72+
name: this.name,
73+
data: this.replaceValues,
74+
});
75+
result["googleDocId"] = googleDocId;
76+
77+
/* CREATE THE PDF */
78+
79+
if (this.mode.includes(MODE_PDF)) {
80+
const pdfId = await client.export({
81+
file: googleDocId,
82+
mimeType: 'application/pdf',
83+
name: this.name,
84+
destination: this.folderId,
85+
});
86+
result["pdfId"] = pdfId;
87+
}
88+
89+
/* REMOVE THE GOOGLE DOC */
90+
91+
if (!this.mode.includes(MODE_GOOGLE_DOC)) {
92+
await this.googleDrive.deleteFile(googleDocId);
93+
}
94+
95+
$.export("$summary", "New file successfully created");
96+
return result;
97+
},
98+
};

0 commit comments

Comments
 (0)