Extending Nano-Sheets with Macros
Adding macro capability is easyNano-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
]