I am trying to alter the below script to write Google Analytics data to a new row in an existing sheet (the only sheet within my Google Sheet), but am not having any success. I am new to Google Apps Scripting, so any help is much appreciated.
I have tried replacing "var sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();" with "var sheet = SpreadsheetApp.getActiveSpreadsheet().appendRow();", but am getting errors.
function outputToSpreadsheet(results) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();
// Print the headers.
var headerNames = [];
for (var i = 0, header; header = results.getColumnHeaders()[i]; ++i) {
headerNames.push(header.getName());
}
sheet.getRange(1, 1, 1, headerNames.length)
.setValues([headerNames]);
// Print the rows of data.
sheet.getRange(2, 1, results.getRows().length, headerNames.length)
.setValues(results.getRows());
}