Writing to Google Sheets from a Google Form using Script

77 views Asked by At

So I have a google form and I am processing the data from the form using script, and I ultimately want to populate this data into a spreadsheet. I've tested my code and the logger is printing all the data in the form itself, but my google sheet remains empty. I suspect I'm using the wrong code to write to sheets, but I'm not sure. I scoured through the Sheets API and it says that I can write into a sheet using appendRow and that I can choose which sheet to write to using openById(), but these do not seem to work. I would really appreciate any insights!

This is the code I have right now to write into my spreadsheet, which is not working.

var array1 = [id, score];  // these are 2 variables that were initialized previously 
array1 = array1.concat(array2);  
// array2 is another array that I've initialized previously which works fine
// (I've tested both of these arrays in the Logger and it prints correctly)  
var ss=SpreadsheetApp.openById(‘SSID’); 
var sheet = ss.getSheetByName("Name");
sheet.appendRow(array1);
2

There are 2 answers

2
Andrew Roberts On

Take the speech marks from around SSID, if the constant SSID is defined:

var SSID = 'xxxxxx';
var ss = SpreadsheetApp.openById(SSID);

or if you are using the ID directly:

var ss = SpreadsheetApp.openById('xxxxxx');
0
Kartik Shah On

So after playing around it for a while, I've realized that the fix was to simply delete the onFormSubmit trigger and then run it again. Not sure why this fixed it, but I'm assuming that it wasn't syncing/updating accurately. Thanks for all the help!