Skip to main content

I need to create a zap that:

  1. Is triggered by the creation of a new row in an existing google sheets; and
  2. Creates a new spreadshsheet with only the columns that have a value in the newly created row in step 1. 

 

I have 286 columns in the google sheets, so applying individual Filter by Zapier just doesn’t cut it. 

I tried Code by Zapier and used the AI generated Javascript but it doesn’t work. Always gives back an error.

 

Can someone help please? Attached are the names of the columns. 

Hi ​@zamshaRR 👋

You’re definitely on the right track with trying a Code by Zapier action. It could output a list of column names where the corresponding field contained a value. Then use the output from that Code step to create a spreadsheet with only those columns. I expect the prompt for the Code step might just need a bit of tweaking.

Here’s the prompt I gave the AI code generator for a Python, not Javascript Code action:

e6f4f8795c6a7c4716f6777d4cfa0c9a.png
It then created this code:


# Define the mapping of input fields to their corresponding column names
field_to_column = {
'productNameField': 'Product Name',
'productIDField': 'ID',
'costField': 'Cost',
'colorsField': 'Colors',
'qtyField': 'Stock Quantity'
}

# Collect column names for fields that have values
columns_with_values = i
column_name for field, column_name in field_to_column.items()
if input_data.get(field) # Check if the field has a value
]

# Join the column names into a comma-separated string
output = {'columns': ', '.join(columns_with_values)}

The full set up looked like this:

799ec88cdc6ca27bb4b6f659490f7230.png
Which correctly output just the Product Name and Cost column names:

a9485b3d58ca50a3f6b24a8058c7e6f9.png
Then when I selected that set of column names from the Code step in a Create Spreadsheet action it successfully created a new spreadsheet with only those columns:

a58da3c0d6592f8bb72b3aabf4a830b2.png
c0ab473de0477499a4eb0e3d609efdaa.png

The trouble you’d have with is approach is if you’d want to then add values into that spreadsheet the Zap just created—as each field would need to be mapped so that the Zap knows where to put each value, but only the columns that were added to the spreadsheet would be available to map a value to:

6735d0c94d89a96bc3151a26bfb82451.png
So it might be better to create a spreadsheet with all the possible columns, map each field form the Google Sheets trigger and then run a script on Google’s end to automatically hide any columns that don’t contain a value. I found a post on Stack overflow that gives an example of how to do that: Hide columns when cell is empty in google script

Or, perhaps you could use an API Request action or Custom Action for Google Sheets to map the fields to the relevant columns without the need to map them. Both of those types of actions are a bit more advanced but we have some general guides on how to set them up here:

Hope that helps to get you pointed in the right direction. If you run into any issues on any of that or have further questions do let us know—happy to assist further! 🙂 


Reply