'How to set event listeners to Google Forms in script

I created a form using Google Forms UI. Now I want to enable some Form Elements based on the data from a Textfield.

function myFunction() {
  var form = FormApp.openById(myformID);
  var formitems = form.getItems()
//display the form elements and ids
  for (var i in formitems) { 
    Logger.log(formitems[i].getTitle() + ': ' + formitems[i].getId());
  }

  var section3= form.getItemById(myformelementid)
  Logger.log(section3.getTitle())

**//I want to add an eventlistener that triggers upon entering text in the textfield, the eventchange function can handle the enabling of other form elements 
  section3.addEventListener("change",onChangeEvent())**
  
}

Any help would be appreciated.



Solution 1:[1]

To do this in App Script (https://script.google.com/), you have to setUptrigger as follows:

function setUpTrigger() {
  const form = FormApp.openById(formId);
  const trigger = ScriptApp.newTrigger("onSubmit")
    .forForm(form)
    .onFormSubmit()
    .create();
}

Then you can define the onSubmit function as follows:

async function onSubmit(e) {
    const eResponse = e.response;
    //get all response as an object
    const response = eResponse.getItemResponses();
    const responseObj = {};
    //get all response as object with key value pair
    for (let i = 0; i < response.length; i++) {
      responseObj[response[i].getItem().getTitle()] = response[i].getResponse();
    }

    const email = eResponse.getRespondentEmail(); //get email address if you're collecting it
    if (email) {
      responseObj.email = email;
    }
  
    console.log("responseObj", responseObj);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 olawalejuwonm