I am trying to get google form responses on submitting to be saved in an array rather than a google sheet. I would then require to access about four elements from the responses in array to create a new project via API based on the responses. I have looked online for a code to accomplish this, but the one I got is giving me a challenge in accessing only the elements I need in the post request.
Here is the code accessing the responses on submit.
function formResponse()
{
var form = FormApp.openById('form-id');
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++)
{
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++)
{
var itemResponse = itemResponses[j];
Logger.log('Response #%s to the question "%s" was "%s"',
(i + 1).toString(),
itemResponse.getItem().getTitle(),
itemResponse.getResponse());
}
}
return formResponses;
}
Here a code for the POST request which is supposed to acquire data from the previous function. So basically I would like to get the project name, client name, start and end date of the project on the form submit to be passed directly to the post request.
var data = {
'name': lastRow[0][2],
'client': lastRow[0][5],
'starts_at': lastRow[0][7],
'ends_at': lastRow[0][8],
'project_state': "Tentative",
};
var payload = JSON.stringify(data);
var options = {
'method': 'POST',
'Content-Type': 'application/json',
'payload': data,
};
}
var url = TK_URL + 'auth=' + TOKEN
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() === 200) {
var json = JSON.parse(response);
var id = json["id"];
I will appreciate any help in structuring the code to allow me to get a response directly on submit and pass it to the post request.