Synthoid / ExportSheetData

Add-on for Google Sheets that allows sheets to be exported as JSON or XML.
MIT License
236 stars 46 forks source link

How can I create an array of objects? #66

Closed thiwwy closed 6 years ago

thiwwy commented 6 years ago

This is a very nice tool but I am having an issue to create an array of objects. The JSON that I am trying to export should appear like this:

{ "id": "0016", "name": "blue jeans trausers", "price": "£14.00", "details": "blue jeans", "size" : "m", 'x': '5', 'y': '30', "sizesAvailable":[ { "size" : "xs", "availability" : "0" }, { "size" : "s", "availability" : "0" }, { "size" : "m", "availability" : "0" }, { "size" : "l", "availability" : "0" }, { "size" : "xl", "availability" : "0" } ] }

I played a bit with the options and so far I managed to get just one object in "sizesAvailable", while I need actually many objects. I have tried to insert commas between all the objects that I need and it didn't work. I also played with the "Nested Elements" but still no results. How can I format my table correctly? You can see my spreadsheet here:

https://docs.google.com/spreadsheets/d/11bH3K0lI8OkXgIEKkUiQDIKS1if00sx341b26jLN8mE/edit?usp=sharing

Thanks in advance for your help.

Synthoid commented 6 years ago

Put an example together here. Enabled options for "Nested Elements", "Export cell arrays" and "Nested Array prefix". Produces the following JSON:

{
  "Sheet1": [
    {
      "id": "1",
      "name": "Shirt",
      "price": "10.00",
      "details": "none",
      "size": "M",
      "x": "9",
      "y": "9",
      "sizesAvailable": [
        {
          "size": "XS",
          "availability": "0"
        },
        {
          "size": "S",
          "availability": 0
        },
        {
          "size": "M",
          "availability": 0
        },
        {
          "size": "L",
          "availability": 0
        },
        {
          "size": "XL",
          "availability": 0
        }
      ],
      "color": "#000000",
      "color research": "black",
      "colors available": [
        1,
        2,
        3
      ],
      "related items": [
        1,
        2,
        3
      ],
      "images": [
        "/data/list-clothes-images/0016/Jeans-01",
        "/data/list-clothes-images/0016/Jeans-02",
        "/data/list-clothes-images/0016/Jeans-03",
        "/data/list-clothes-images/0016/Jeans-04"
      ]
    }
  ]
}

Does that work for what you need?

thiwwy commented 6 years ago

Yep, this is what I need but I have another issue. I actually have more than one row in my table, but when I export the JSON I have just one element (the last row precisely). I played a bit with the options and when "Nested Elements" is checked I have more objects, but the "sizesAvailable" looks like this:

"[sizesAvailable]{#1}size": "XS", "[sizesAvailable]{#1}availability": "0", "[sizesAvailable]{#2}size": "S", "[sizesAvailable]{#2}availability": "0", "[sizesAvailable]{#3}size": "M", "[sizesAvailable]{#3}availability": "0", "[sizesAvailable]{#4}size": "L", "[sizesAvailable]{#4}availability": "0", "[sizesAvailable]{#5}size": "XL", "[sizesAvailable]{#5}availability": "0",

viceversa, if "Nested Elements" is not checked, "sizeAvailable" looks fine but I have just the last object. You can see my table here: [https://docs.google.com/spreadsheets/d/1ZxpCk3coKcakhdLw-T2-KXeWaM7hi2LSsuPw-MQWYIk/edit?usp=sharing] is it a bug or am I doing something wrong with the options?

Synthoid commented 6 years ago

Made some quick edits to your example. Main issue was the sheet tab's name needed to start with "NA_" for the nested array prefix option to work. You should just need "Nested Elements", "Export cell arrays" and "Nested Array prefix" enabled to get the result you want. Let me know if that works.

Also, enabling all of the options for some sections can give unexpected results, so be careful of that in the future.

thiwwy commented 6 years ago

Yep, now it works! Thank you very much!