Synthoid / ExportSheetData

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

Cell array incorrectly coercing strings to numbers in JSON output #159

Open stuffisthings opened 1 year ago

stuffisthings commented 1 year ago

Describe the bug I have array cells in my document that can contain a mix of values like 6 or "6-7". I want the numbers to be treated as numbers and the others to be treated as strings. But values like "6-7" are coerced to 6 in the output. This is true even in arrays that are all string values like "6-7"/"6-7"/"6-7" (/ is my seperator here).

To Reproduce Steps to reproduce the behavior:

  1. Create a cell with value "6-7"/"6-7"/"6-7"
  2. Name the column JA_foo
  3. Options: Array prefix: "JA_", Array seperator: "/", Export contents as array
  4. JSON output will be rendered as [ { "foo": [ 6, 6, 6 ] } ]

Expected behavior JSON output sould be [ { "foo": [ "6-7", "6-7", "6-7" ] } ]

Screenshots

Here's some test cases against the actual output: image

[
  {
    "foo": [
      6,
      6,
      6,
      6
    ]
  },
  {
    "foo": [
      "Thing",
      "Thing",
      "Thing"
    ]
  },
  {
    "foo": [
      "One-2",
      "One-3"
    ]
  },
  {
    "foo": [
      "2-One",
      "3-One"
    ]
  },
  {
    "foo": [
      6,
      9
    ]
  },
  {
    "foo": [
      6,
      6,
      6
    ]
  }
]

Info (please complete the following information):

Possible solution

My guess would be it has something to do with use of parseFloat in getCellContentArray (because parseFloat('6-7') === 6).

I could try to do a PR at some point but what I've done to deal with similar situations in the past is check the length of parseFloat(val) against the length of val and if they are different treat val as a string -- not sure that is totally robust though.