Search code examples
javascriptgoogle-apps-scriptgoogle-sheetscustom-function

google spreadsheet script changes comma to full-stop upon reading from a cell?


So, I'm trying to read data from a cell in Google Spreadsheet's script and something's not right for me. I made a quick test function that takes the data from the cell, turns it to a string and returns the result, like so:

function TEST(str)
{
  return str.toString();
}

Then I made two test cells:

A1 with "17"
A2 with "135,136"
A3 with "1,11,29,43,68,74,109,122"

Then I called my function and I'm baffled by results

TEST(A1) returns "17"
TEST(A2) returns "135.136"
TEST(A3) returns "1,11,29,43,68,74,109,122"

So it seems that if there is exactly one comma it gets cast to a full-stop instead but if there are multiple none of them get replaced. What is going on and how can I read data from a cell as a string with 100% certainty that it won't get changed in any way?


Solution

  • Issue:

    • Some locations use , as decimal separator. So, 135,136 is a valid number(=135.136). But, 1,11,29,43,68,74,109,122 is not. Valid numbers are by default aligned to the right of the cell(Clear cell formatting to witness).

    Solution(s):

    • Set your locale properly, if you're going to use ,s as commas and not as decimal separators OR
    • Input numbers as text. Use ' before inputting data in each cell. '135,136 OR
    • Send inputs as Text. =TEST(TO_TEXT(A1)) OR
    • Use range#getDisplayValues() instead.