Objectsheet Tutorial

2a. Referring to Cells in Other Rows

basics : cell references and formulas : referring to cells in other rows : independent variables : cell formatting : cell styling : cell display : scripts and HTML : conclusion (objectsheet home)

There are different ways to refer to cells elsewhere on the sheet. Keep in mind that the objectsheet disposes with the strict cartesian (A1, B2) referencing scheme of the traditional spreadsheet. In the objectsheet, we reference cells by first choosing a row (or "object"), then selecting a column (or "property of the object"). The result takes the form object.property.

Object calculations in the objectsheet are done in the context of "the current row". If you refer to the another cell on the current row as we did in the last section, the object is thus implied and the property name by itself suffices. For referring to cells in other rows (ie. other objects), we must include a reference to that row.

The us[] Array

The first way to refer to other rows on a sheet is with the us[] array. Each element in the us[] array corresponds to a row (object) in the objectsheet, the array index corresponding to the row number. To illustrate, let's start with last section's sheet:

To use us[], we pick an array index corresponding to the desired row and then pick off a property using dot notation. Remember, anywhere we can enter a literal number or text, we can enter a formula by preceeding it with an "=" (in this case, we'll use "=us[0].day + 2"). The objectsheet prefixes the formula with the calculated answer followed by ":". Below, we have filled in row index 2 with such a formula:

This formula grabs the object at row 0 (us[0]) and then picks off the value of its day property (0.25) and adds 2 to it.

For relative referencing, we use the index variable. During sheet calculation, this variable holds the number of the row currently being calculated. We can therefore create relative row offsets with a formula like =us[index-1].day+2; see below:

Here, us[index-1] refers to "the row with index one less than this row", which in this case is row index 1.

The prev Object

Actually, since references to "the row just above this one" are so common that the objectsheet has a internal shortcut object called prev. It always refers to "the previous row". The following formula in row 2 is equivalent to the one above.

So referring to any place on the objectsheet is simple. The us[] array along with the index variable lets you refer to any row on the sheet. The prev object can be used to refer to the previous row. And there are also other ways to refer to "other rows". You can also give rows explicit names and refer to rows with those names. This is described in the reference, but not in this tutorial.

You Javascript wizards will recognize that you can likewise refer to properties indirectly with by switching from dot notation (prev.day, above) to array notation (prev["day"]). Here, "day" is just a string and can be replaced with an arbitrary expression.


next: independent variables

rk-21 aug 01