How to separate metafields into different Google Sheets columns?

jake_mitchell
Shopify Partner
120 2 53

I am adding a row to Google Sheets when certain things happen. Let's just say I'm using 'product added to store'. 

 

I want to be able to have columns for each of the main metafields added to the item. 

 

At the moment if you use the Namspace or Value variables it will just compile all the different metafields into a single cell. 

 

jake_mitchell_0-1669214747942.png

 

I want to be able to have different columns for each metafield, but can't work out the liquid to do this. 

 

I was trying something like this (where the bold text is my namespace.key: 

 

{% for metafields_item in product.metafields %}
{% for metafields_item in product.department %}
{{metafields_item.value}}
{% endfor %}
{% endfor %}

 

It seems odd that there isn't a way to say that I only want to show the value of one particular metafield per column. I assume that must be possible and that I'm just missing something. 

 

Does anyone know? 

 

 

Replies 7 (7)

paul_n
Shopify Staff
1005 133 240

You cannot use namespace and key that way in Flow yet. You should remove that inner loop and add:

 

{% if metafields_item.key == "department" and metafields_item.namespace == "product" %}

{{ metafields_item.value }}

{% endif %}

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
jake_mitchell
Shopify Partner
120 2 53

Love this! Thanks @paul_n 

jake_mitchell
Shopify Partner
120 2 53

Old comment edited to be replaced by reply above. 

jake_mitchell
Shopify Partner
120 2 53

Ok. 

Having looked over it further I think the issue may be in how I presented the prolem. 

Let's say we have 2 metafield each with multiple options that can be added. 

Metafield 1Metafield 2
Option 1Option A
Option 2Option B
Option 3Option C

 

For the purposes of explaining this the bold ones are the ones I add to the product. 

 

When this gets to the google sheet it gets recorded as below (where whitespace is added if options were used that weren't the first option available.

 

["Option 1"]

 

["Option 2"]

["Option A"]

 

["Option B"]

 

I need it to record values from the same namespace.key together i.e.

 

Option 1, Option 2Option A, Option B

 

I suspect I need to use some combination of trim, remove (for the  [""]) and join (to make it a comma separated list, but can't work it out. 

If the google sheet connecter will always split MF values across multiple columns it is not usable by us. Suspect this may be a common issue for multi-select MFs.

paul_n
Shopify Staff
1005 133 240

Assuming you want to keep the comma, it needs to be escaped

{{- metafields_item.value | replace: ",","\," -}}

 

Hyphens remove the whitespace. If you want to remove [ and ] just add two more "replace" filters to the above code.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
jake_mitchell
Shopify Partner
120 2 53

Thanks Paul. 

There isn't actually a comma to keep. The metafield value split over different columns in sheet. What I want to do is keep all MF relating to any one particular namespace.key within the same cell and have them in a comma separated list, with all that whitespace removed. 

Otherwise it become impossible to read the sheet as the MFs will not line up vertically with each other. 

paul_n
Shopify Staff
1005 133 240
It split over multiple columns because it has a comma in it and the Sheets
action will split by comma. If you want them in the same column, you need
to keep or replace (maybe with a space, " ") the original comma
Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.