I'm learning to code and learn from various websites. I'm stuck while going through one of the exercises and want solution from the community. The code is :
//Inserts a new sheet and writes a 2D array of data in it
function writeDataToSheet(data) {
var ss = SpreadsheetApp.getActive();
sheet = ss.insertSheet();
sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
return sheet.getName();
}
I want data to be written in the same google sheet and not in the new sheet and can that same sheet be used for run periodically by using a time-driven trigger. (Depending on your use case, you might want to modify the writeDataToSheet() function to keep appending data to the same sheet versus creating new sheets.)
Please guide me for the same.
Thank you
CodePudding user response:
If your new data is just one line it can be done this way:
function writeDataToSheet(data) {
SpreadsheetApp.getActiveSheet().appendRow(data);
}
Where the data
is an array. Something like this: ['aaa', 'bbb', 'ccc']
;
CodePudding user response:
Insert sheet and write 2d array to it:
function insertnewsheetandwrite2darraytoit() {
SpreadsheetApp.getActive().insertSheet().getRange(1,1,[[1,2,3]].length,[[1,2,3]][0].length).setValues([[1,2,3]]);
}