I'm working on a Google Spreadsheet modification web application and currently have a column of values formatted like so "#, name" and I need to programmatically split the # and the name into separate columns using Google Scripts. The results are coming from Forms, which is why they need modified. I have it partially working but I'm stuck on how to get it to fully work. Here is what I am doing so far:
output = splitColumn(initialRange.getValues()); //Initial range is the column I"m working with
m = 0;
var ids= [];
var names= [];
for(k = 0; k <= output.length; k+=2)
{
ids[m] = output[k][0];
m += 1;
}
m=0;
for(n = 1; n < output.length; n+=2)
{
names[m] = output[n][0];
m += 1;
}
schoolName = aSheet.getRange(2, i+2, aSheet.getLastRow());
schoolName.setValue(names[0]);
schoolIDs = aSheet.getRange(2, i+1, aSheet.getLastRow());
schoolIDs.setValue(ids[0]);
...
function splitColumn(range) {
var output = [];
for(var i in range) {
var split = range[i][0].split(",");
if(split.length == 1) {
output.push([split[0]]);
} else {
for(var j in split) {
output.push([split[j]]);
}
}
}
return output;
}
To see my overall objective, these two screenshots should show it.
Original Spreadsheet from Form: Formatted Version of Spreadsheet:
Either a modification to my existing code to make it work or suggesting a different approach would be appreciated. Current problems are actually setting the values, as it doesn't like the type I am using, and that the split does not get rid of the preceding white space between the "," and the "name" so my array has [space]name otherpartofname.
Logger.log('my variable value: ' + varName)
to print variable values to the LOG, then VIEW the LOG. Put the Logger.log() in key places to verify that you are getting the expected results. – Alan Wells