Google App Sheet Script - how to turn cell value to string?

Viewed 10909

I'm writing a Google Apps For Google Sheets. I want to read cell contents as a string and then parse it. In a cell I have a comma delimited list of IDs (e.g. "13,57,29"). Then I wrote a code that turns this into an array of strings (so "13,57,29" becomes ["13", "57", "29"]). So far I used this code:

var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();  
var blockedListString = data[0][0];
var blockedArray = blockedListString.split(",");

This works fine for the example I gave above. But I'm running into problem when the cell has only "13" in it. I get an error saying:

TypeError: cannot find function split in object 13.

So basically it seems like it stops being treated as a string and becomes something else (int?). The getValues() function returns an Object[][] so there's probably some indirect cast going on.

How can I fix that / force the cell to be read as string / force something to be cast to a string?

0 Answers
Related