I have script to copy json data to google sheet, bus Is there any way to append multiple rows instead of single row
Can anybody explain how i change this script ? Thanks
function doPost(request = {}) {
const { parameter, postData: { contents, type } = {} } = request; //request data
const { dataReq = {} } = JSON.parse(contents); //content
const { fname = {} } = JSON.parse(contents); //function name
const response = {
status: "function not found: " + fname, // prepare response in function not found
data2: dataReq
}
switch (fname) { //function selection
case 'pasteData':
var output = JSON.stringify(pasteDAta(dataReq)) //call function with data from request
break
default:
var output = JSON.stringify(response)
break
}
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON); //response to frontend
}
function pasteDAta(dataReq) {
const id = '1_27rjNQmlXrwVKpLWUbGrJYPJufGRa7Dk-XEKcNAHr0'; //id of Google Sheet
var sheet = SpreadsheetApp.openById(id).getSheetByName('Sheet1'); //sheet
var headings = sheet.getDataRange().getValues()[0]; //Headers
var i = 0 //to test the times that efectively adds rows the forEach function
dataReq.forEach((a) => { //go trought every item on dataReq as 'a'
let holder = []; //to steore temp the elements
for (x in headings) { //to add in order of Headers on sheet
let output = (headings[x] in a) ? a[headings[x]] : ''; //if exist add, if not empty
holder.push(output); //add to holder
}
sheet.appendRow(holder); //put holder(order data) on sheet
i += 1 //to test the times
});
return "Numbers of sheets added: "+i;
}