My Google Apps Script Works Perfectly with Only One Exception

86 views
Skip to first unread message

Robert Mcleod

unread,
Jun 5, 2025, 11:08:31 AMJun 5
to Google Apps Script Community
Hi...I have finally got the following Apps Script code to work. However, I also want it to automatically transfer the active spreadsheet name to the new Google Form file name and Form title in order to avoid manually replacing 'Quiz Form' in Line 10 every time I run the code:


function createQuizFromSheet() {
  // Get the active spreadsheet and sheet
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();

  // Get all the data from the sheet, skipping the header row
  const data = sheet.getDataRange().getValues().slice(1);

  // Create a new Google Form
  const form = FormApp.create('Quiz Form');
  form.setIsQuiz(true); // Turn the form into a quiz

  // Loop through each row of data (each question)
  data.forEach(row => {
    // Extract question details from the row
    const questionTitle = row[0];
    const choices = row.slice(1, 5); // Choices A-D
    const correctAnswerLetter = row[5];
    const points = row[6];

     // Convert the letter to index for correct choice
    let correctAnswerIndex = 0;
    if (correctAnswerLetter === "B") correctAnswerIndex = 1;
    if (correctAnswerLetter === "C") correctAnswerIndex = 2;
    if (correctAnswerLetter === "D") correctAnswerIndex = 3;


    // Add multiple choice question to the form
    const item = form.addMultipleChoiceItem()
      .setTitle(questionTitle);

     // Create choices, mark the correct one and set points
    const choiceItems = [];
    for(let i = 0; i< choices.length; i++){
      if (choices[i]){ //only create if the choice is not blank
        choiceItems.push(item.createChoice(choices[i], i === correctAnswerIndex));
      }
    }
    item.setChoices(choiceItems);
    item.setPoints(points);
  });


  // Get and log the form's published URL
  const formUrl = form.getPublishedUrl();
  Logger.log('Form URL: ' + formUrl);

  // Optional: Display form URL in the spreadsheet
  // sheet.getRange(1, 8).setValue('Form URL'); // Set header
  // sheet.getRange(2, 8).setValue(formUrl); // Output URL
}

AliceKeeler Keeler

unread,
Jun 5, 2025, 11:37:24 AMJun 5
to Google Apps Script Community
I would recommend you don't use 'Quiz Form' in the first place. Above the creating a quiz, grab the name of the spreadsheet and then use that name to create the quiz in the first place. 

Robert Mcleod

unread,
Jun 5, 2025, 1:15:57 PMJun 5
to Google Apps Script Community
Hi Kelly...I don't specifically know the specific location of the script for what you are referencing with "Above the creating a quiz." Furthermore, you stated "grab the name of the spreadsheet and then use that name to create the quiz in the first place." You seem to imply you want me to manually grab the spreadsheet name which is what I don't want to do. I am simply requesting a piece of code that will automatically get the job done.

Robert

Michael O'Shaughnessy

unread,
Jun 5, 2025, 8:35:15 PMJun 5
to google-apps-sc...@googlegroups.com
Hello Rober,

What Alice is saying is to write a line of code to get the sheet name.  So what I suggest is to do the following:
After this line:
const sheet = ss.getActiveSheet(); 
Add this line:
const quizTitle = sheet.getName();

Then replace 'Quiz Form' with you new variable:
  const form = FormApp.create(quizTitle)

Now your sheet can have several tabs with quiz questions.  Then whatever tab you are currently on when you run your script will be the one it uses and will name the form the same as the name of the tab.

Now for your next challenge, add a menu and a custom dialog box to get the name of the quiz from the user!!  This is not difficult to do and you can do it if you have managed to get the code you are working to work!!  To help guide you here is a link to Google's documentation:

Let me know if this works for you!


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/d963c448-de2a-4f4b-916c-9d832f21dba0n%40googlegroups.com.

Robert Mcleod

unread,
Jun 5, 2025, 10:31:42 PMJun 5
to google-apps-sc...@googlegroups.com
Michael...Before I retire for the rest of the evening I want to know the purpose of you suggesting the challenge of adding a menu and a custom dialog box to get the name of the quiz from the user!! Why would I need to get the name of the quiz from the user when in fact I am the person who is administering the quiz? After a day or two of giving a quiz or test, I will simply download the results into a .csv file, and transfer the data into my Excel gradebook.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/jsJ-zED3-eQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/CAHNYQLgEJmW41nD4PNTV4m6ddu2W9%3DWkk5u88UppxC5xH9wj4A%40mail.gmail.com.

TENE FONHOUE FERRY VANECK

unread,
Jun 10, 2025, 9:31:02 AMJun 10
to Google Apps Script Community
Hello All, 

Thank you for accepting me into the group, so I need help.
My goal is to automatically fill out any Google form with first and last name, phone number, city, email, full name, and add a photo and IDcard.

Please need help
Best regards, 

Michael O'Shaughnessy

unread,
Jun 10, 2025, 6:26:28 PMJun 10
to google-apps-sc...@googlegroups.com
My suggestion to add the menu and a dialog is so that you don't have to interact with the code anymore.  You fill out your sheet, hit the menu button and a quiz is created.  By adding a dialog box and using the input you can easily share your "quiz creating spreadsheet" solution with others who know nothing about coding BUT they will be able to use the spreadsheet.

Also, after you have successfully coded your quiz script,  the next logical step to improve your coding skills is to add a menu.

Michael O'Shaughnessy

unread,
Jun 10, 2025, 6:28:20 PMJun 10
to google-apps-sc...@googlegroups.com
We need a little more info here... Automatically fill the form out from where?  With data from a spreadsheet?  IF you want to add your own information you can use an extension like Lastpass to add your info.

So, what is the ultimate goal?

Reply all
Reply to author
Forward
0 new messages