I have the below script that I am using to split out a JSON string.
Currently it has a unique ID in Col A, and the Data in Col B.
I want to be able to have the data in any column, provided the column is called DATA.
I want the script to look for column titled data and keep all columns left of that in the out put tab.
I have tried a few things but none of them are working.
I have created a sample workbook here to show what I'm after Splitter Example
Any help here would be amazing!
function JSON_SPLITTER() {
var ss = SpreadsheetApp.getActive();
var inputsheet = ss.getSheetByName("Input");
var outputsheet = ss.getSheetByName("Current Output");
// 0.
var response = SpreadsheetApp.getUi().prompt('JSON String Heading', 'Enter Column Heading for the JSON String in Row 1 Exactly as it appears', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL)
var JSONcolumnname = response.getResponseText()
// 1. Retrieve values from the input sheet.
var [head, ...additionalinfo] = inputsheet.getDataRange().getValues();
// 2. Check "DATA" column.
var dataIdx = head.indexOf(JSONcolumnname);
if (dataIdx == -1) throw new Error("No DATA column.");
// 3. Retrieve all keys from JSON data of the "DATA" column.
var headers = [... new Set(additionalinfo.flatMap(r => {
var obj = JSON.parse(r[dataIdx]);
return obj.hasOwnProperty("additionalInfo") ? Object.keys(obj.additionalInfo) : Object.keys(obj);
}))];
// 4. Create values of JSON data.
var objValues = additionalinfo.map(r => {
var obj = JSON.parse(r[dataIdx]);
return obj.hasOwnProperty("additionalInfo") ? headers.map(h => obj.additionalInfo[h] || "") : headers.map(h => obj[h] || "");
});
// 5. Create output values.
additionalinfo.forEach((r, i) => r.splice(dataIdx, 1, ...objValues[i]));
head.splice(dataIdx, 1, ...headers)
var res = [head, ...additionalinfo];
// 6. Put the values to the output sheet.
outputsheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}