I am using 2 sheets, rawData and processedData.
rawData looks like this:
Title Options
Title1 Option1, Option2, Option3, Option4
Title2 Option1, Option2, Option3, Option4, Option5
Title3 Option1, Option2, Option3
processedData should look like this:
Title Options
Title1 Option1
Title1 Option2
Title1 Option3
Title1 Option4
Title2 Option1
Title2 Option2
Title2 Option3
Title2 Option4
Title2 Option5
Title3 Option1
Title3 Option2
Title3 Option3
In Google Sheets, I would use the formula =TRANSPOSE(SPLIT(rawData!B2,",",TRUE,TRUE)) to get the options for the first title in the format I need.
I'm trying to create a Google Apps Script that could take each row of rawData and convert it to the format shown in processedData.
function formatData() {
// File
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get rawData sheet
var rawData = ss.getSheetByName('rawData');
// Get processedData sheet
var processedData = ss.getSheetByName('processedData');
// Get range that contains titles, get its values
var titlesRange = rawData.getRange('A2:A');
var titlesRangeValues = titlesRange.getValues();
// Get range that contains options, get its values
var optionsRange = rawData.getRange('B2:B');
var optionsRangeValues = optionsRange.getValues();
// Get number of rows in rawData sheet
var titlesCount = GSheetsUtils.getRowsData(rawData);
// Get last row in processedData sheet
var lastRow = processedData.getLastRow();
// Copy each title to processedData and format the options
for (var i = 0; i<titlesCount.length; i++) {
processedData.getRange(lastRow + 1,1).setValue(titlesRangeValues[i][0]);
processedData.getRange(lastRow + 1,2).setValue(optionsRangeValues[i][0]);
lastRow++;
}
}
Executing this code will populate processedData like so:
Title Options
Title1 Option1, Option2, Option3, Option4
Title2 Option1, Option2, Option3, Option4, Option5
Title3 Option1, Option2, Option3
How can I get the script to split and transpose the options but write the same title for each option?
Thank you in advance.
PS: The code is likely not efficient and I assume it lacks any best practices, it's my first time using Google Apps Script so if anyone has any pointers I would appreciate it. I have been looking through Google's documentation but I need to sleep on it.