I'm trying to split a string into separate cells, but the order of substrings will be different to how it appears in the string originally. Not all parts of the string are necessary and the length of each substring isn't static. I managed to achieve something workable on a separate project where the substring lengths were static using lastIndexOf but it was incredibly clunky.
I'm hoping that someone could point me to a better way of approaching the problem.
Each string of data will look like this:
01|020202|03|0404040404|05|0606060606|
My previous code was equivalent to this:
function splitString() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Data");
var input = sheet.getRange(2,4).getValue();
var data1 = input.substring(input.lastIndexOf("!")-5,input.lastIndexOf("|")-1);
var data2 = input.substring(input.lastIndexOf("|")-10, data1);
// etc.
sheet.getRange(3,2).setValue(data1);
sheet.getRange(4,2).setValue(data2);
// etc.
}
Can anybody suggest a better method for doing this? Any help would be much appreciated.