Can Pipedream Retrieve Parts of a Google Doc?

This topic was automatically generated from Slack. You can find the original thread here.

Can Pipedream retrieve a Google Doc and then return parts of the document?

Yes, Pipedream can retrieve a Google Doc and return parts of the document. You can use the Google Drive API to access the document and then process the content as needed. Here’s an example Pipedream component that retrieves a Google Doc: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, docId: { type: "string", label: "Google Doc ID", }, }, async run({ steps, $ }) { const response = await axios($, { url: `https://www.googleapis.com/drive/v3/files/${this.docId}`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, params: { alt: "media", }, }); // Process the content of the Google Doc as needed // For example, you can return specific parts of the document return response; }, }); Replace docId with the ID of the Google Doc you want to retrieve. This component will return the content of the Google Doc. You can then process the content and return specific parts of the document as needed.

I want to parse part of document and send it to ChatGPT. Then I want to return the results back to the Google Doc.

To achieve this, you can follow these steps: 1. Retrieve the Google Doc content using the Google Drive API. 2. Parse the desired part of the document. 3. Send the parsed content to ChatGPT. 4. Get the results from ChatGPT. 5. Update the Google Doc with the results. Here’s an example Pipedream component that demonstrates these steps: ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { google: { type: “app”, app: “google”, }, docId: { type: “string”, label: “Google Doc ID”, }, }, async run({ steps, $ }) { // Step 1: Retrieve the Google Doc content const response = await axios($, { url: https://www.googleapis.com/drive/v3/files/${this.docId}, headers: { Authorization: Bearer ${this.google.$auth.oauth_access_token}, }, params: { alt: “media”, }, }); // Step 2: Parse the desired part of the document // Replace this with your parsing logic const parsedContent = response; // Example: assuming the parsed content is the entire document // Step 3: Send the parsed content to ChatGPT // Replace with your ChatGPT API call and credentials const chatGPTResponse = await axios($, { url: “https://api.openai.com/v1/engines/davinci-codex/completions”, method: “POST”, headers: { “Content-Type”: “application/json”, “Authorization”: “Bearer YOUR_CHATGPT_API_KEY”, }, data: { prompt: parsedContent, max_tokens: 50, // Adjust as needed }, }); // Step 4: Get the results from ChatGPT const chatGPTResults = chatGPTResponse.choices[0].text; // Step 5: Update the Google Doc with the results // Replace with the appropriate Google Drive API call to update the document await axios($, { url: https://www.googleapis.com/drive/v3/files/${this.docId}, method: “PATCH”, headers: { Authorization: Bearer ${this.google.$auth.oauth_access_token},

What Pipedream App retrieve’s the Google Doc?

The Pipedream app for Google can be used to retrieve a Google Doc. You can use the Google Drive API to access the document and process its content. To use the Google app in Pipedream, you’ll need to set up the Google app as a prop in your component code, like this: props: { google: { type: "app", app: "google", }, // Other props }, Then, you can use the Google app’s OAuth access token (this.google.$auth.oauth_access_token) to make authorized API requests to Google Drive API.

If I have a URL:

https://docs.google.com/document/d/1b5PzFnvjFyi7yg_VtgXc_v0XVR6x9D_IyQj3wkO_xfs/edit?usp=drivesdk

I want to get the 1b5PzFnvjFyi7yg_VtgXc_v0XVR6x9D_IyQj3wkO_xfs. How do I do this?

To extract the Google Doc ID from the URL, you can use a regular expression in JavaScript. Here’s an example: const url = "https://docs.google.com/document/d/1b5PzFnvjFyi7yg_VtgXc_v0XVR6x9D_IyQj3wkO_xfs/edit?usp=drivesdk"; const regex = /document\/d\/([a-zA-Z0-9-_]+)/; const match = url.match(regex); const docId = match ? match[1] : null; console.log(docId); // Output: 1b5PzFnvjFyi7yg_VtgXc_v0XVR6x9D_IyQj3wkO_xfs This code snippet uses a regular expression to match the Google Doc ID pattern and extracts it from the URL. If the URL is valid and contains a Google Doc ID, it will be stored in the docId variable.

Is there a way to make this variable?

Yes, you can create a variable to store the extracted Google Doc ID from the URL. Here’s an example of how to do it: const url = "https://docs.google.com/document/d/1b5PzFnvjFyi7yg_VtgXc_v0XVR6x9D_IyQj3wkO_xfs/edit?usp=drivesdk"; const regex = /document\/d\/([a-zA-Z0-9-_]+)/; const match = url.match(regex); const docId = match ? match[1] : null; In this example, the docId variable will store the extracted Google Doc ID (1b5PzFnvjFyi7yg_VtgXc_v0XVR6x9D_IyQj3wkO_xfs). You can then use this variable in your Pipedream component or any other code that requires the Google Doc ID.

I am not getting the variable back.

const url = "{{steps.trigger.event.custom_fields[10].display_value}}";
const regex = /document\/d\/([a-zA-Z0-9-_]+)/;
const match = url.match(regex);
const docId = match ? match[1] : null;
console.log(docId);

It seems like you’re trying to extract the Google Doc ID from a URL stored in a custom field from the trigger event. The issue might be related to the use of double curly braces {{ }} for accessing the value. In Pipedream component code, you should access the value directly from the steps object. Try the following code: ```
const url = steps.trigger.event.custom_fields[10].display_value; const regex = /document/d/([a-zA-Z0-9-_]+)/; const match = url.match(regex); const docId = match ? match[1] : null; console.log(docId);

I getting a STEPS IS NOT DEFINED error.

I apologize for the confusion. It seems like the issue is related to the code snippet you provided. Please make sure you’re using the code within a Pipedream component. Here’s an example of how to use the code snippet within a Pipedream component: import { axios } from "@pipedream/platform"; export default { name: "Extract Google Doc ID", description: "Extracts the Google Doc ID from a given URL", props: { url: { type: "string", label: "Google Doc URL", }, }, async run() { const regex = /document\/d\/([a-zA-Z0-9-_]+)/; const match = this.url.match(regex); const docId = match ? match[1] : null; console.log(docId); return docId; }, }; In this example, I’ve created a Pipedream component with a url prop to accept the Google Doc URL. The run method contains the code to extract the Google Doc ID and return it. Make sure to use this component within a Pipedream workflow.

So now I’m trying to parse the Google Doc. I’m using this code:

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    google: {
      type: "app",
      app: "google",
    },
    docId: {
      type: "string",
      label: "Google Doc ID",
    },
  },
  async run({ steps, $ }) {
    const response = await axios($, {
      url: `https://www.googleapis.com/drive/v3/files/${this.docId}`,
      headers: {
        Authorization: `Bearer ${this.google.$auth.oauth_access_token}`,
      },
      params: {
        alt: "media",
      },
    });

    // Process the content of the Google Doc as needed
    // For example, you can return specific parts of the document
    return response;
  },
});

I am getting the following error:

Error - Request failed with status code 403
{"error":{"code":403,"message":"Only files with binary content can be downloaded. Use Export with Docs Editors files.","errors":[{"message":"Only files with binary content can be downloaded. Use Export with Docs Editors files.","domain":"global","reason":"fileNotDownloadable","location":"alt","locationType":"parameter"}]}}

I apologize for the confusion. The error message indicates that you should use the Export API for Google Docs files instead of downloading them directly. Here’s the updated code to export the Google Doc as plain text: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, docId: { type: "string", label: "Google Doc ID", }, }, async run({ steps, $ }) { const response = await axios($, { url: `https://www.googleapis.com/drive/v3/files/${this.docId}/export`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, params: { mimeType: "text/plain", }, responseType: "text", }); // Process the content of the Google Doc as needed // For example, you can return specific parts of the document return response; }, }); This code exports the Google Doc as plain text. You can change the mimeType parameter to export the document in a different format, such as "application/pdf" for PDF or "application/vnd.openxmlformats-officedocument.wordprocessingml.document" for Microsoft Word.

This is working out nicely.

Now that I have the data, how do I tell Pipedream to get the data from one particular section?

I’m glad it’s working well for you! To extract data from a specific section of the Google Doc, you’ll need to identify a pattern or delimiter that separates the sections. Once you have that, you can use JavaScript string manipulation methods to extract the desired section. Here’s an example: export default defineComponent({ // ...props and other component properties async run({ steps, $ }) { // ...Google Doc export code // Assuming sections are separated by "==== Section Title ====" const sectionDelimiter = "==== Section Title ===="; const sections = response.split(sectionDelimiter); // Get the desired section by index (e.g., 1 for the second section) const desiredSectionIndex = 1; const desiredSection = sections[desiredSectionIndex]; return desiredSection; }, }); In this example, I assume that sections are separated by a specific delimiter (e.g., “==== Section Title ====”). You’ll need to replace this delimiter with the one used in your Google Doc. The code then splits the document content into an array of sections and returns the desired section by its index. Adjust the desiredSectionIndex variable to get the specific section you need. If you need further assistance or have a different way of identifying sections, please let me know.

Can you just write the code?

Certainly! Here’s the updated code to parse a specific section from the Google Doc: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, docId: { type: "string", label: "Google Doc ID", }, }, async run({ steps, $ }) { const response = await axios($, { url: `https://www.googleapis.com/drive/v3/files/${this.docId}/export`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, params: { mimeType: "text/plain", }, responseType: "text", }); // Assuming sections are separated by "==== Section Title ====" const sectionDelimiter = "==== Section Title ===="; const sections = response.data.split(sectionDelimiter); // Get the desired section by index (e.g., 1 for the second section) const desiredSectionIndex = 1; const desiredSection = sections[desiredSectionIndex].trim(); return desiredSection; }, }); Replace the sectionDelimiter with the delimiter used in your Google Doc to separate sections. Adjust the desiredSectionIndex to get the specific section you need.