Objectsheet Tutorial

4. The Format Row

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)

Objectsheet cell attributes:

The format row in the objectsheet's template gives us control over what is displayed for a cell value. It allows us to transform the raw, calculated values into the content actually shown in the cell. This includes things like managing decimal places and other number formatting, labeling, etc.

The transformation process in the format row is completely generic; the formula can be anything. But there are a few, important functions that the format row can be used for. For starters, this is where number formatting goes: fixing the number of decimal places, scientific (or engineering) notation, and parenthesizing negative numbers.

Let's start with a new sheet: and populate it as follows:

Remember; to recreate this sheet:

  1. Enter the formula for 'a' and 'b' in the corresponding cell. pow() is the Javascript function that raises to a power (b = a5 in this case)
  2. Grow the sheet by 5 rows by dragging the "expandor" downwards (the number of objects to be added appears in the gray status bar)

Oooh, kind of messy with all those decimal places. The objectsheet treats decimal numbers like a traditional spreadsheet, displaying as many decimal places as possible. We'll alter the display format to clean this up. To do so for our objectsheet, we first show the format row. We use the VIEW menu to do this:

===>

There are several built-in fuctions to control number formatting. Here we use the fix() and sci() functions. fix(value, n) rounds value to n (or less) decimal places and sci(value, n) does the same, but uses scientific notation.

We'll enter "fix(self, 3)" as the format formula for column a and "sci(self,3)" for column b. The variable self refers to "the current value in this column".

Column a Updated Column b Updated

What is going on in with these format row formulas is simply that the calculated value is run through that formula before it is displayed. The functions fix() and sci() are simple Javascript functions that round their arguments to the requested number of decimal places. The transformation process is illustrated below.

Note: since fix() and sci() are generic, we could have just as easily used them in the formula row (as in fix(range(-2,7),3) for a). This would have the effect of limiting the precision of the "raw" internal values.

Since the format transformation is generic, we can place any formula there. Another possibility is to add a label. Just by adding a string prefix or suffix, we can show "units" or any other label (note the change in the format formulas):

Again, this format formula affects only the displayed value; the internal value is the original number.

We can make the labels conditional via Javascript's conditional (x ? y : z) operator, which evaluates to y if x is true and z if x is false. Note that for the screenshot below, we've clicked in the format cell of column b-- it expands so we can see the whole formula.

We can even convert the number displayed, like, to other units (see the format formula in column a):

The original, full-precision value remains behind the scenes, and is the value used when referenced from other formulas. In the above example, though we've translated column a to centimeters for display, the original calculated values for the sheet remain (in inches supposedly):

Note that labels are nothing new for the traditional spreadsheet. You can indeed add labels to cells in a traditional spreadsheet like, in an adjacent column like so:

That's fine but it does start intermingling the visual presentation with the calculation flow. It takes up an extra column, and it's an extra chunk of stuff to move around. Also, if/when the sheet grows in complexity, subtle difficulties creep in (for instance, lookup and indexing functions need to know to "skip" over label columns). Of course, if you like things that way, you can use this technique in the objectsheet as well.


next: cell styling

rk, 2 may 01