I currenty use Excel Office Script to get a response from Jotform API.
In my Json response, on the first level of my Array, I get some informations like "Submission ID" and "timestamp from Submission", then I have a Key "Answers" with all the answers from that Submission (could be 150 anwers). So it's a 2D array.
I'm having trouble being able use setValues after all the iterations and write all the answers in Excel in one shot.
Here is my iteration where rows will be "Submission ID + Timestamp", then next to it, subRows which will have all the answers for that submission, each answers being one Excel row each
let rows: (string | boolean | number)[][] = [];
let subRows: (string | boolean | number)[][] = [];
for (const [key, value] of Object.entries(result)) {
rows.push([value["id"], value["created_at"]]);
for (const [subKey, subValue] of Object.entries(value["answers"])) {
if (subValue.hasOwnProperty("answer")) {
subRows.push([checkAnswer(subValue["answer"])]);
}
}
}
// This function is important because sometimes I have several answers in one answer, so I join it when necessary.
function checkAnswer(answer: string[]) {
if (answer.length > 1) {
return (typeof answer == "string" ? answer : answer.join("||"));
}
return answer;
}
// Here are the methods I use to write the result on my Excel file. The first one is great, it is one Excel row/ one result.
const targetRange = workSheet.getRange('A5').getResizedRange(rows.length - 1, rows[0].length - 1);
targetRange.setValues(rows);
// I TRANSPOSE so that the subROWS get in line, not vertically.
subRows = subRows[0].map((_, colIndex) => subRows.map(row => row[colIndex]));
const subTargetRange = workSheet.getRangeByIndexes(4, 2, subRows.length, subRows[0].length).setValues(subRows);
My problem is that all the subrows (all the answers) are being written in one line, there is not one answer/one row.
I could use setValues inside the iteration, but I always try not too as it slows down the work.
The best being able to make one big array and do everything in one setValues.
Any idea ?
Thank you very much,
Cedric