Date Validation in google forms with App Script

Certain scripts require that you use the question's ID of the question you are targeting in your google form. So in cases where you cannot use the index and you are using the ID, the script below can help you print out in the console all the IDs of your questions. Watch the YouTube video to find out how the script was implemented and ran. 

    
  function logQuestionIds() {
    var form = FormApp.openById('your_form_id');
    var items = form.getItems();
    for (var i =   0; i < items.length; i++) {
      var item = items[i];
      Logger.log('Question ID: ' + item.getId());
  }
}
    
    Copy Code
  

See the onFormSubmit() function below that as demonstrated in the YouTube Video was used to remove option items in our questions when they reached the maximum number of responses we set for the options. 

    
function onFormSubmit(e) {
  var form = FormApp.openById('your_form_id'); //Replace with the form ID
  var formResponses = form.getResponses();
  var item = form.getItemById('your_question_id'); // Replace with actual question ID
  var choices = item.asMultipleChoiceItem().getChoices();
  var choiceCounts = {};
  var selectedChoices = [];
  var remainingChoices = [];

  // Initialize choiceCounts with   0 for each choice
  choices.forEach(function(choice, index) {
    choiceCounts[choice.getValue()] =   0;
    remainingChoices.push(choice.getValue()); // Initialize remainingChoices with all choices
  });

  // Count the number of times each choice is selected
  formResponses.forEach(function(response) {
    var answer = response.getItemResponses().filter(function(itemResponse) {
      return itemResponse.getItem().getId() === item.getId();
    })[0].getResponse();

    choiceCounts[answer]++;
    var index = choices.findIndex(choice => choice.getValue() === answer);
    selectedChoices.push({ choice: answer, index: index, count: choiceCounts[answer] });

    // Check if count reaches 3, or your required number and move choice to a separate array
    if (choiceCounts[answer] ===   3) {
      remainingChoices.splice(remainingChoices.indexOf(answer),   1); // Remove choice from remainingChoices
    }
  });

  // Filter out choices with counts less than   3
  var newChoices = remainingChoices.map(choice => choices.find(c => c.getValue() === choice));

  // If no choices remain, set another  choice as a fallback option
  if (newChoices.length ===  0) {
    newChoices = [item.asMultipleChoiceItem().createChoice('Replace with your custom fallback message')];
  }

  // Set the new choices for the multiple-choice question
  // This should be done outside of the loop that processes the responses
  item.asMultipleChoiceItem().setChoices(newChoices);
}
    
    Copy Code
  

Make sure you  have linked your form to a sheet before you deploy it for respondents to make their inputs. The video didn't capture this requirement. As the question's options gets repopulated, the people who selected dates earlier have their responses over written in the google form responses section. If it is linked to a sheet, the correct responses for all respondents will be well captured. 

Boton Flotante - WhatsApp