Create table from one object

Hello,

I have used the google spreadsheet api get access a public spreadsheet.

I have filtered the data so far but I am not able to format the data properly in order to push the data into my personal google spreadsheet.

How can I do this?

Please check the following screenshot in order to understand my current format:

Thank you in advance!

What you want to do? Can you explain a bit.

Thank you for the reply.

My goal is to paste the rows and columns (from the screenshot) into my private google sheet.

However, I am struggling with how to do this…

Kind regards
Silas

Hi @Silas_Hundhausen, from looking at the data structure from your screenshot, this will almost certainly require custom JS through the Function node.

Since the data structure is somewhat nested, maybe you could copy the actual JSON code in here rather than a screenshot (of course you can replace the actual values with some dummy text)? Otherwise people trying to help with the transformation would have to retype all the keys from the screenshot manually and get everything right.

Also, did you run into any trouble with the existing Google Sheets node? This node would return a data structure that’s much easier to process with n8n than the raw HTTP response from the Google Sheets API.

Thanks for reply.

Attached you can find the JSON-Structure as requested (shortened output).

My main goal is to access a public google sheet (Read only permission) and upload the data into my database. I have tried to use the google sheets node with the “service account” but I didn’t receive output. Do you have a better solution?

[
    {
        "sheets": [
            {
                "data": [
                    {
                        "rowData": [
                            {},
                            {
                                "values": [
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {
                                        "formattedValue": "##### Text #####"
                                    }
                                ]
                            },
                            {
                                "values": [
                                    {},
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {},
                                    {
                                        "formattedValue": "##### Text #####"
                                    }
                                ]
                            },
                            {
                                "values": [
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    },
                                    {
                                        "formattedValue": "##### Text #####"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

Hi @Silas_Hundhausen, many thanks for sharing!

Here is an example workflow setting your data in the initial Function node (you can ignore that, it’s just to simulate your HTTP Request node). It then uses the Split Items node to convert the rowData into individual items before using an IF node to check if the respective item has a values property (and thus excluding empty rows). It then uses a Function Item node to split up the values field into individual fields, one per column like so:

return {
  "Column A": item.values[0].formattedValue,
  "Column B": item.values[1].formattedValue,
  "Column C": item.values[2].formattedValue,
  "Column D": item.values[3].formattedValue,
  "Column E": item.values[4].formattedValue,
  "Column F": item.values[5].formattedValue
};

I assume you know the actual column names (or have own ones you want to use), so you might want to replace Column A, Column B etc. with these. Also these are just the first six columns, but you can add more columns as required. The entire workflow looks like so:

And here’s the JSON data which you can copy into your own n8n instance:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.hasOwnProperty(\"values\")}}",
              "value2": true
            }
          ]
        }
      },
      "name": "Values Available?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "sheets[0].data[0].rowData",
        "options": {}
      },
      "name": "Split Out Rows",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n        \"json\": {\n            \"sheets\": [{\n                \"data\": [{\n                    \"rowData\": [{},\n                        {\n                            \"values\": [{\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                }\n                            ]\n                        },\n                        {\n                            \"values\": [{},\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {},\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                }\n                            ]\n                        },\n                        {\n                            \"values\": [{\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                },\n                                {\n                                    \"formattedValue\": \"##### Text #####\"\n                                }\n                            ]\n                        }\n                    ]\n                }]\n            }]\n        }\n    }\n\n];"
      },
      "name": "Set Test Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return {\n  \"Column A\": item.values[0].formattedValue,\n  \"Column B\": item.values[1].formattedValue,\n  \"Column C\": item.values[2].formattedValue,\n  \"Column D\": item.values[3].formattedValue,\n  \"Column E\": item.values[4].formattedValue,\n  \"Column F\": item.values[5].formattedValue\n};"
      },
      "name": "Split in Columns",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        1050,
        210
      ]
    },
    {
      "parameters": {},
      "name": "Do Nothing",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1050,
        400
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Test Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Values Available?": {
      "main": [
        [
          {
            "node": "Split in Columns",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Do Nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out Rows": {
      "main": [
        [
          {
            "node": "Values Available?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Test Data": {
      "main": [
        [
          {
            "node": "Split Out Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope this helps! Let me know if you have any questions on this :slight_smile:

1 Like

Thank you very much!!! It works :slight_smile:

1 Like