Login | Register   
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, Part 2 : Page 3

Building on Part 1 of this article, not only can you create a working spreadsheet in just a few bytes using REBOL, but you can extend it to support printing, standard keyboard navigation, macros containing code, and even make it shareable and dynamically updatable—while still using only a few bytes of code.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Extending Nano-Sheets with Macros
Adding macro capability is easy—Nano-Sheets already has the powerful REBOL engine under the hood. For example, simply type the following example formula into a cell and press the Enter key (thanks and credit to Allen Kamp from REBOL Forces for this example).

=length? read http://www.rebol.com

The formula displays the size of the REBOL home page in the cell. Remember that URLs are a native data type in REBOL.

Traditional spreadsheets have specialized worksheet functions tailored for use in formulas such as SUM, or AVG. By including your own REBOL functions in the Nano-Sheets engine you can make them available to end users, just as if they were native. Here are some examples:

avg: average: func ["Arithmetic mean" block [any-block!]] [ divide sum block length? block ] gcd: func ["Greatest common denominator" m [integer!] n [integer!]] [ either (m // n) = 0 [n] [gcd n (m // n)] ; Euclid's algorithm ] geo-mean: func ["Geometric mean" block [any-block!]] [ either empty? block [0] [(product block) ** (1 / length? block)] ] median: func [ "Returns the number in the middle of a set of numbers sorted by value" block [any-block!] /local len mid ] [ block: sort copy block len: length? block mid: to integer! len / 2 either odd? len [ pick block add 1 mid ][ (block/:mid) + (pick block add 1 mid) / 2 ] ] mode: func [ "Returns the most frequently occurring value in the block" block [any-block!] /local last-item result high-count count ][ block: sort copy block result: last-item: first block count: high-count: 1 foreach item next block [ either item = last-item [count: count + 1] [ if count > high-count [ high-count: count result: last-item ] last-item: item count: 1 ] ] if count > high-count [result: last-item] result ] product: func [ "Multiplies all the values in the block" block [any-block!] /local result ][ result: 1 foreach value reduce block [result: result * value] result ] sum: func [ "Adds all the values in the block" block [any-block!] /local result ][ result: 0 foreach value reduce block [result: result + value] result ]

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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