-- Built-in spreadsheet functions. -- Every function receives the already evaluated arguments; ranges arrive as -- { __range = true, values = { ... } } and are flattened by the helpers below. local format = require("sheet.format") local M = {} local function fail(code) error(code, 0) end local function flatten(args) local out = {} for i = 1, #args do local argument = args[i] if type(argument) == "table" and argument.__range then for j = 1, #argument.values do out[#out + 1] = argument.values[j] end else out[#out + 1] = argument end end return out end -- Numbers only: text inside a range is ignored, like a spreadsheet does. local function numeric_values(args) local values = flatten(args) local out = {} for i = 1, #values do local value = values[i] if type(value) == "number" then out[#out + 1] = value elseif type(value) == "string" then local number = tonumber(value) if number then out[#out + 1] = number end end end return out end local function number_arg(value) if type(value) == "number" then return value end if type(value) == "string" then local number = tonumber(value) if number then return number end end if type(value) == "boolean" then if value then return 1 end return 0 end fail("#VALUE!") end local function text_arg(value) local kind = type(value) if kind == "string" then return value end if kind == "number" then return format.number(value) end if kind == "boolean" then if value then return "TRUE" end return "FALSE" end fail("#VALUE!") end local function boolean_arg(value) local kind = type(value) if kind == "boolean" then return value end if kind == "number" then return value ~= 0 end if kind == "string" then local lower = string.lower(value) if lower == "true" or lower == "oui" or lower == "vrai" then return true end if lower == "false" or lower == "non" or lower == "faux" or lower == "" then return false end end fail("#VALUE!") end local function sum(args) local values = numeric_values(args) local total = 0 for i = 1, #values do total = total + values[i] end return total end local function product(args) local values = numeric_values(args) local total = 1 for i = 1, #values do total = total * values[i] end return total end local function average(args) local values = numeric_values(args) if #values == 0 then fail("#DIV/0!") end local total = 0 for i = 1, #values do total = total + values[i] end return total / #values end local function minimum(args) local values = numeric_values(args) if #values == 0 then return 0 end local best = values[1] for i = 2, #values do if values[i] < best then best = values[i] end end return best end local function maximum(args) local values = numeric_values(args) if #values == 0 then return 0 end local best = values[1] for i = 2, #values do if values[i] > best then best = values[i] end end return best end local function count(args) return #numeric_values(args) end local function count_non_empty(args) local values = flatten(args) local total = 0 for i = 1, #values do if values[i] ~= nil then total = total + 1 end end return total end local function median(args) local values = numeric_values(args) if #values == 0 then fail("#DIV/0!") end table.sort(values) local middle = (#values + 1) // 2 if #values % 2 == 1 then return values[middle] end return (values[middle] + values[middle + 1]) / 2 end local function choose(args) if boolean_arg(args[1]) then if args[2] == nil then return true end return args[2] end if args[3] == nil then return false end return args[3] end local function all_of(args) local values = flatten(args) for i = 1, #values do if not boolean_arg(values[i]) then return false end end return true end local function any_of(args) local values = flatten(args) for i = 1, #values do if boolean_arg(values[i]) then return true end end return false end local function not_of(args) return not boolean_arg(args[1]) end local function round(args) local value = number_arg(args[1]) local digits = 0 if args[2] ~= nil then digits = number_arg(args[2]) end local multiplier = 10 ^ digits local scaled = value * multiplier local rounded if scaled >= 0 then rounded = math.floor(scaled + 0.5) else rounded = math.ceil(scaled - 0.5) end if digits == 0 then return rounded end return rounded / multiplier end local function absolute(args) return math.abs(number_arg(args[1])) end local function square_root(args) local value = number_arg(args[1]) if value < 0 then fail("#VALUE!") end return math.sqrt(value) end local function integer_part(args) return math.floor(number_arg(args[1])) end local function modulo(args) local left = number_arg(args[1]) local right = number_arg(args[2]) if right == 0 then fail("#DIV/0!") end return left % right end local function power(args) return number_arg(args[1]) ^ number_arg(args[2]) end local function concat(args) local values = flatten(args) local parts = {} for i = 1, #values do parts[#parts + 1] = format.value(values[i]) end return table.concat(parts) end local function length(args) return #text_arg(args[1]) end local function upper(args) return string.upper(text_arg(args[1])) end local function lower(args) return string.lower(text_arg(args[1])) end local function left(args) local text = text_arg(args[1]) local amount = number_arg(args[2]) if amount < 0 then fail("#VALUE!") end return string.sub(text, 1, amount) end local function right(args) local text = text_arg(args[1]) local amount = number_arg(args[2]) if amount < 0 then fail("#VALUE!") end if amount >= #text then return text end return string.sub(text, #text - amount + 1) end local function trim(args) local text = text_arg(args[1]) return string.match(text, "^%s*(.-)%s*$") end -- name, minimum arguments, maximum arguments (-1 = unlimited), implementation local definitions = { { "SUM", 1, -1, sum }, { "PRODUCT", 1, -1, product }, { "AVG", 1, -1, average }, { "MIN", 1, -1, minimum }, { "MAX", 1, -1, maximum }, { "COUNT", 1, -1, count }, { "COUNTA", 1, -1, count_non_empty }, { "MEDIAN", 1, -1, median }, { "IF", 2, 3, choose }, { "AND", 1, -1, all_of }, { "OR", 1, -1, any_of }, { "NOT", 1, 1, not_of }, { "ROUND", 1, 2, round }, { "ABS", 1, 1, absolute }, { "SQRT", 1, 1, square_root }, { "INT", 1, 1, integer_part }, { "MOD", 2, 2, modulo }, { "POWER", 2, 2, power }, { "CONCAT", 1, -1, concat }, { "LEN", 1, 1, length }, { "UPPER", 1, 1, upper }, { "LOWER", 1, 1, lower }, { "LEFT", 2, 2, left }, { "RIGHT", 2, 2, right }, { "TRIM", 1, 1, trim }, } local by_name = {} local names = {} for i = 1, #definitions do local definition = definitions[i] by_name[definition[1]] = { min = definition[2], max = definition[3], fn = definition[4] } names[#names + 1] = definition[1] end function M.call(name, args) local builtin = by_name[name] if builtin == nil then fail("#NAME?") end local received = #args if received < builtin.min or (builtin.max >= 0 and received > builtin.max) then fail("#VALUE!") end return builtin.fn(args) end function M.exists(name) return by_name[string.upper(name)] ~= nil end function M.names() local copy = {} for i = 1, #names do copy[i] = names[i] end table.sort(copy) return copy end return M