Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spreadsheet - Google App Script [string split function]

Just wanted to verify some thought regarding split function. I have constructed a simple code.

var array1 = [{}];
var string1 = "A, B, C, D";

array1 = string1.split(",");

The problem is based on this kind of coding for example in flash. The string1 will split all "," then transfers it to the array1 in this format ["A","B","C", "D"]. Is this kind of concept similar to Google Spreadsheet - GAS? If yes can you site some example? Thanks a lot guys.

P.S: When I tried splitting the "," it only returns the value "A B C D" as a single element.

Thanks, Nash :)

like image 552
Nash Avatar asked Aug 01 '12 05:08

Nash


2 Answers

Your code should definitely work, I just ran this with a breakpoint on Logger.log(array1); The debugger shows it as an array, and the log logs it as: [A, B, C, D]. Note, that to get the output you wanted I had to add a space to the split to get: string1.split(", ");

function myFunction() {
  var array1 = splitTest();
  Logger.log(array1);
}

function splitTest() {
  var array1 = [{}];
  var string1 = "A, B, C, D";

  array1 = string1.split(", ");
  return array1
}
like image 64
Brandon Avatar answered Sep 23 '22 11:09

Brandon


J.Doe, I cannot submit a comment either, but your problem here is that if you get the value from a form, its type can be an object instead of a string. Hence you are getting the error of no function in object.

You can go around this by converting those objects to strings this way:

objectPretendingString = JSON.stringify(objectPretendingString) //becomes a string
like image 35
delimiter Avatar answered Sep 19 '22 11:09

delimiter