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 5

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.

Extending Nano-Sheets for Loading and Saving
Saving the data from a sheet is simple. All you need to do is walk the list of cells, emitting the name and contents for each cell. That data all goes into a block, which is then saved with one function call. REBOL's native data serialization support is terrific for this kind of thing.

   foreach [id cell] cells [
       if not empty-cell? cell [
           repend buffer [
               cell/var reduce [any [cell/formula  get 
   save file buffer
Loading data is a little more involved than saving it, because the load process must allow for future extensions. The actual file format is a REBOL dialect designed to represent spreadsheet data. To read the file, you need to parse the data, according to the grammar for that dialect, and take actions when the incoming content matches production rules. The REBOL documentation has detailed information on parsing in REBOL.

You could just load a serialized block of values and let the index of each item implicitly define the cell it goes to, but that would mean storing an empty value (a marker of some kind) for every empty field, an approach which is neither scalable nor extensible. Fortunately, REBOL makes it easy to use the dialected approach, which lets the spreadsheet use a scalable, extensible data format that's easily editable by humans using any plain text editor. Here's an example data file:

   A1 [7-Feb-2005] B1 [[system/version]] C1 [100] D1 ["Gregg"] E1 [[head reverse copy d1]] 
   C2 [200] 
   C3 [300] 
   C4 [[C1 + C2 + C3]] D4 [[now/date]]
Figure 1 shows what the preceding spreadsheet file looks like when loaded into Nano-Sheets.

Figure 1. Nano-Sheets: The figure shows how the sample data file looks when loaded into Nano-Sheets.
The following lines of code provide graphical buttons and an integrated macro language in the Nano-Sheets spreadsheet project.

   use [
       ; rules
       buttons== cell== sheet-code==
       ; vars
       id val text action face style
   ] [
       id: val: text: action: face: none
       style: 'btn  ; 'button
       buttons==: [
           'buttons into [
               any [
                   set val word! (id: val) 2 [
                       set val string! (text: val)
                       | set val block! (action: val)
                   ] (
                       repend sheet-buttons [id text 
                       append lay/pane face: make-
                         face/size/offset style
                       if 'button = style 
                         [face/edge/size: 1x1]
                       face/text: text
                       face/action: action
                       face/style: style
       cell==: [
           set id word! into [
               opt 'formula set val [block! | path!] 
                  (cells/:id/formula: :val)
               | opt 'value set val 
                   [string! | scalar-types] (
                   set cells/:id/var cells/:id/text: val
       sheet-code==: ['do set sheet-code block! 
         (do sheet-code)]
       sheet==: [
           (sheet-code: none  clear sheet-buttons)
           opt sheet-code==
           any [buttons== | cell==]
Here's an example spreadsheet that provides buttons, an integrated macro language, and uses the preceding extensions to Nano-Sheets:

   do [
       right-now: does [now/time/precise] 
       circle-area: func [diameter] 
          [diameter / 2 ** 2 * pi]
   ] buttons [
       A10 "Random-test" [
         set-cell 'a11 circle-area random 10 1E-2] 
      C10 "test-2" [set-cell 'c11 right-now] 
      A5 "Check" [print "check"] 
      F16 "Done" [quit]] 
      A1 ["test"] 
      C1 [[now/date]] 
      D1 [3] 
      E1 [$200.00] 
      F1 [1x2] 
      E2 [[d1 * e1]] 
      A11 [19.63] 
      C11 [0:46:00.171]
      C12 [[c11 + 1]]
Figure 2. Nano-Sheets Extended: Here's the preceding code example that provides buttons running in Nano-Sheets.
When loaded into Nano-Sheets, the result looks like Figure 2.

By this point, you're probably aware of some of the benefits of designing small Reblets in REBOL. These examples were constructed in a few days' time. This article itself took more time to write than the software. An upcoming article will discuss extending Nano-Sheets to include pretty-printing capabilities, adding keyboard capabilities, and explore how you can use the spreadsheet as a component of more complete applications.

Commercializing the X Internet, Steve Shireman founded eFishAntSea to solve main-stream business problems with Advanced X Internet solutions. With over 27 years of product development in these areas: VLSI manufacturing, dairy automation with RF AutoID, safety-critical train automation, CDMA data wireless product design (including the only "land-line transparent" wireless local loop products, telematics systems, and the fastest wireless data TCP/IP/RLP implementation) Steve is pioneering some vertical market integration such as X-SCADA� and education with other X Internet partners and businesses.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date