I am currently working on a project where I need to store Google Form responses in an array instead of a Google Sheet. After collecting the responses, I plan to extract specific elements from the array and use them to create a new project through an API. Although I have searched online for a solution, the code I found is posing difficulties in accessing only the necessary elements for the post request.
Below is the code snippet that fetches the form responses upon submission:
function captureFormResponse()
{
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;
}
Additionally, here is the code for the POST request that should fetch data from the previous function. Specifically, I aim to retrieve the project name, client name, start date, and end date from the form submission and pass this information 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"];
If anyone can provide guidance on how to structure the code to seamlessly capture responses upon submission and forward them to the post request, it would be greatly appreciated.