I haven't tested this code.
So, I'm not sure if it has any bugs or not.
But it should be complete enough to explain what needs to be done.
If you have any questions, post back.
First enter your email address for FormSumbit into the getAPI_key function var myEmail
Run the function getAPI_key.
Hard code the API key into the function writeDataToSheet.
Hard code the spreadsheet file ID and sheet tab name into the function writeDataToSheet
Run writeDataToSheet
function getAPI_key() {
var ao,data,json,myEmail,response;
/*
First get your API key which is needed to make a request to get your data
*/
myEmail = "Enter the email you use for Form Submit here";
ao = {};
response = makeRequest(ao);
data = response.getContentText();
Logger.log('data: ' + data)
json = JSON.parse(data);
Logger.log('json: ' + json)
}
function writeDataToSheet() {
var answers,apikey,data,i,innerArr,k,L,sh,ss,submissions,thisAnswer,values;
apikey = "Enter API key here";
data = getFormSubmitData(apikey);
if (data.success !== 'true') {
Logger.log('ERROR')
return;
}
submissions = data.submissions;//An array of objects
L = submissions.length;
values = [];
for (i=0;i<L;i++) {
innerArr = [];
answers = submissions[i].form_data;//Get an object of answers out of the object
for (k in answers) {
thisAnswer = answers[k];
innerArr.push(thisAnswer);
}
values.push(innerArr)
}
ss = SpreadsheetApp.openById("Put the spreadsheet file ID here");
sh = ss.getSheetByName("Put the name of the sheet tab here");
sh.getRange(1, 1, values.length, values[0].length).setValues(values);//Set all values at once
}
function getFormSubmitData(API_Key) {
var ao,data,json,response;
ao = {};
response = makeRequest(ao);
data = response.getContentText();
Logger.log('data: ' + data)
json = JSON.parse(data);
Logger.log('json: ' + json)
return data;
}
function makeRequest(po) {
try{
var i,L,r;
/*
po.url - the url to make the https request to
po.options - the payload and method and any other options - Will default to GET
po.loopCnt - number of times to try
po.logError - log an error if there is a failure - false - dont log error
*/
/* Because payload is a JavaScript object it will be interpreted as
as form data (No need to specify contentType- it will automatically
default to either 'application/x-www-form-urlencoded'
or 'multipart/form-data') */
//Logger.log('po: ' + po)
if (!po.url) {
console.error('Error Code - 98765 - No URL passed in. Parameters:' + JSON.stringify(po));
return;
}
if (!po.options) {//Always need options because always need to mute exceptions
po.options = {};
}
L = po.loopCnt ? po.loopCnt : 3;
po.options.muteHttpExceptions = true;//Make sure this is always set
for (i=1;i<L;i++) {
try{
r = UrlFetchApp.fetch(po.url, po.options);
break;
}catch(e){
if (i!==L-1){Utilities.sleep(i*2000);}
if (i>=L-1) {
if (!po.logError) {
break;
}
Logger.log('error 53: ' + e.error)
console.error('ERROR - qhyyfhvxud6tr46f8g - : ' + e + ' Stack: ' + e.stack + "\n\n" + r);
}
};
}
//Logger.log('r 59: ' + r)
if (!r) {
console.error('ERROR - getting response' );
return;
}
//Logger.log('r.getResponseCode() 66: ' + r.getResponseCode());
if (r && r.getResponseCode() !== 200) {//There should always be a response unless the
//Fetch call failed and if that happens then the outer try - catch can handle it
//Logger.log('code is not ' + ' 200')
if (r && r.getResponseCode() !== 200) {//There should always be a response unless the
//Fetch call failed and if that happens then the outer try - catch can handle it
//Logger.log('code is not' + '200')
var arg = po ? JSON.stringify(po) :"po not passed in";
Logger.log('ERROR ' + r + '\npo:' + arg);
return false;
}
return false;
}
//Logger.log('r 81: ' + r)
return r;
}catch(e){
var rzpnzCode,theTxt;
if (r) {
try{
rzpnzCode = r.getResponseCode();
}catch(e) {
Logger.log('ERROR: ' + e.message + "\n\nStack: " + e.stack);
}
}
if (rzpnzCode !== 200) {//There could be an error even if the Request was successful - but if the request
//was successful an error message should not be sent out because the error could just be that the receiving
//URL did not return anything - which could happen a lot even when everything is fine
try {
theTxt = r.getContentText();
} catch(e) {
theTxt = 'Error getting information out of the return value';
}
Logger.log('ERROR: ' + e.message + "\n\nStack: " + e.stack + "\nThe message: " + theTxt);
}
return r;
};