Home > front end >  Validate form with a row value from spreadsheet before submit
Validate form with a row value from spreadsheet before submit

Time:12-16

I have a Form linked to SpreadSheet and i want to validate the text field "email", and if field exists, go to specific section/page break programatically because Google Forms doesn't give me the option to validate a textfield.

This is a bit of the code i have done until now:

function selectedItems(){

//Select the form and get the items
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length - 1];
var itemResponses = formResponse.getItemResponses();

var name = "";
var email = "";
var gender = "";

//get email field response of the form
for(var i = 0; i < itemResponses.length; i  ){
  switch(itemResponses[i].getItem().getTitle()){
    case "Correo electrónico:": email = itemResponses[i].getResponse();
    break;
    case "Sexo:": gender = itemResponses[i].getResponse();
    break;
    case "Nombre completo:": name = itemResponses[i].getResponse();
    break;
    default:
    break;
  }
}
 
 //get the spreadsheet where the data is stored
 var spreadSheet = SpreadsheetApp.openById(MI FORM ID)
 var spreadSheetData = spreadSheet.getDataRange()
 var rowValue = spreadSheetData.getValues()
 
 //get the sections title... i don't know what to do here
 var sections = form.getItems(FormApp.ItemType.PAGE_BREAK)
 var section = sections[0].getTitle()

 }

Basically:

I need that if mail exists in spreadsheet, instead of continue to the next section in Google Forms, i need that the user go to last page. I don't know if it's possible with a Form made via GForms. Thanks in advance.

CodePudding user response:

There is no way of achieving this as even though you end up validating a text field such that it contains an email address, you cannot redirect the user to another section as there is no method for it.

From the documentation:

createChoice(value, navigationItem) > Creates a new choice with a page-navigation option that jumps to a given page-break item. This is equivalent to createChoice(value, navigationType) with navigationType set to FormApp.PageNavigationType.GO_TO_PAGE. Choices that use page navigation cannot be combined in the same item with choices that do not use page navigation.

However, the above method is specific to the MultipleChoiceItem Class.

A potential workaround is to use a multiple choice item and redirect the user based on their choice - however, this implies that you will end up relying on the user's statement, without any control from your side.

function selectedItems() {
  //other code
  let form = FormApp.getActiveForm();
  let emailQuestion = form.addMultipleChoiceItem()
    .setTitle('Please input your email')

  var validEmailSection = form.addPageBreakItem()
    .setTitle('Yayy! You have a valid email!')
    .setHelpText('Ah bee cee dee');

  emailQuestion.setChoices([
    emailQuestion.createChoice('Yes, I confirm I have a valid email', validEmailSection),
    emailQuestion.createChoice('No, the email is incorrect', FormApp.PageNavigationType.CONTINUE),
  ]);
}

Another option...

File a feature request on Google's Issue Tracker here!

Reference

  • Related