36
Programming Spreadsheets

Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Embed Size (px)

Citation preview

Page 1: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Programming Spreadsheets

Page 2: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

The Original PC “Killer App”• The application that legitimized

the PC as an office tool was thespreadsheet. Previously, PC wereused primarily by hobbyists.

• Inspired by an accountant’sledger metaphor• But, it is much more…

an alternative method ofprogramming a computer

• Data is stored in cells of a table• Computations are specified implicitly using formulas that

relate table cells

Page 3: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Let’s get Started…

• You’ll need a standard spreadsheet to do thenext few in-class exercises

• One alternative is to use Microsoft Excel oriWork Numbers

• Otherwise you can use the following free onehttp://spreadsheet.google.com

– need to create a “gmail” account

Page 4: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Basic Spreadsheet Layout

• Letter-labeledcolumns

• Number-labeledrows

• Matrix or gridof cells

• Cells containdata– Strings– Numbers– Computations

Page 5: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

A Useful Example

• In cell “A2” enter a “1”• In cell “A3” enter

“=A2+1”• Copy the contents

of A3 to cells A4-A25• Type the Label “N” into cell “A1”• Now add labels “64N”, “4N^2”, “16NlogN”,”2^N”,

and “(2N+2)(2N-1)” to cells B1-F1 respectively.

Page 6: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Example Continued

• Next we will fill cells of our spreadsheet with computations• Select cell “B2” (you’ll need to double click on it in most

cases) and type the formula “=64*A2”• Alternative ways of entering formulas

– Instead of typing “A2” as in the example above, you couldinstead “reference” it by typing “=64*” and then selecting(single-clicking on) the desired cell

• Continue with formulas for elements is row 2

Page 7: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Copying Cells

• After you have entered formulas for row 2, copy that rowto rows 3- 25. This can be done by selecting cells B2-F2,copying it using on of– Press “Ctrl-C” on a PC of “command-C” on a Mac– Right-clicking (Ctrl-click on a Mac) and choosing “copy”– Selecting “copy” from the edit menu

• Paste these contents to rows 3-25– “Ctrl-V” (PC) or “command-V” Mac– “Paste” on either “right-click” or “Ctrl-click” context menus– Paste from the “edit” menu

• Many ways to do the same thing

Page 8: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Result

• After Pasting your spreadsheet should look like:

Page 9: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Closer Inspection

• Now select one of the cells you pasted (e.g. B4)• Notice the formula is not exactly as you typed• By default references to cells are relative to the current

cell.• When copying, this “relativeness” is maintained

Page 10: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Absolute Addressing• We’ll modify the formula in column 2 so that the constant

“64” is a user specified variable• First write the string “k =“ into cell H4 and the numerical

constant “60” into cell H5• Now modify cell B2’s formula to read “=H5*A2”• Copy this cell and paste it across the rest of the column.

Is the result what you expected? Why?• Repeat this process with B2’s formula instead as:

“=$H$5*A2”• A “$” before a column’s label means use “absolute”

addressing for the “column-part” of the cell’s address• The “$” before the row’s label similarly specifies an

“absolute” row address

Page 11: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Results

• Now try changing the value in cell H5

Page 12: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Plotting

• In addition to performing calculationsspreadsheets are invaluable for generating quickplots

• Select the contents of cells B2-F12, and thenselect the “Graphing wizard”, from the menu. Thegraphing wizard varies slightly from spreadsheetto spreadsheet.

• Choose a “line plot”, an appropriate title, axislabels, and plot names.

Page 13: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Result

Page 14: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Importing Data into a Spreadsheet

• Often, you’ll want to bring data from othersources into a spreadsheet for analysis

• Most support common data table formatsincluding text files formatted as “commaseparated values” (extension .csv), “tabseparated values” (extension .txt), and HTMLtables.

Page 15: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Exercise

• Download the “NCAA2008.csv” file from thecourse website to your local computer using the“save link” option of your browser

• Open an empty Google spreadsheet(http://docs.google.com) , using theNew|Spreadsheet menu selection

• Within the spreadsheet window choose the“File|Import” option, and then use the dialog toupload the “NCAA2008.csv” file from where yousaved it.

• Select the “Open now>>” link of the dialog

Page 16: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Exercise (cont.)

• At this point, your spreadsheet should look like:

Page 17: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Clean Up

• First, we’ll make a few cosmetic changes byresizing columns to better fit their contents

• To resize a column, you select a border of thecolumn’s label and drag it to expand or reduce its

size (other spreadsheets, ex. Excel, provideformatting options on their menu thatautomatically fit a column or row to its contents)

Page 18: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

More Formatting

• After resizing all columns, we will adjust the table-split bar of the spreadsheet so that the column text labels are always visible

• The “table-split” bar is the fat gray row outlinelocated just below the cell-label row

• Click and drag it as before, but this time place it just below the first row ‘1’

Page 19: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Adding Columns

• Next we’ll add 2 new columns, one before the “W”column, and one after the “L”

• This is done byselecting thecolumn label oneither side of theinsertion andchoosing “column left”or “column right” asappropriate from the “insert” menu.

Page 20: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Derived Data Columns

• Next we’ll label the new column to the left of the W column as GP (for “Games Played”), and the one on the right as Pct (for “Percentage”).

• Add a formula to sum the Wins and Losses in the first row of GP column (ex. =F2+G2), and copy it to the rest of the rows in that column

Page 21: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Formatting Cells

• Repeat this process for the “Pct” using theformula Pct = Wins / GamesPlayed. The resultshould look like:

• You can format thePct column so thatit is 3 digits, bymodifying thefunction to“=TEXT(F2/E3, “#.000”)”

Page 22: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Sorting

• You can use sorting to reorder the rows (teams)in your spreadsheet

• Select the “Pct” column, by clicking on the columnlabel “H”

• Choosethe Sorttab

• SelectZ->A

Page 23: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

More Statistics

• Do the following exercises in class– Add columns for ave. points per game– Ave. opponents points per game– Ave. winning margin– Sort the list by the average winning margin

• There are also fields for each teams record, andpoints scores against other teams in the NCAAtournament. What fraction of teams would youexpect have winning records against other teamsin the tournament?

Page 24: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Spreadsheet Simulations

• Spreadsheets can do much more than add andaverage columns of numbers

• “What if” experiments• Simulating complicated systems• You don’t need complicated equations to

generate unexpected behaviors• As an example, let’s interpret “Cells” literally

Page 25: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Conway’s Game of Life

• In 1970, John H. Conway suggesteda simple model of life in a simplifiedPetri dish

• Assume that cells restricted areto positions in a rectangular grid,and obey the following simple rules1) A cell with fewer than 2 neighbors dies2) A cell with more than 3 neighbors dies3) A new cell is born in an empty box withexactly 3 neighbors

Page 26: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Examples

• Life cases

Page 27: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Spreadsheet Iteration

• You’ll need to use Excel for this example• Normally, a spreadsheet will re-evaluate the

contents of a formula cell when one of it’s inputcells is changed. A single cell might set off manychanges

• This can lead to problems when there is a “circular cell reference”

• Try this:

Page 28: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Turn on Iteration

• On Excel 2007 selectthe Office button and“Excel Options”

• On the resulting menuselect “Formulas”,“Enable iterative calculation”and set the maximum iterations to 1

• On older versions of Excel choose “Tools | Options”and select the “Calculations” tab. Check the“iterations” box, and set maximum iterations to 1.

Page 29: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Test it

• Now go back to our original formula, and press “Ctrl-=“ tosee the effect

• Next, lets modify theformula to:“= IF(B1, 0, A1+1)”

• This formula increments the contents of the cell A1 if thecell B1 is 0, otherwise it is sets A1 to 0

• Then set B1 to “1” and press “Ctrl-=“ a few times• Next fill cells from C3 to Q3 with “0”• Copy rows 3 to rows 4 to 17• Then select all these zeros, and

“Format | Autofit Column Width”

Page 30: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Conditional Formatting

• Copy rows from C3-Q17 to C20, and again to C37• Then select all 3 of

these copied blocksand apply “ConditionalFormatting”

• Choose the“Format only cellsthat contain” option

• Choose the “Cell Value”,“not equal to”, “0”

• Set the font color towhite and fill to red

Page 31: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Initializing the Configuration

• Change some of the cells from ‘0’ to ‘1’• In A2 add the formula “=MOD($A$1,2)”• In C20 define the formula:

“=IF($A$2,B2+C2+D2+D3+D4+C4+B4+B3,C20)”• Copy this formula

across cells C20-Q34• In C3 define the formula:

“ =IF($A$1, C3,C37)”• Copy this formula

across cells C3-Q17

Page 32: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Encoding “Life’s” Rules

• What you have now should copy the image fromthe bottom to the top block if you set B1 to 1

• Then set B1 to “0” and press “Ctrl-=”• The middle block should count the number of

neighbors of each cell of the top block• Now, in cell C3 enter the formula:

“=IF($A$1, IF($A$2, C3,IF(C3,IF(AND(C20>1,C20<4),1,0), IF(C20=3,1,0))),C37)”

• Copy the formula across cells C3-Q17

Page 33: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Playing Life

• Now create a simple 3x3 blockin the lowest area (C37-Q51)

• Enter “1” in B1 (the patternshould now be copied to thetop area)

• Enter “0” in B1 (you shouldsee counts in the middlearea)

• Continually press “Ctrl-=” to see the progressionof you cell colony

Page 34: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Interesting Patterns

• There are particular cell patternswith specific behaviors

• Some move and reconstructthemselves at an offsetposition (gliders)

• Others alternate in a sequence(pulsars)

• You can play and generate yourown patterns

Page 35: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Spreadsheet Summary• A spreadsheet is a simple model of computation with cells

containing data and instructions• Formulas can be referenced that access either specific

cells, or cells with a relative offset• Most spreadsheet include plotting capabilities• Cells and formulas can be rearranged using sorting• Normally, a spreadsheet will continue updating its formula

cells until all are resolved• Alternatively, spreadsheets can be set in a “iterate” mode,

where each formula is evaluated a specific number oftimes.

Page 36: Programming Spreadsheets. The Original PC “Killer App” The application that legitimized the PC as an office tool was the spreadsheet. Previously, PC were

Next Time

• We’ll look at a simple database model usingXML