Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Mustaches from "google-docs-mustaches";
import googleDrive from "../../google_drive.app.mjs";
import { parseObjectEntries } from "../../common/utils.mjs";

const MODE_GOOGLE_DOC = "Google Doc";
const MODE_PDF = "Pdf";
Expand All @@ -8,7 +9,7 @@ export default {
key: "google_drive-create-file-from-template",
name: "Create New File From Template",
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)",
version: "0.1.16",
version: "0.1.17",
annotations: {
destructiveHint: true,
openWorldHint: true,
Expand All @@ -17,10 +18,20 @@ export default {
type: "action",
props: {
googleDrive,
drive: {
propDefinition: [
googleDrive,
"watchedDrive",
],
optional: true,
},
templateId: {
propDefinition: [
googleDrive,
"fileId",
(c) => ({
drive: c.drive,
}),
],
description:
"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}}`.",
Expand All @@ -29,6 +40,9 @@ export default {
propDefinition: [
googleDrive,
"folderId",
(c) => ({
drive: c.drive,
}),
],
description:
"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.",
Expand Down Expand Up @@ -65,19 +79,38 @@ export default {
mode: this.mode,
};

const isSharedDrive = this.drive && this.drive !== "My Drive";

const client = new Mustaches.default({
token: () => this.googleDrive.$auth.oauth_access_token,
});

// COPY THE TEMPLATE

const drive = this.googleDrive.drive();
const copiedTemplate = await drive.files.copy({
fileId: this.templateId,
requestBody: {
name: "template-copy",
parents: [
"root",
],
},
supportsAllDrives: true,
});
const templateId = copiedTemplate.data.id;

/* CREATE THE GOOGLE DOC */

let googleDocId;
try {
googleDocId = await client.interpolate({
source: this.templateId,
destination: this.folderId,
source: templateId,
destination: !isSharedDrive
? this.folderId
: undefined,
name: this.name,
data: this.replaceValues,
data: parseObjectEntries(this.replaceValues),
});
} catch (e) {
const {
Expand All @@ -93,22 +126,56 @@ export default {

/* CREATE THE PDF */

let pdfId;
if (this.mode.includes(MODE_PDF)) {
const pdfId = await client.export({
pdfId = await client.export({
file: googleDocId,
mimeType: "application/pdf",
name: this.name,
destination: this.folderId,
destination: !isSharedDrive
? this.folderId
: undefined,
});
result["pdfId"] = pdfId;
}

// MOVE FILE(S) TO SHARED DRIVE

if (isSharedDrive) {
if (this.mode.includes(MODE_GOOGLE_DOC)) {
const file = await this.googleDrive.getFile(googleDocId);
await this.googleDrive.updateFile(googleDocId, {
fields: "*",
removeParents: file.parents.join(","),
addParents: this.folderId || this.drive,
supportsAllDrives: true,
});
}

if (pdfId) {
const pdf = await this.googleDrive.getFile(pdfId);
await this.googleDrive.updateFile(pdfId, {
fields: "*",
removeParents: pdf.parents.join(","),
addParents: this.folderId || this.drive,
supportsAllDrives: true,
});
}
}

/* REMOVE THE GOOGLE DOC */

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

// REMOVE THE COPIED TEMPLATE

await drive.files.delete({
fileId: templateId,
supportsAllDrives: true,
});

$.export("$summary", "New file successfully created");
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion components/google_drive/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/google_drive",
"version": "1.3.0",
"version": "1.3.1",
"description": "Pipedream Google_drive Components",
"main": "google_drive.app.mjs",
"keywords": [
Expand Down
Loading