Headers in spreadsheet file

Hello! I would like to create workflow which gets some data from array of arrays and converts it to csv.
For a test I’m using simple function node with such code:

return [{json: ['a', 'b', 'c', 'd', 'e']},{json:['a1', 'b1', 'c1', 'd1', 'e1']}]

Then I’m using a “spreadsheet file” node which gives a result like this:

How can I specify csv headers in that file and get rid of indices at first row?

Thanks :slight_smile:

Having an array directly under the property json is not “allowed”. It is supposed to be always an object. The documentation about that can be found here:
https://docs.n8n.io/#/data-structure

Anyway if the data is in the format you did describe you would have to convert it into the correct format. Here an example how it can be done:
(You just have to define the name of the headers in the Convert-Node)

{
  "nodes": [
    {
      "parameters": {
        "operation": "toFile",
        "fileFormat": "csv",
        "options": {}
      },
      "name": "Spreadsheet File",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json: ['a', 'b', 'c', 'd', 'e']},{json:['a1', 'b1', 'c1', 'd1', 'e1']}];\n"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const columns = ['headline1', 'headline2', 'headline3', 'headline4', 'headline5'];\n\nconst returnItems = [];\n\nreturn items.map(item => {\n  const returnItem = {};\n  for (let i=0; i < item.json.length; i++) {\n    returnItem[columns[i]] = item.json[i];\n  }\n\n  return {\n    json: returnItem,\n  };\n});\n\n"
      },
      "name": "Convert",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Convert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert": {
      "main": [
        [
          {
            "node": "Spreadsheet File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes