RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Nano-Sheets: A Small But Mighty Spreadsheet Engine in REBOL  : Page 4

Even in today's big-computing environment, not everything useful has to be big. This article shows you how to build a tiny and basic—but powerful—GUI spreadsheet engine in REBOL, using fewer than 1500 bytes of code.

Recalculating Cell Values
You should note a few important things regarding recalculations:

  1. Computation moves from top to bottom.
  2. Recalculation is non-iterative.
  3. Recalculation is triggered whenever a cell loses focus.
  4. The spreadsheet displays any error text in the offending cell if it can't execute the formula successfully.
  5. The spreadsheet executes formulas using the REBOL do command. This means you can run any REBOL code as a formula. The good news is, that means formulas are all-powerful; but that's also the bad news. The simple model discussed here has no security limitations on what formulas can or can't do, so be careful and remember that formulas execute every time a cell loses focus—this version doesn't check to see if the cell value has changed. If you write a formula that reads data from a web site, this version will execute that formula every time the spreadsheet recalculates the cell.
   compute: does [
       foreach cell cells [
           if cell/formula [
               if error? cell/text: try [do cell/formula] 
                 [cell/text: "ERROR!"]
               set cell/var cell/text
               show cell
Common Questions You Might Have
Q: How do I enter formulas?

A: Just as you would in a traditional spreadsheet; put an equal sign (=) as the first character. For example, typing =now/time into a cell will print the current time. Typing =checksum "any message" will yield a CRC checksum. Formulas can combine cell data and REBOL Language expressions.

Q: How is the macro language implemented?

A: The formulas and macros are REBOL code that the spreadsheet evaluates from the cell. The most important thing to keep in mind, in this context, is that REBOL evaluates left-to-right; there is no inherent operator precedence. If you need to control the order of evaluation, use parentheses to group items.

Q: How does Nano-Sheets handle data types?

A: If you type $1000000 into cell A1, and 1.2531 into cell A2, and then enter the formula =A1 * A2 in cell A3, you'll see the way REBOL handles values like money. No messy, time-consuming fussing with formats. REBOL supports all the following scalar data types directly: integer, decimal, money, time, date, tuple, and pair.

Q: Nano-Sheets doesn't like money values with comma separators in them. Why?

A: The lexical format for money values in REBOL was designed to allow foreign currencies to be entered in the same manner as U.S. currency. That means the decimal point can be either a period (.) or a comma (,) and you normally wouldn't use grouping separators. If you want to use them, the correct character is the apostrophe ('). For example, $1234567.89, $1'234'567.89, and $1'234'567,89 are all equivalent.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date