2021年8月24日 星期二

Chapter 13 Adding different types of questions to a form

 

//Create new form - Global variable so it can be seen by all functions

const FORM = FormApp.create("Questionnaire");

 

function makeQuestionnaire() {

//Get data and last row from spreadsheet

  const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

  const data = sh.getDataRange().getValues();

 

 

//Loop through each question and check what question type it is

  data.forEach(checkQuestionType) 

}

 

//function to check question type

function checkQuestionType(data){

  if(data[1] === "text"){ 

    makeTextQ(data);

  }

  else if(data[1] === "date"){ 

    makeDateQ(data);

  }

  else if(data[1] === "duration"){ 

    makeDurationQ(data);

  }

  else if(data[1] === "multiplec"){ 

    makeMultipleCQ(data);

  }

  else if(data[1] === "list"){ 

    makeListQ(data);

  }

  else if(data[1] === "scale"){ 

    makeScaleQ(data);

  }

  else if(data[1] === "checkbox"){ 

    makeCheckboxQ(data);

  }

  else if(data[1] === "grid"){ 

    makeGridQ(data);

  }

  else if(data[1] === "paragraph"){ 

    makeParagraphQ(data);

  }

}

 

////Functions to make different types of questions

//Make text question

function makeTextQ(data){

  let text = data[2];

  let item = FORM.addTextItem();

  item.setTitle(text);

}

 

//Make date question

function makeDateQ(data){

  let date = data[2];

  let item = FORM.addDateItem();

  item.setTitle(date);

  item.setRequired(true);

}

 

//make duration question

function makeDurationQ(data){

  let duration = data[2];

  let item = FORM.addDurationItem();

  item.setTitle(duration);

}

 

//make Multiple-Choice question

function makeMultipleCQ(data){

  let mcQuestion = data[2];

  let item = FORM.addMultipleChoiceItem();

  item.setTitle(mcQuestion);

Logger.log(data);

  //Getting options

  let trimmedData = data.filter(function(str) {

    return /\S/.test(str);

    });

  //Getting options without spaces

  let slicedData = trimmedData.slice(3,trimmedData.length);

 

  //Adding option to Form

  item.setChoiceValues(slicedData);   

}

 

//make List question

function makeListQ(data){

  let listQuestion = data[2];

  let item = FORM.addListItem();

  item.setTitle(listQuestion);

  let trimmedData = data.filter(function(str) {

    return /\S/.test(str);

    });

 

  let slicedData = trimmedData.slice(3,trimmedData.length);

  item.setChoiceValues(slicedData);   

}

 

//make Scale question

function makeScaleQ(data){

  let scaleQ = data[2];

  let item = FORM.addScaleItem();

  item.setTitle(scaleQ);

 

  let trimmedData = data.filter(function(str) {

  return /\S/.test(str);

  });

 

  //Get and set lower and upper bounds of scale

  let lower = trimmedData.slice(3, 4);

  let upper = trimmedData.slice(4, 5);

  item.setBounds(lower, upper);

  //Set labels to lower and upper bounds

  item.setLabels(trimmedData.slice(5, 6), trimmedData.slice(6, 7));  

}

 

//make Checkbox question

function makeCheckboxQ(data){

  let checkboxQ = data[2];

  let item = FORM.addCheckboxItem();

  item.setTitle(checkboxQ);

  let trimmedData = data.filter(function(str) {

    return /\S/.test(str);

    });

  let slicedData = trimmedData.slice(3,trimmedData.length);

   item.setChoiceValues(slicedData);   

}

 

//make Grid question

function makeGridQ(data){

  let gridQ = data[2];

  let item = FORM.addGridItem();

  item.setTitle(gridQ);

 

  let trimmedData = data.filter(function(str) {

    return /\S/.test(str);

    });

   

  //Find where "Columns:" text is in array

  let columnPosition = trimmedData.indexOf("Columns:");

 

  //Get row values for grid

  let slicedDataRows = trimmedData.slice(3,columnPosition); 

  //Get column values for grid

  let slicedDataColumns = trimmedData.slice(columnPosition+1,trimmedData.length);

 

  item.setRows(slicedDataRows); 

  item.setColumns(slicedDataColumns);  

}

 

//make Paragraph question

function makeParagraphQ(data){

  let paragraphText = data[2];

  let item = FORM.addParagraphTextItem();

  item.setTitle(paragraphText);

}

沒有留言:

張貼留言