This topic was automatically generated from Slack. You can find the original thread here.
I connected to the gmail app. Used receiving of emails as the trigger. The event I am creating from that trigger is to add a new row in a google sheet.
However, I need the new row to break up the event snippet into appropriate columns in the sheet. How do I do that?
For example, for an email that’s my trigger, the event snippet looks like this:
“Name John Smith Question 1 Yes Question 2 No Question 3 Yes”
I need “John Smith” to go into column 1 in the spreadsheet. “Yes” in column 2. “No” in column 3, etc.
But I cannot because it’s only letting me select the event.snippet as a whole.
To achieve this, you can use a code step to process the event snippet and extract the required values before adding them to the Google Sheet. Here’s an example workflow: 1. Use the Gmail “New Email” trigger. 2. Add a “Run Node.js code” step with the following code to process the event snippet: const snippet = steps.trigger.event.snippet; const parts = snippet.split(" "); const name = `${parts[1]} ${parts[2]}`; const question1 = parts[4]; const question2 = parts[6]; const question3 = parts[8]; return { name, question1, question2, question3 }; 3. Add a Google Sheets “Add Row” action step. In the step configuration, map the output from the code step to the appropriate columns in the sheet. For example: - Column 1: steps.code.name - Column 2: steps.code.question1 - Column 3: steps.code.question2 - Column 4: steps.code.question3 This will split the event snippet and add the extracted values to the appropriate columns in the Google Sheet.
Apologies for the confusion. It seems there was a formatting issue in the code example. Here’s the corrected code for the “Run Node.js code” step: async function processSnippet(snippet) { const parts = snippet.split(" "); const name = `${parts[1]} ${parts[2]}`; const question1 = parts[4]; const question2 = parts[6]; const question3 = parts[8]; return { name, question1, question2, question3 }; } const result = await processSnippet(steps.trigger.event.snippet); return result; This code defines an async function processSnippet and then calls it with the event snippet. The returned result will be available for the next step to add to the Google Sheet.
This is working! But what if the values in the answers are not always the same number of parts?
Is there a different ways to find the values by line break for example?
Yes, you can modify the code to handle cases where the values in the answers have a different number of parts. If the values are separated by line breaks, you can split the event snippet using line breaks and then process each line accordingly. Here’s an updated version of the code step: ```
async function processSnippet(snippet) { const lines = snippet.split(“\n”); const name = lines[0].split(" “)[1] + " " + lines[0].split(” “)[2]; const question1 = lines[1].split(” “)[2]; const question2 = lines[2].split(” “)[2]; const question3 = lines[3].split(” ")[2]; return { name, question1, question2, question3 }; } const result = await processSnippet(steps.trigger.event.snippet); return result;
I apologize for the issue you’re facing. It seems like the error is caused by a missing or undefined value in the event snippet. To handle this, let’s add a check to ensure the snippet exists before processing it. Here’s the updated code: ```
async function processSnippet(snippet) { if (!snippet) { console.error(“Snippet is undefined or missing”); return; } const lines = snippet.split(“\n”); const name = lines[0].split(" “)[1] + " " + lines[0].split(” “)[2]; const question1 = lines[1].split(” “)[2]; const question2 = lines[2].split(” “)[2]; const question3 = lines[3].split(” ")[2]; return { name, question1, question2, question3 }; } const result = await processSnippet(steps.trigger.event.snippet); return result;
Same error: TypeError
Cannot read properties of undefined (reading ‘split’).
Here are the details:
at null.processSnippet (file:///tmp/__pdg__/dist/code/1ea73ca22aa895544e40f0f85fa71932b98bdcb1402ecfb3d50c39b934829cf6/component.mjs:11:25)
at Object.run (file:///tmp/__pdg__/dist/code/1ea73ca22aa895544e40f0f85fa71932b98bdcb1402ecfb3d50c39b934829cf6/component.mjs:21:22)
at null.executeComponent (/var/task/launch_worker.js:229:53)
at MessagePort.messageHandler (/var/task/launch_worker.js:726:28)
I apologize for the inconvenience. It seems like one of the lines in the snippet is undefined or missing. To handle this, let’s add checks for each line before processing it. Here’s the updated code: ```
async function processSnippet(snippet) { if (!snippet) { console.error(“Snippet is undefined or missing”); return; } const lines = snippet.split(“\n”); const nameLine = lines[0]; const question1Line = lines[1]; const question2Line = lines[2]; const question3Line = lines[3]; if (!nameLine || !question1Line || !question2Line || !question3Line) { console.error(“One or more lines are missing or undefined”); return; } const name = nameLine.split(" “)[1] + " " + nameLine.split(” “)[2]; const question1 = question1Line.split(” “)[2]; const question2 = question2Line.split(” “)[2]; const question3 = question3Line.split(” ")[2]; return { name, question1, question2, question3 }; } const result = await processSnippet(steps.trigger.event.snippet); return result;
I see, if the text is not separated by line breaks, we can try using a different delimiter to split the text. Can you provide a sample of the text you’re working with? This will help me understand the format and suggest a suitable delimiter to split the text correctly.
Name John Smith Email johnsmith@gmail.com Phone Number 99999999 Have you ever owned a dog? No Do you prefer a male or female puppy? Male Are you willing to walk with the dog regularly? Yes Does anyone in your immediate family have allergies to dogs? No Are you aware of the costs associated with keeping a dog? No Do you have a fenced backyard? No Do you have parks near your home? No When would you like to adopt a puppy? ASAP
On average, how many hours per day will the puppy be left alone? Less than 1 hour How did you find out about us? Search Engine (e.g. Google, Yahoo) Comments or questions you have for us hello, I used to have a dog blah blah
I understand that the text is not separated by line breaks. In this case, we can use a regular expression to extract the required information. Here’s an updated code snippet to process the text: ```
async function processSnippet(snippet) { if (!snippet) { console.error(“Snippet is undefined or missing”); return; } const nameRegex = /Name\s(.?)\sEmail/; const question1Regex = /Have you ever owned a dog?\s(.?)\sDo/; const question2Regex = /Do you prefer a male or female puppy?\s(.?)\sAre/; const question3Regex = /Are you willing to walk with the dog regularly?\s(.?)\sDoes/; const nameMatch = snippet.match(nameRegex); const question1Match = snippet.match(question1Regex); const question2Match = snippet.match(question2Regex); const question3Match = snippet.match(question3Regex); if (!nameMatch || !question1Match || !question2Match || !question3Match) { console.error(“One or more matches are missing or undefined”); return; } const name = nameMatch[1]; const question1 = question1Match[1]; const question2 = question2Match[1]; const question3 = question3Match[1]; return { name, question1, question2, question3 }; } const result = await processSnippet(steps.trigger.event.snippet); return result;
I apologize for the error in the code. It seems there’s an issue with the formatting. Please share the code you’re trying to run, and I’ll help you identify and fix the issue.
exportdefault defineComponent({ asyncfunction processSnippet(snippet) { if (!snippet) {
console.error(“Snippet is undefined or missing”); return;
}
const nameRegex = /Name\s(.?)\sEmail/; const question1Regex = /Have you ever owned a dog?\s(.?)\sDo/; const question2Regex = /Do you prefer a male or female puppy?\s(.?)\sAre/; const question3Regex = /Are you willing to walk with the dog regularly?\s(.?)\sDoes/;