I apologize for the inconvenience you’re experiencing. It appears that the code is trying to insert text into the Google Document, but it may not be finding the correct location to insert the text. Let’s modify the code to search for the text properly and handle any errors that might occur. Please try the updated code below: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, documentId: { type: "string", label: "Google Document ID", }, chatGPTResponse: { type: "string", label: "ChatGPT Response", }, }, async run({ $ }) { const searchText = "==== AI GENERATED START ===="; try { const docResponse = await axios(this, { method: "get", url: `https://docs.googleapis.com/v1/documents/${this.documentId}`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, }); const docContent = docResponse.body.content; const searchTextElement = docContent.find( (element) => element.paragraph && element.paragraph.elements.some( (elem) => elem.textRun && elem.textRun.content.includes(searchText) ) ); if (!searchTextElement) { throw new Error(`Search text "${searchText}" not found in the document.`); } const startIndex = searchTextElement.paragraph.elements[0].startIndex + searchText.length; await axios(this, { method: "post", url: `https://docs.googleapis.com/v1/documents/${this.documentId}:batchUpdate`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, data: { requests: [ { insertText: { location: { index: startIndex, }, text: `\n${this.chatGPTResponse}`, }, }, ], }, }); } catch (error) { console.error("Error:", error.response ? error.response.data : error.message); throw new Error("Failed to update Google Doc. Please check the logs for more details."); } }, });
This code retrieves the Google Document content first, then searches for the searchText
within the document