Search code examples
google-apps-scriptgoogle-sheetsgoogle-forms

Google Spreadsheet to Google form - Create Answers key


function getDataFromGoogleSheets() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('test1');
  const [header, ...data] = sheet.getDataRange().getDisplayValues();
  const choices = {};
 
  header.forEach(function(title, index) {
    choices[title] = data.map(row => row[index]).filter(e => e !== '');
  });

  //Logger.log(choices);
  //Logger.log(TitleA);

  return choices;
}

function myFunction1() {
  const choices = getDataFromGoogleSheets();
  
  var form = FormApp.create('New Form');

  for (var i in choices) {
    form.addMultipleChoiceItem()
      // I was going to write the if function with "i = i +1" here.
      .setTitle(i) 
      .setChoiceValues(choices[i]) // .setChoiceValues(choices[i], true) --- All answer Check;;;
      .showOtherOption(true);
  }
}

With all the materials produced in VBA, I am thinking of aligning the answer to A in column 1 and randomly turning it into a form in Google Script.


[Add - 230710]

enter image description here

enter image description here

  1. Title : 1. Find food?
  2. Question : a. cat, b. dog, c. pizza(red, Answer) d. clothes, e. tire

Google Spread Sheet to Google Form

  1. Title and Question = Okay
  2. I want to put in the answer key, but I don't know how to make it.

Solution

  • Although I'm not sure whether I could correctly understand your question, how about the following sample script?

    Sample script:

    Before you use this script, please set correctColor. From your showing Spreadsheet image, I guessed it might be #ff0000. So, please confirm it and modify it.

    function getDataFromGoogleSheets() {
      const correctColor = "#ff0000"; // Please set this for your actual situation.
    
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const sheet = ss.getSheetByName('test1');
      const range = sheet.getDataRange();
      const ar = range.getDisplayValues();
      const choices = ar[0].map((_, col) => ar.map((row) => row[col])).map(([h, ...v]) => [h, v]);
      const correct = range.getTextStyles().reduce((ar, r, i) => {
        r.forEach((c, j) => {
          if (c.getForegroundColorObject().asRgbColor().asHexString() == correctColor) {
            ar.push([i - 1, j]);
          }
        });
        return ar;
      }, []);
      return { choices, correct };
    }
    
    function myFunction1() {
      const { choices, correct } = getDataFromGoogleSheets();
      const form = FormApp.create('New Form');
      form.setIsQuiz(true);
      choices.forEach(([h, v], j) => {
        const q = form.addMultipleChoiceItem().setTitle(h).showOtherOption(true);
        const cv = v.map((e, i) => q.createChoice(e, correct[j][0] == i ? true : false));
        q.setChoices(cv);
      });
    }
    

    Testing:

    When this script is run using your showing Spreadsheet, a new Google Form is created as follows.

    enter image description here