Help with GoogleSheet to Python to GoogleSheet

I’m trying to figure out how to place the row data into (txt= “newRow”) to run Python code

I was able to get the row data from GoogleSheet

Now I need to run it through Python code

And put results back into the GoogleSheet

step 1
Here’s my GoogleSheet trigger for the new row added:

steps.trigger.event.newRow

step 2
Here’s the print new row added:

from pipedream.script_helpers import (steps, export)

Reference data from previous steps

print(steps[“trigger”][“event”][“newRow”])

step 3
Here is where I’m running into issues. How do I place the “newRow” data in my following Python code sample:

import re

txt = “newRow”

Step 4
Once the “newRow” data runs through Python code, I will push the result back to GoogleSheet

Appreciate any help and guidance thank you!

Hi @jack-ai

First off, welcome to the Pipedream community. Happy to have you!

Congrats! You’ve figured out how to consume Step Exports from other steps, the other half to learn is how to pass data from a code step to Step Exports.

You can use the built in export function described here in the Python docs: Python

So you could do something like:

from pipedream.script_helpers import (steps, export)

# text processing here

export('txt', txt)

Then you can consume this step’s exports just like the initial Google Sheets trigger.

Thanks but how do I reference the row data as ‘txt’ ?

Hi @jack-ai

I’m sorry I thought you had already connected those dots. The referencing of the step data and your text processing should happen in the same Python step:

from pipedream.script_helpers import (steps, export)
import re

txt = steps[“trigger”][“event”][“newRow”]

# You're now ready to perform some actions like regex testinsg, etc.
# But remember that Python strings are immutable!

export('txt', txt)

This video explains how to use Step Exports with pre-built actions:

And this video shows how to use Step Exports with Node.js steps:

But the same concepts apply to Python as well.

Ok do you suggest running Python in a new step or the same step (because I’ve tried in the same step and it’s not running the txt through the code)

Hi @jack-id

You need to make a dedicated Python step for running Python code:

By default, all other pre-built steps in Pipedream are powered by Node.js.

You can follow those directions above to create a new Python code step that runs Python specifically.

Hope this helps!

Thanks I’ll take a look and try this. As I was having success just running the Python step when inputting the data e.g. txt = “the meatballs are great” but when I try to input newRow as the data that’s when it is having issues. so hopefully your suggestion works

Ok I was able to run the Python code in a new step and the results are under “Logs”

But when I try to export to GoogleSheet, “Logs” data is not showing up as an option.

how do I choose the Logs data?

Hi @jack-ai

The logs are intended for diagnostics and troubleshooting your steps, it’s not intended to be a channel to connect steps together.

Step Exports are the primary way of connecting steps together in Pipedream. I encourage you to watch our full Pipedream University series here:

The video links I sent above describe how to export and import data across steps in your workflow as well.

I was attempting to push the results to a google sheet not connect to a step

  1. import row from googlesheet step
  2. run python step
  3. push the result to google sheet step

but if you’re saying I can’t access the “logs” data to push to a google sheet, is there no workaround?

Hi @jack-ai

You’re very very close, but the mental model is export and import between steps, not pushing directly from one to another.

  1. (Trigger) Import Google Sheet

    • Exports: Google Sheet
  2. Python Code Step

    • Imports: Google sheet using from pipedream.script_helpers import steps
    • Exports: transformed txt using from pipedream.script_helpers import export
  3. Update Google Sheet

    • Imports: txt from Python Code Step
    • Exports: Result from Google Sheets

The Python Code step isn’t aware of the Update Google Sheet action after it. It just knows it’s exporting data called txt.

This allows you to reuse txt across multiple steps after the Python Code step.

Got it but in my case I need the results from the Python step being the Print which goes to “Logs” and there’s no way to push that to a Google Sheet

Hi @jack-ai

To help further, please provide your Python code as is. Then I can provide some guidance on how to use Step Exports.

I think it would just help the most just to show you directly.

from pipedream.script_helpers import (steps, export)
import re

txt = steps["trigger"]["event"]["newRow"]

export('txt', txt)

a = re.findall(r”\b(pizza|pissa)\b”, str(txt), re.IGNORECASE)
if a:

    a1 = re.findall(r”\b(pizza|pissa)(?:\W+\w+){0,3}?\W+(small|medium|large)\b|\b(small|medium|large)(?:\W+\w+){0,3}?\W+(pizza|pissa)\b", str(txt), re.IGNORECASE)
    if a1:
        print(“pizza order“)

    if not a1:
        print(“need size”)

Want the PRINT data e.g. “pizza order” (which goes into “Logs”) to go to google sheet in next step

Thanks for sharing your code - you can use export as many times as you wish in a code step.

I suggest you export a variable called pizza_order that contains a1:

from pipedream.script_helpers import (steps, export)
import re

txt = steps["trigger"]["event"]["newRow"]

export('txt', txt)

a = re.findall(r”\b(pizza|pissa)\b”, str(txt), re.IGNORECASE)
if a:

    a1 = re.findall(r”\b(pizza|pissa)(?:\W+\w+){0,3}?\W+(small|medium|large)\b|\b(small|medium|large)(?:\W+\w+){0,3}?\W+(pizza|pissa)\b", str(txt), re.IGNORECASE)
    if a1:
        print(“pizza order“)
        
    if not a1:
        print(“need size”)
    
    # Export the result of the regex operation as "pizza_order" for the next steps in your workflow to use.
    export("pizza_order", a1)

But pizza_order can be empty or undefined, so I suggest adding a Filter action between your Python Step and the Google Sheets updating step.

Thank you for this! I did test this method and while it works great, I would have to change my Python code to include the export for print as you have (not sure I want to as it would mean changing possibly thousands of lines)

Great @jack-ai

If you need assistance with specific conversions or implementations of workflows I recommend you post to our job board here: Hire an expert - Pipedream

An expert will get in touch with you and can help you implement the changes you need.