Advanced · Lesson 4 · Connect
Connect with Apps Script: make an online quiz that records scores in a spreadsheet automatically
Complete the foundation in Lessons 1–3 before connecting data and deploying a web app. See the full learning path
① First, understand the three parts
About 5 minutes
| Part | What it is | What it does |
|---|---|---|
| Google Sheets | The database | Stores the questions, answers and scores |
| Code.gs | The back-end code | Fetches the questions from the spreadsheet and writes the scores back to it |
| index.html | The front-end web page | What students see |
You will not write code line by line in this lesson. Gemini writes the code; your job is to explain clearly what you want, then paste the code into the right places.
② Steps 1 and 2: set up the question bank and enter questions
About 7 minutes
- Create a blank spreadsheet: type
sheets.newin the address bar. The new spreadsheet is saved in your Google Drive; use this spreadsheet from now on. - Rename the tab at the bottom left (usually 'Sheet1') to
Questions: double-click the tab name, type Questions and press Enter. The code finds the data by this name, so if the name is different, it will not find anything. - Copy the header row below, click cell A1 and paste it. The seven headings fill columns A to G.
- Below the header row, add five questions. We suggest simple arithmetic to start with, because what you are practising now is the process, not question writing.
Question Option A Option B Option C Option D Answer Explanation
| Column | What to enter | Example |
|---|---|---|
| A · Question | The full question | 7 + 5 = ? |
| B–E · Option A–D | Four options | 10 / 11 / 12 / 13 |
| F · Answer | Only one capital letter: A, B, C or D | C |
| G · Explanation | An explanation for students who get it wrong; can be left blank | 7 plus 5 equals 12. |
The most common mistake: Putting the full option in column F (for example, '12'). Column F may only contain one of the letters A, B, C or D.
The 'Questions' tab of your spreadsheet has 5 questions. This is the first result of this lesson.
③ Step 3: generate the code with Gemini
About 8 minutes
- Open gemini.google.com. Below the text box, click 'Add files' (the + button) and choose 'Canvas' (on some accounts it may still be under a 'Tools' button).
- Copy the whole prompt below, change [Secondary / Primary] to your students' level, then send it.
I want to make a simple online quiz system with Google Apps Script. Please write the code for me. Data source: I have a Google Sheets spreadsheet with a sheet named 'Questions'. Row 1 is the header row. Columns A to G are, in order: Column A: Question Column B: Option A Column C: Option B Column D: Option C Column E: Option D Column F: Answer (always exactly one letter: A, B, C or D) Column G: Explanation (may be blank) The questions start in row 2, and the number of questions can vary. What it should do: 1. When students open the web page, they first enter their class (e.g. 3A) and class number (e.g. 12), then click 'Start quiz'. Do not ask students for their names. 2. The system reads all the questions from the 'Questions' sheet and shows the four options for each question as radio buttons. 3. When a student clicks 'Submit': - mark the answers straight away, showing correct answers in green and wrong answers in red; - for each wrong answer, show the correct answer, plus the explanation if column G has one; - show the total score at the bottom of the page, e.g. '3 / 5'. 4. After submission, write the score to a sheet named 'Results' in the same spreadsheet, with these columns in order: Time, Class-No (e.g. 3A-12), Score, Total. If the 'Results' sheet does not exist, create it automatically and add a header row. Technical requirements: - Give me two files: Code.gs (back end) and index.html (front end). - The front end calls the back-end functions with google.script.run. - Do not use any external libraries or CDNs; put all the CSS and JavaScript inside index.html. - Important: options read from the spreadsheet may be numbers (e.g. 12). Before comparing answers, convert both the options and the correct answer to text (String); otherwise every numeric question will be marked wrong. - Keep the interface clean and simple, suitable for [Secondary / Primary] students, with all text in British English. - Add comments in English to the code, so that I can understand what each part does.
The most useful thing to learn from this prompt: it spells out exactly what the sheet is called and what goes in each column. If you want AI to write code for you, you must first explain clearly how the data is laid out.
Gemini gives you two pieces of code: one is Code.gs and the other is index.html.
④ Step 4: paste the code into Apps Script
About 8 minutes
- Go back to your question bank spreadsheet and choose 'Extensions' → 'Apps Script' from the menu. A new tab opens.
- On the left is the default script file, usually called
Code.gs. Delete everything in it and paste the Code.gs that Gemini gave you. - Click the + next to 'Files' on the left, choose 'HTML', and enter the file name
index(do not type .html; it is added automatically). - Delete the existing content of the new file and paste the index.html that Gemini gave you.
- Click the save icon at the top (or press Ctrl+S / ⌘+S).
The HTML file must be called index: If the name is wrong, the web page opens blank. This is one of the most common problems.
There are two files on the left, Code.gs and index.html, and both are saved.
⑤ Step 5: deploy it as a web app
About 10 minutes
- At the top right of Apps Script, click 'Deploy' → 'New deployment'.
- Next to 'Select type', click the gear icon and choose 'Web app'.
- Two important settings: set Execute as to 'Me' (the app runs as you), so that students do not need access to the spreadsheet; set Who has access to 'Anyone' (in some versions it reads 'Anyone, even anonymous'), so that students can answer without signing in to Google.
- Click 'Deploy'. The first time, Google asks you to authorise the app: choose your Google account → 'Advanced' → 'Go to … (unsafe)' (… is your project's name) → 'Allow' (the last button may read 'Continue' instead).
- When it is done, you get the web app URL (under 'Web app'). This is the link for your students.
The 'Go to … (unsafe)' warning is normal: you have just written this program yourself and Google has not reviewed it, so the warning appears. You are giving permission to your own program.
If you change the code later, choose 'Deploy' → 'Manage deployments' → 'Edit' (the pencil icon) → set 'Version' to 'New version' → 'Deploy'. The URL stays the same. Saving alone does not update the web page.
You have a web app URL.
⑥ Step 6: try it yourself
About 5 minutes
- Open your link on your phone, enter a class and class number, and do the quiz once.
- Go back to the spreadsheet and check whether a 'Results' tab has appeared.
- Try the demo quiz once and compare it with your version: the screens, the marking and the score record should be similar. (The demo is in Chinese and asks for a name – you can enter a code.)
A 'Results' tab has appeared in the spreadsheet, with the score you just recorded. To change the questions later, just add or remove rows in the spreadsheet; you do not need to change the code.
⑦ Before you give it to students
About 4 minutes
This simplified version sends the correct answers to the student's browser together with the questions. Students who know how to view the page source can see the answers. To use it for formal assessment, it must be changed so that the server does the marking, i.e. the answers are never sent to the browser.
Also: the 'Results' tab stores only the class and class number, not names. But a class and class number can still identify a student, so do not share the spreadsheet with students or anyone else.
⑧ Not working? Start here
| What you see | The most likely cause |
|---|---|
| The web page is blank | The HTML file is not called index. Rename the file in Apps Script, then deploy again. |
| An error such as 'cannot find the sheet' | The spreadsheet tab is not called 'Questions'. Rename it to Questions. |
| The questions appear, but every answer is marked wrong | Column F contains the option itself instead of the letter A, B, C or D. |
| Every arithmetic question is marked wrong | Numbers in the spreadsheet are read back as numbers, which do not equal the text on the web page. The generated code must convert them with String() before comparing; if it does not, use the fallback code below. |
| I changed the code, but the web page has not changed | You need to deploy again (see Step 5); saving alone does not update it. |
| Students say the link does not open | 'Who has access' was not set to 'Anyone' when you deployed. Edit the deployment settings again. |
| There is no 'Results' tab | The code creates this tab automatically only after a quiz has been submitted successfully once. |
| My school account has no 'Anyone' option | Your school administrator has restricted sharing outside the school. Deploy with a personal Google account instead, or ask your school administrator. |
| Students get an error or cannot submit | The student is signed in to several Google accounts at the same time (an official known limitation: Google says this is not supported for web apps). Ask the student to use an Incognito window, or to sign in to only one account, and try again. |
| None of the above, and it still does not work | Replace both files with the fallback code below, then deploy again. |
Fallback code
Please try the version you generated first: the point of this lesson is learning how to get AI to write code for you, and this is only a safety net. This code does the same things that the prompt above asks for.
Show Code.gs (back end)
Show index.html (front end)
More ways to use it
Secondary example: S3 English vocabulary
| Question | Options A–D | Answer |
|---|---|---|
| "reluctant" means… | unwilling / excited / tired / careful | A |
Primary example: P4 English collective nouns
| Question | Options A–D | Answer |
|---|---|---|
| a ( ) of fish | school / herd / flock / pack | A |
The same three parts can be extended step by step
- Add sign-in: add a 'Students' sheet, so that students sign in before they answer.
- Add a mistake log: record the questions each student got wrong, so that they can later redo only those questions.
- Email score reports automatically: after submission, use MailApp to email the score and an analysis of the wrong answers to the student (you would need to record students' email addresses separately, so think about privacy first).
- Teacher dashboard: work out the percentage of the class who answered each question correctly, and see which wrong option many students chose.
Summary
You have completed the first advanced lesson: connecting an interactive web page to a spreadsheet. Find more prompts in the Prompt library.
Lesson 5, Workspace Studio, and Lesson 6, Gemini Tasks, are in development. See what is planned.
Want to learn more?
Watch related videos on the Desmond EdTech teaching channel (in Cantonese)
