Skip to content

Commit 627fe9c

Browse files
authored
Google Drive - Create File From Template - support templates in shared drives (#19053)
* support templates in shared drives * updates * save to shared drive * update
1 parent 9e1c971 commit 627fe9c

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

components/google_drive/actions/create-file-from-template/create-file-from-template.mjs

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Mustaches from "google-docs-mustaches";
22
import googleDrive from "../../google_drive.app.mjs";
3+
import { parseObjectEntries } from "../../common/utils.mjs";
34

45
const MODE_GOOGLE_DOC = "Google Doc";
56
const MODE_PDF = "Pdf";
@@ -8,7 +9,7 @@ export default {
89
key: "google_drive-create-file-from-template",
910
name: "Create New File From Template",
1011
description: "Create a new Google Docs file from a template. Optionally include placeholders in the template document that will get replaced from this action. [See documentation](https://www.npmjs.com/package/google-docs-mustaches)",
11-
version: "0.1.16",
12+
version: "0.1.17",
1213
annotations: {
1314
destructiveHint: true,
1415
openWorldHint: true,
@@ -17,10 +18,20 @@ export default {
1718
type: "action",
1819
props: {
1920
googleDrive,
21+
drive: {
22+
propDefinition: [
23+
googleDrive,
24+
"watchedDrive",
25+
],
26+
optional: true,
27+
},
2028
templateId: {
2129
propDefinition: [
2230
googleDrive,
2331
"fileId",
32+
(c) => ({
33+
drive: c.drive,
34+
}),
2435
],
2536
description:
2637
"Select the template document you'd like to use as the template, or use a custom expression to reference a document ID from a previous step. Template documents should contain placeholders in the format `{{xyz}}`.",
@@ -29,6 +40,9 @@ export default {
2940
propDefinition: [
3041
googleDrive,
3142
"folderId",
43+
(c) => ({
44+
drive: c.drive,
45+
}),
3246
],
3347
description:
3448
"Select the folder of the newly created Google Doc and/or PDF, or use a custom expression to reference a folder ID from a previous step.",
@@ -65,19 +79,38 @@ export default {
6579
mode: this.mode,
6680
};
6781

82+
const isSharedDrive = this.drive && this.drive !== "My Drive";
83+
6884
const client = new Mustaches.default({
6985
token: () => this.googleDrive.$auth.oauth_access_token,
7086
});
7187

88+
// COPY THE TEMPLATE
89+
90+
const drive = this.googleDrive.drive();
91+
const copiedTemplate = await drive.files.copy({
92+
fileId: this.templateId,
93+
requestBody: {
94+
name: "template-copy",
95+
parents: [
96+
"root",
97+
],
98+
},
99+
supportsAllDrives: true,
100+
});
101+
const templateId = copiedTemplate.data.id;
102+
72103
/* CREATE THE GOOGLE DOC */
73104

74105
let googleDocId;
75106
try {
76107
googleDocId = await client.interpolate({
77-
source: this.templateId,
78-
destination: this.folderId,
108+
source: templateId,
109+
destination: !isSharedDrive
110+
? this.folderId
111+
: undefined,
79112
name: this.name,
80-
data: this.replaceValues,
113+
data: parseObjectEntries(this.replaceValues),
81114
});
82115
} catch (e) {
83116
const {
@@ -93,22 +126,56 @@ export default {
93126

94127
/* CREATE THE PDF */
95128

129+
let pdfId;
96130
if (this.mode.includes(MODE_PDF)) {
97-
const pdfId = await client.export({
131+
pdfId = await client.export({
98132
file: googleDocId,
99133
mimeType: "application/pdf",
100134
name: this.name,
101-
destination: this.folderId,
135+
destination: !isSharedDrive
136+
? this.folderId
137+
: undefined,
102138
});
103139
result["pdfId"] = pdfId;
104140
}
105141

142+
// MOVE FILE(S) TO SHARED DRIVE
143+
144+
if (isSharedDrive) {
145+
if (this.mode.includes(MODE_GOOGLE_DOC)) {
146+
const file = await this.googleDrive.getFile(googleDocId);
147+
await this.googleDrive.updateFile(googleDocId, {
148+
fields: "*",
149+
removeParents: file.parents.join(","),
150+
addParents: this.folderId || this.drive,
151+
supportsAllDrives: true,
152+
});
153+
}
154+
155+
if (pdfId) {
156+
const pdf = await this.googleDrive.getFile(pdfId);
157+
await this.googleDrive.updateFile(pdfId, {
158+
fields: "*",
159+
removeParents: pdf.parents.join(","),
160+
addParents: this.folderId || this.drive,
161+
supportsAllDrives: true,
162+
});
163+
}
164+
}
165+
106166
/* REMOVE THE GOOGLE DOC */
107167

108168
if (!this.mode.includes(MODE_GOOGLE_DOC)) {
109169
await this.googleDrive.deleteFile(googleDocId);
110170
}
111171

172+
// REMOVE THE COPIED TEMPLATE
173+
174+
await drive.files.delete({
175+
fileId: templateId,
176+
supportsAllDrives: true,
177+
});
178+
112179
$.export("$summary", "New file successfully created");
113180
return result;
114181
},

components/google_drive/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/google_drive",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Pipedream Google_drive Components",
55
"main": "google_drive.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)