Search code examples
google-apps-scriptgoogle-forms

How can i change dynamically the name of a question through google sheets using app script?


I am trying to make a form where my partner can write a question on the google sheets and then the question automatically changes on the form function updateForm() {

function updateForm() {

  //pergunta1
  var ss = SpreadsheetApp.getActive();
  var setupSheet = ss.getSheetByName("Perguntas/Respostas");
  var langVals = setupSheet.getRange(2,2,setupSheet.getLastRow()-7,1).getValues();
  Logger.log(langVals);
  var form = FormApp.openById("1-_7qJ7W89pQ9yE6EN8HuePiegL1FdQcv6qwyBoB_igo");
  var langsMultipleChoiceQuestion = form.getItemById("611392210").asMultipleChoiceItem();
  langsMultipleChoiceQuestion.setChoiceValues(langVals);

//Pergunta2
  var langVals = setupSheet.getRange(2,3,setupSheet.getLastRow()-7,1).getValues();
  Logger.log(langVals);
  var form = FormApp.openById("1-_7qJ7W89pQ9yE6EN8HuePiegL1FdQcv6qwyBoB_igo");
  var langsMultipleChoiceQuestion = form.getItemById("1744979888").asMultipleChoiceItem();
  langsMultipleChoiceQuestion.setChoiceValues(langVals);

}

Solution

  • To update the title of an existing question you need to use the method setTitle(), as specified here. E.g.

    form.getItemById("1744979888").setTitle("This is the new question");

    You can also update the question with contents of a specific cell in your spreadsheet, for example with the content of the cell A1 in your sheet:

    form.getItemById("1744979888").setTitle(setupSheet.getRange("A1").getValue());