Home > Net >  Get The Last Google Form Update but only the selected Fields
Get The Last Google Form Update but only the selected Fields

Time:02-28

This Google App Script code returns all updates (Answers) of the form but I want to return only from selected fields out of many fields of the form.

E.g. I want to return only Name, Address, Mobile.

is it possible to do it GAS?

function getArrayOfLastSubmissionsAnswers() {
  var allQuestions,i,itemType,L,thisAnswer,thisQuestion,thisSubmissionsAnswers,number_of_submissions;
    //Define all variables without assigning a value - value at this point
    //is undefined

  number_of_submissions = FormApp.openById('1VH8ayeXPtsURs9MPxIJ9bj6wQVXmdD9yQ-JWHyOAZ8').getResponses().length;
  allQuestions = FormApp.openById('1VH8ayeXPtysURs9PxIJ9bj6wQVXmdD9yQ-JWHyOAZ8').getResponses()[number_of_submissions - 1].getItemResponses();

  L = allQuestions.length;//How many questions are there in the Form
  thisSubmissionsAnswers = [];//Create an empty array for answers

  for (i=0;i<L;i  ) {//Loop through all the questions in this Form submission
    thisQuestion = allQuestions[i];//Get this question

    itemType = thisQuestion.getItem().getType();

    if (itemType === FormApp.ItemType.PAGE_BREAK) {
      continue;//keep looping
    };

    thisAnswer = thisQuestion.getResponse();//Get the answer
    Logger.log(i   " - "   thisAnswer); 

    thisSubmissionsAnswers.push(thisAnswer);//add answer to the array
  };

  Logger.log("thisSubmissionsAnswers: "   thisSubmissionsAnswers); 

  return thisSubmissionsAnswers;
};

CodePudding user response:

You can used the method getTitle() to retrieve the question text

Sample:

...
thisQuestion = allQuestions[i];
var questionTitle = thisQuestion.getItem().getTitle();
console.log("question: "   questionTitle);
if(questionTitle == "Name" || questionTitle == "Address" || questionTitle == "Mobile"){
  ...
  thisAnswer = thisQuestion.getResponse();//Get the answer
  Logger.log(i   " - "   thisAnswer); 
  thisSubmissionsAnswers.push(thisAnswer);//add answer to the array
  ...
}
...
  • Related