I'm processing the result of a Google form with GAS. I can obtain the responses (= answers) of all items (= form questions) by:
let rbForm = FormApp.openById("nmkhVb56R…");
let rbFormResponses = rbForm.getResponses()[rbForm.getResponses().length - 1];
let rbItemResponses = rbFormResponses.getItemResponses();
Logger.log(rbItemResponses[0].getResponse()); // The answer of the first question
But now I need to have the response to a specific item and I need to address this item by its itemId. How can I get this object directly without iterating through all responses?
CodePudding user response:
Try Form.getItemById()
and FormResponse.getResponseForItem().getResponse()
, like this:
function test() {
const rbForm = FormApp.openById('nmkhVb56R…');
const item = rbForm.getItemById('...');
const responses = rbForm.getResponses();
const lastResponse = responses[responses.length - 1];
const answer = lastResponse.getResponseForItem(item).getResponse();
console.log(answer);
}