Reputation: 35
I am using a modified version of Martin Hawksey's Google Apps Script that takes the data in one of my sheets in a Google Spreadsheet another sheet makes a new row for each entry in my event description column (as separated by a comma - i.e. cell D2="Description 1, Description 2, Description 3, etc.). While this works very well, I would like for it to do one more function - I would like for it to take the last entry in the event description (perhaps separated from the others by a semicolon instead of a comma) and push it into the adjacent cell once and only once for each unique event while deleting the semicolon or other separator. I've tried a number of approaches, but being new to Google Apps Script, I am not having success.
function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
var output = [];
for (i in anArray){ // for each row
var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column
for (j in splitArray){ // for each split cell value
if(splitArray[j]=="" && j>=1)
continue;
var row = anArray[i].slice(0); // take a copy of source row
row[splitColumnIndex] = alltrim(splitArray[j]); // replace comma separated value with current split value
output.push(row); // push new row to output
}
}
return output;
}
function alltrim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
Essentially, this is what I'm trying to do - turn this:
Date Client County Description
9/21/14 12345 Greene Location 1, Location 2, Location 3; Y
9/24/14 54321 Brown Location 1, Location 2; X
into this:
Date Client County Description Time
9/21/14 12345 Greene Location 1
9/21/14 12345 Greene Location 2
9/21/14 12345 Greene Location 3 Y
9/24/14 54321 Brown Location 1
9/24/14 54321 Brown Location 2 X
Any help would be greatly appreciated!
Thank you!
Upvotes: 1
Views: 1290
Reputation: 3094
This edited splitColumnAndRepeatRows() function will do what you are after, with an added bonus that any (not just the last one) comma-separated item in your column can be a ;-delimited list and will be separated into 2 cells.
function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
var output = [];
for (var i in anArray){ // for each row
var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column
for (var j in splitArray){ // for each split cell value
if(splitArray[j]=="" && j>=1)
continue;
var row = anArray[i].slice(0); // take a copy of source row
var trimmedString = alltrim(splitArray[j]);
var subArray = trimmedString.split(";"); // split current item of specified column at ;
if ( subArray.length > 1 ) { // if current item is a ;-delimited list (now split into an array)
row[splitColumnIndex] = alltrim(subArray[0]); // replace comma-separated value with first item of ;-delimited array
row[splitColumnIndex+1] = alltrim(subArray[1]); // append second item of ;-delimited list as new cell to row
}
else {
row[splitColumnIndex] = trimmedString; // replace comma separated value with current split value
row[splitColumnIndex+1] = ""; // append empty cell to row
}
output.push(row); // push new row to output
}
}
return output;
}
It can turn something like this:
9/21/14 12345 Greene Location 1; A, Location 2, Location 3; B
9/24/14 54321 Brown Location 1, Location 2; C
Into this:
9/21/14 12345 Greene Location 1 A
9/21/14 12345 Greene Location 2
9/21/14 12345 Greene Location 3 B
9/24/14 54321 Brown Location 1
9/24/14 54321 Brown Location 2 C
Upvotes: 1