This topic was automatically generated from Slack. You can find the original thread here.
Let’s say I use a workflow to open a Google Doc. Is there a way for me to determine what is the first H1 in the document and use it as a value in another application?
To extract the first H1 from a Google Doc and use it as a value in another application, you can follow these steps: 1. Use the Google Drive API to find the Google Doc by its ID or name. 2. Use the Google Docs API to read the content of the document and find the first H1. 3. Use the extracted H1 value in another application. Here’s an example Pipedream component that reads a Google Doc and extracts the first H1: 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://docs.googleapis.com/v1/documents/${this.docId}`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, }); const firstH1 = response.body.content.find( (element) => element.paragraph && element.paragraph.paragraphStyle.namedStyleType === "HEADING_1" ); if (firstH1) { const h1Text = firstH1.paragraph.elements[0].textRun.content.trim(); return h1Text; } else { console.log("No H1 found in the document"); } }, }); Replace docId with the ID of your Google Doc. Once you have the first H1 value, you can use it as an input in another application’s action or API call within your workflow.
To create a new draft post in your WordPress site using the REST API, you’ll need to make an authenticated POST request to the wp/v2/posts endpoint. First, you need to obtain the necessary credentials (username and password or an API token) for your WordPress site. Here’s an example Pipedream component that connects to your WordPress site and creates a new draft post: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { wordpressUrl: { type: "string", label: "WordPress Site URL", }, username: { type: "string", label: "WordPress Username", }, password: { type: "string", label: "WordPress Password", secret: true, }, postTitle: { type: "string", label: "Post Title", }, postContent: { type: "string", label: "Post Content", }, }, async run({ steps, $ }) { const authToken = Buffer.from(`${this.username}:${this.password}`).toString("base64"); const response = await axios($, { method: "POST", url: `${this.wordpressUrl}/wp-json/wp/v2/posts`, headers: { Authorization: `Basic ${authToken}`, }, data: { title: this.postTitle, content: this.postContent, status: "draft", }, }); return response; }, }); Replace wordpressUrl, username, and password with your WordPress site’s URL and credentials. Set postTitle and postContent to the desired title and content for the new draft post. This component will create a new draft post on your WordPress site using the provided information.
I apologize for the issue you’re experiencing. It seems the code is only returning the first part of the title when it starts with a number. Let’s modify the code to correctly return the full title: ```
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://docs.googleapis.com/v1/documents/${this.docId}, headers: { Authorization: Bearer ${this.google.$auth.oauth_access_token}, }, }); const firstH1 = response.body.content.find( (element) => element.paragraph && element.paragraph.paragraphStyle.namedStyleType === “HEADING_1” ); if (firstH1) { const h1Text = firstH1.paragraph.elements.map(element => element.textRun.content).join(“”).trim(); return h1Text; } else { console.log(“No H1 found in the document”); } }, });