Spreadsheets
Overview
A spreadsheet shows data in a grid of rows and columns, like a table in a book or a sheet in a spreadsheet app. Each box in the grid is a cell. Spreadsheets are a good fit for data where every row has the same kinds of facts, such as survey answers, sports statistics or sales. Real data is often messy: the same answer is typed in different ways, some answers are missing, and some have extra spaces. In this tutorial, you will load some practice survey answers into a spreadsheet on a page, move around it with the keyboard, clean up its messy values by hand and with code, and save them to a file.
Goals
You have the following goals for this tutorial:
By the end of this lesson, you can
- Add a spreadsheet to a page and load CSV data into it
- Move around, select and edit cells with the keyboard and the mouse
- Read and change cells from code to clean up text data
- Find and replace text, undo changes, and save a file
- Compare how cleaning choices change results, and summarize a cleanup
Warm up
Think about a table you have used, such as a class schedule, a sports standings table or a bus timetable. How do you find one value in it? You probably go across a row and down a column until they meet. Now imagine the table was filled in by many different people. One person writes "ninth", another writes "9", and another leaves the box empty. What problems would that cause if you wanted to count how many people are in each grade? What could you do about the empty box?
Vocabulary
You will be learning about the following vocabulary words:
| Term | Definition |
|---|---|
| Spreadsheet | A control that shows data in a grid of rows and columns. |
| Cell | One box in a spreadsheet, where a row and a column meet. |
| Cell name | The column letter and row number of a cell, such as C2 for the third column of the second row. |
| CSV file | A comma separated value file: a text file with one row per line and a comma between the values in a row. |
| Text value | A value made of characters. In a Quorum spreadsheet, every cell holds text, even when it seems to be a number. |
| Clean data | Data where the same thing is always written the same way, without extra spaces, typing mistakes or unexplained gaps. |
| Undo | To take back the last change. Redo puts it back again. |
New Code to Learn
You will be using the following new pieces of code:
| Quorum Code | Code Example | Explanation |
|---|---|---|
| AddSpreadsheet(text name) | Spreadsheet sheet = page:AddSpreadsheet("Club survey") | Adds an empty spreadsheet to a page. The name is what a screen reader calls the spreadsheet. |
| LoadFromCommaSeparatedValue(text value) | sheet:LoadFromCommaSeparatedValue(survey) | Fills the spreadsheet from text in CSV form: one row per line and commas between the values. |
| GetSpreadsheet(text name) | Spreadsheet sheet = page:GetSpreadsheet("Club survey") | Finds a spreadsheet on a page by its name. |
| GetRowSize() | integer rows = sheet:GetRowSize() | Returns how many rows the spreadsheet has. |
| GetSize() | integer columns = sheet:GetSize() | Returns how many columns the spreadsheet has. |
| GetText(integer row, integer column) | text name = sheet:GetText(1, 0) | Returns the text in a cell. Rows and columns are counted from 0 in code. |
| SetText(integer row, integer column, text value) | sheet:SetText(1, 1, "9") | Changes the text in a cell. |
| GetCellName(integer row, integer column) | text place = sheet:GetCellName(1, 2) | Returns the name a person uses for a cell, such as C2. |
| AddColumn(integer index) | sheet:AddColumn(3) | Adds an empty column at a position. Columns to its right move over. |
| Find(text value, boolean matchCase, boolean wholeCell, boolean forward) | boolean found = sheet:Find("art", false, true, true) | Moves to the next cell that holds the text. Returns true if it found one. |
| ReplaceAll(text value, text replacement, boolean matchCase, boolean wholeCell) | integer count = sheet:ReplaceAll("ninth", "9", false, true) | Replaces the text in every matching cell and returns how many cells changed. |
| Save(File file) | sheet:Save(file) | Writes the spreadsheet to a CSV file. In a browser, the file is downloaded. |
CSTA Standards
This tutorial covers the following CSTA standards:
For teachers · 2026 CSTA K–12 Computer Science Standards
High School · Data & Analysis
Use a computational tool to clean and organize text-based data.
Only this lesson so far
Middle School · Data & Analysis
Analyze options to address a data quality issue.
Also in 2 other lessons
Middle School · Data & Analysis
Analyze how decisions made at different stages of working with data can lead to biased data, misleading conclusions, and compromised AI models.
Only this lesson so far
Middle School · Data & Analysis
Summarize a data investigation process, including potential biases, limitations, and supporting evidence.
Only this lesson so far
Middle School · Computing & Society
Analyze intended and unintended impacts of a historical computing technology on society and the environment.
Only this lesson so far
High School · Data & Analysis
Create a data dictionary that describes the name, type, and allowable values for each attribute and the logical relationships between variables in a dataset.
Also in 2 other lessons
Explore
Coding a spreadsheet
A Spreadsheet shows rows of text in a grid. Across the top are
column letters: A, B, C and so on. Down the left side are row numbers,
starting at 1. Together, a letter and a number name a cell, such as C2.
Two things are different from the spreadsheet apps you may know:
- Every value is text. A cell holding 10 holds the characters "1" and "0", not a number. There are no formulas, no sorting, no number types and no charts. A Quorum spreadsheet is for exploring data, organizing it and fixing it.
- Code counts from 0. In your program, the first row is row 0 and the
first column is column 0. So the cell a person knows as C2 is row 1,
column 2 in code. The action
GetCellNameturns one into the other for you.
A spreadsheet reads and writes CSV files. In a browser, writing a file downloads it to your computer. A CSV file is plain text, with one row per line and commas between the values. Most spreadsheet apps can save a CSV file, so it's an easy way to move data between programs.
A short history of spreadsheets
VisiCalc, released in 1979 by a company named Software Arts, was the first spreadsheet program for personal computers. Dan Bricklin built a first version while studying business at Harvard, and Bob Frankston joined him to write it for the Apple II computer. It became a "killer app": many people bought an Apple II just to run VisiCalc, and spreadsheets from Lotus, Microsoft and others followed (source).
Think about the impacts of that one program. What do you think people hoped it would do for them? What changed that no one may have planned, such as who started buying computers, which jobs changed, or how many important decisions now rest on data typed into a grid by hand?
The practice data
The data on this page is made up for practice. It is not a real survey. Imagine that six students filled in a form for a school club fair, and the answers were saved as CSV text. Your program will hold this text itself, so it runs anywhere, including in a web browser:
Name,Grade,Club
Ana,9,Chess
Ben,ninth,chess
Cai, 10,Robotics
Dee,10,robotics
Eli,,Art
Fay,11,ART
Examine it closely. This small file already has several data quality problems:
- Ben's grade is written as a word, "ninth", and everyone else's is a number.
- Cai's grade has a space before the 10, and Dee's club has a space after "robotics".
- The clubs are written with different capital letters: Chess and chess, ART and Art.
- Eli's grade is missing.
Loading the data on a page
Replace the code in Main.quorum with this program:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Spreadsheet
class Main is Form
action Main
Display()
end
action CreateGame
Page page = GetMainPage()
page:SetTitle("Club Survey")
text survey = "Name,Grade,Club
Ana,9,Chess
Ben,ninth,chess
Cai, 10,Robotics
Dee,10,robotics
Eli,,Art
Fay,11,ART"
Spreadsheet sheet = page:AddSpreadsheet("Club survey")
sheet:LoadFromCommaSeparatedValue(survey)
sheet:SetDescription("Practice survey answers. One row for each student, with a name, a grade and a club.")
parent:Form:CreateGame()
end
end
This program is built the same way as the last program in the Trees tutorial:
class Main is Formmakes the program itself a form, andDisplay()opens the window.- A spreadsheet needs the window to be open, so the page is built in
CreateGame, which Quorum calls once the window is open. parent:Form:CreateGame()lets the form finish its own setup: it shows the page and gives the first control the focus.surveyholds the practice data as text, one row per line with commas between the values. A text value can go on for several lines, as long as it starts and ends with a quotation mark.page:AddSpreadsheet("Club survey")adds an empty spreadsheet. The spreadsheet is as wide as the page, with a set height; it scrolls when the data doesn't fit. "Club survey" is the name a screen reader uses for it.LoadFromCommaSeparatedValue(survey)fills the spreadsheet from the text.
Run the program. The spreadsheet shows the six students and the header row, plus some empty rows and columns after them to type into.
Using a spreadsheet with the keyboard
The keys work like the keys in popular spreadsheet apps. On a Mac, use Command wherever this list says Ctrl, except for Ctrl+Space, which is Ctrl on every computer.
Moving around:
- The arrow keys move one cell.
- Ctrl with an arrow key jumps to the edge of the data in that direction.
- Home goes to the start of the row. Ctrl+Home goes to the first cell, and Ctrl+End to the last cell with data.
- Page Up and Page Down move one screen.
- Tab and Shift+Tab move right and left. Enter and Shift+Enter move down and up.
Selecting:
- Shift with an arrow key, Home, Page Up or Page Down makes the selection bigger.
- Shift+Space selects the whole row, Ctrl+Space the whole column, and Ctrl+A everything.
Editing:
- Typing replaces the cell's text with what you type. F2 edits the text that is already there.
- While you edit, Enter, Tab, Shift+Enter and Shift+Tab keep your change and move. Escape cancels it.
- Delete and Backspace clear the selected cells.
- Ctrl+Plus adds rows or columns, and Ctrl+Minus removes them. With whole rows or columns selected, it adds or removes those. With cells selected, a menu asks which.
- Ctrl+C, Ctrl+X and Ctrl+V copy, cut and paste.
- Ctrl+Z undoes. Ctrl+Y or Ctrl+Shift+Z redoes.
- Shift+F10 opens a menu with these commands and more.
With the mouse: drag across cells to select them. Click a column's letter or a row's number to select the whole column or row. Drag the right edge of a column's letter to make it wider or narrower, or double-click the edge to fit the column to its text. Right-click to open the menu. On a Mac, Control-click works too.
Each of these has a keyboard way in the lists above: Shift with an arrow key selects like a drag, Ctrl+Space and Shift+Space select like clicking a letter or a number, and Shift+F10 opens the menu. To fit a column from the keyboard, press Ctrl+Space to select it, press Shift+F10, press Down until "Fit column width" is chosen, and press Enter. Dragging a column to a width of your own choosing has no keyboard way yet.
Try it now. Fix Ben's grade by hand: move to B3, type 9, and press
Enter. Then press Ctrl+Z to undo it, and Ctrl+Y to put it back.
This changes the spreadsheet only while the program is running. In a browser, nothing is written back: run the program again and Ben's grade says "ninth" again, because it comes from the text in your program.
Spreadsheets and screen readers
A screen reader treats the spreadsheet as a table. Moving to a cell reads its text and its name: a cell holding 71 in C2 is read as "71, C2". Selecting a range reads the range, such as "D3 through E5 selected".
Reading cells from code
Your program can read every cell. GetRowSize says how many rows there
are, and GetText(row, column) returns one cell's text. Add this code to
CreateGame, just before parent:Form:CreateGame():
integer row = 1
repeat while row < sheet:GetRowSize()
text name = sheet:GetText(row, 0)
text grade = sheet:GetText(row, 1)
text place = sheet:GetCellName(row, 1)
output name + " is in grade " + grade + " (cell " + place + ")"
row = row + 1
end
The loop starts at row 1 to skip the header row. Run the program. The
output shows a line for each student, such as "Ben is in grade ninth
(cell B3)". Find the line for Eli. What does GetText return for a cell
with nothing in it?
Cleaning the data with code
Fixing six rows by hand is fine. Fixing six thousand is not. Code can do the same fixes on every row. This version adds a Clean Up button that:
- removes spaces from the start and end of every grade and club, with
Trim; - writes every club in small letters, with
ToLowerCase, so Chess, chess and CHESS all become chess; - replaces every cell that says exactly "ninth" with "9", with
ReplaceAll.
First, add a new class to the project named CleanBehavior.quorum. It
comes from FormBehavior, which gives it the GetPage action, so it can
find the spreadsheet by its name with GetSpreadsheet:
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Spreadsheet
class CleanBehavior is FormBehavior
action Run(BehaviorEvent event)
Page page = GetPage()
Spreadsheet sheet = page:GetSpreadsheet("Club survey")
if sheet = undefined
return now
end
integer row = 1
repeat while row < sheet:GetRowSize()
text grade = sheet:GetText(row, 1)
sheet:SetText(row, 1, grade:Trim())
text club = sheet:GetText(row, 2)
club = club:Trim()
sheet:SetText(row, 2, club:ToLowerCase())
row = row + 1
end
integer count = sheet:ReplaceAll("ninth", "9", false, true)
output "Replaced ninth in " + count + " cells."
end
end
Then add a class named SaveBehavior.quorum. Saving writes a file, so it
works on a computer and in a browser. In a browser, the file is
downloaded, as files from a web page usually are:
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Spreadsheet
use Libraries.System.File
class SaveBehavior is FormBehavior
// In a browser, saving downloads the file.
action Run(BehaviorEvent event)
Page page = GetPage()
Spreadsheet sheet = page:GetSpreadsheet("Club survey")
if sheet = undefined
return now
end
File file
file:SetPath("Survey.csv")
sheet:Save(file)
output "Saved the survey."
end
end
Last, change Main.quorum to this:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Button
use Libraries.Interface.Controls.Spreadsheet
class Main is Form
action Main
Display()
end
action CreateGame
Page page = GetMainPage()
page:SetTitle("Club Survey")
Button clean = page:AddButton("Clean Up")
clean:SetDescription("Removes extra spaces, writes clubs in small letters, and changes ninth to 9")
Button save = page:AddButton("Save")
save:SetDescription("Saves the survey to Survey.csv. In a browser, the file is downloaded")
text survey = "Name,Grade,Club
Ana,9,Chess
Ben,ninth,chess
Cai, 10,Robotics
Dee,10,robotics
Eli,,Art
Fay,11,ART"
Spreadsheet sheet = page:AddSpreadsheet("Club survey")
sheet:LoadFromCommaSeparatedValue(survey)
sheet:SetDescription("Practice survey answers. One row for each student, with a name, a grade and a club.")
CleanBehavior cleaner
clean:SetBehavior(cleaner)
SaveBehavior saver
save:SetBehavior(saver)
parent:Form:CreateGame()
end
end
Here is what is new:
TrimandToLowerCaseare actions on text. Each returns a new text, so the code stores the result and hands it toSetText.- In
ReplaceAll("ninth", "9", false, true),falsemeans capital letters don't matter, so "Ninth" would match too.truemeans only cells whose whole text is "ninth" change, so a club named "ninth grade band" would be left alone. Save(file)writes the spreadsheet toSurvey.csv. Values you didn't change are written exactly as they were. On a computer, the file is saved in your program's folder. In a browser, it is downloaded.
Run the program and press Clean Up. Notice how the clubs and grades change.
Then press Ctrl+Z in the spreadsheet. The "ninth" comes back, because
ReplaceAll can be undone. The SetText changes stay: changes your
program makes with SetText can't be undone. Changes a person makes, and
Replace and ReplaceAll, can.
The Clean Up button saves nothing. When you are happy with the data, press Save. To start over with the original data, run the program again.
What about the missing grade?
Code can't fix everything. Eli's grade is empty, and no program can know what it should be. You have a few options, and each has a cost:
- Leave it empty. Anyone counting grades must remember to skip it.
- Write a word such as "unknown", so everyone knows it is missing on purpose.
- Ask Eli, and type in the real answer.
- Remove Eli's row. Then Eli's club choice is lost too.
To check a value across the whole spreadsheet, use Find. It moves to
the next cell holding the text and returns true if it found one. It
doesn't find empty cells, so search for those with a loop and GetText, as
in "Reading cells from code".
boolean found = sheet:Find("art", false, true, true)
The last true means search forward. Use false to search back.
Different choices, different results
Every choice you make while cleaning data changes what the data says. Here is the practice survey after Clean Up, with each club counted:
- chess: Ana and Ben, 2 people
- robotics: Cai and Dee, 2 people
- art: Eli and Fay, 2 people
Now suppose someone removes Eli's row because the grade is missing. Art
drops to 1 person, and a report might say "art is the least popular club."
That conclusion comes from a cleaning choice, not from the students. Other
choices work the same way. If one student had written "9th" instead of
"ninth", the ReplaceAll on this page would miss it, and grade 9 would
seem smaller than it is. Choices like these can make data biased: some
people count less than others, without anyone meaning it. The same is true
when data is used to train an AI model: the model learns from the data as
it was cleaned, mistakes and all.
That's why careful data workers keep a short record of their cleanup:
- what they changed, and how many cells changed (
ReplaceAllreturns the count); - what they couldn't fix, such as missing answers, and what they did about each;
- what the data can't tell you because of those choices.
A record like this lets someone else check the work, or make a different choice.
More things a program can do
A few more actions are useful when you organize data:
sheet:AddColumn(3)
sheet:SetText(0, 3, "Checked")
sheet:FitColumnWidth(2)
AddColumn(3)adds an empty column at column 3, which is column D.AddRow(index)does the same for rows, andRemoveColumnAtandRemoveRowAttake them out. These four also clear the undo history.SetText(0, 3, "Checked")gives the new column a header.FitColumnWidth(2)makes column C as wide as its longest text.
Engage
The goal of this activity is to discover how two people's cleaning choices change what the same data says, and to summarize your cleanup. With a partner, write 8 to 10 rows of made-up CSV data about something your class could survey, such as favorite lunch, number of pets, or how you get to school. On purpose, make it messy the way real data is: write some answers in two different ways, add a few extra spaces, use different capital letters, leave two answers blank, and include one answer that could be read more than one way, such as "2 or 3" for number of pets.
Next, each of you cleans your own copy of the data, without talking about
your choices. Put it in the spreadsheet program from this page in place of
the survey text, fix some problems by hand, and change CleanBehavior to
fix the rest with code, such as a ReplaceAll for each answer that was
written two ways. Remember that changes in a running program are not
written back in a browser, so write down your hand fixes as you make them.
Then write a short cleanup record, as in "Different choices,
different results": what you changed, what you couldn't fix and what you
did about it, and one thing your clean data can't tell you. Add a short
data dictionary to it: for each column, its name, the kind of value it
holds (such as a whole number or a word) and the values it may have, such
as grades 9 to 12.
Last, compare. For each answer, count how many people chose it in your file and in your partner's copy. Where the counts differ, find the choice that caused it. Together, write two or three sentences: which conclusion about your class would change depending on whose copy you used, and which copy you would trust more, and why?
Wrap up
Cleaning data takes up a large part of real data work: as one researcher put it, "A huge amount of effort is spent cleaning data to get it ready for analysis" (Hadley Wickham, "Tidy Data," Journal of Statistical Software, 2014, source). Every choice you make changes what the data can tell you. Think about the missing grade. Which option did you pick, and who could be affected by that choice? Why do you think the spreadsheet keeps every value as text, instead of guessing which values are numbers?