Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions sheets/references/sheet-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ <h1 data-ss-id="title">SHEET TITLE</h1>
if (!inp) v = null;
else if (inp.formula !== undefined) {
this.busy[key] = true;
try { v = ++this.steps > LIMIT.steps ? ERR("#CALC!") : this.evaluate(parse(inp.formula), tabName, 0, 0); } catch (e) { v = ERR(typeof e === "string" ? e : "#ERROR!"); }
try { var tree = parse(inp.formula); v = ++this.steps + this.sureReads(tree, tabName, 0, 0) > LIMIT.steps ? ERR("#CALC!") : this.evaluate(tree, tabName, 0, 0); } catch (e) { v = ERR(typeof e === "string" ? e : "#ERROR!"); }
if (isRange(v)) v = v.rows[0][0];
if (v === undefined) v = ERR("#VALUE!"); // a function given too few arguments
if (typeof v === "number" && !isFinite(v)) v = ERR("#NUM!");
Expand All @@ -403,20 +403,39 @@ <h1 data-ss-id="title">SHEET TITLE</h1>
} else v = inp.value;
this.memo[key] = v; return v;
};
Book.prototype.refRange = function (tok, here, dr, dc) {
// A reference's cells: a span, or null where refRange gives #REF! (no such tab, or a range off the sheet).
Book.prototype.refSpan = function (tok, here, dr, dc) {
var tabName = tok.tab || here;
if (!this.raw[String(tabName).toLowerCase()]) return ERR("#REF!");
if (!this.raw[String(tabName).toLowerCase()]) return null;
var parts = tok.v.split(":"), self = this;
function cell(s, isEnd) {
var colOnly = /^\$?([A-Z]{1,3})$/.exec(s);
if (colOnly) { var cc = colNum(colOnly[1]); if (s.charAt(0) !== "$") cc += dc; return { c: cc, r: isEnd ? Math.max(1, self.size(tabName).rows) : 1 }; }
var m = /^(\$?)([A-Z]{1,3})(\$?)(\d+)$/.exec(s); if (!m) return null;
return { c: colNum(m[2]) + (m[1] ? 0 : dc), r: +m[4] + (m[3] ? 0 : dr) };
}
var s = span(cell(parts[0], false), parts[1] ? cell(parts[1], true) : cell(parts[0], false));
return span(cell(parts[0], false), parts[1] ? cell(parts[1], true) : cell(parts[0], false));
};
Book.prototype.refRange = function (tok, here, dr, dc) {
var tabName = tok.tab || here, s = this.refSpan(tok, here, dr, dc), self = this;
if (!s) return ERR("#REF!");
var got = mk(s.r1 - s.r0 + 1, s.c1 - s.c0 + 1, function (r, c) { return self.value(tabName, addr(s.r0 + r, s.c0 + c)); });
return parts[1] ? got : got.rows[0][0];
return tok.v.indexOf(":") >= 0 ? got : got.rows[0][0];
};
// The cells a formula is sure to read: every range it names, except in the parts of IF, IFERROR, IFNA and IFS
// past their first, which may never be worked out. Each read is charged its cells (mk), so a formula whose sure
// reads are past the budget ends in #CALC! without reading a cell, where it once read millions first.
var LAZY = map({ IF: 1, IFERROR: 1, IFNA: 1, IFS: 1 });
Book.prototype.sureReads = function (n, here, dr, dc) {
var self = this, s, f, a;
switch (n.k) {
case "ref": s = this.refSpan(n, here, dr, dc); return s ? s.n : 0;
case "neg": case "pct": return this.sureReads(n.x, here, dr, dc);
case "bin": return this.sureReads(n.l, here, dr, dc) + this.sureReads(n.r, here, dr, dc);
case "call": f = n.f.replace(/^_XLFN\./, ""); a = LAZY[f] ? n.a.slice(0, 1) : F[f] ? n.a : [];
return a.reduce(function (t, x) { return t + self.sureReads(x, here, dr, dc); }, 0);
}
return 0;
};
var KEEP_RANGE = null;
Book.prototype.evaluate = function (n, here, dr, dc) {
Expand Down Expand Up @@ -493,7 +512,7 @@ <h1 data-ss-id="title">SHEET TITLE</h1>
// A rule's formula is written for the top-left cell of its range; dr/dc move it to another cell.
Book.prototype.ruleAt = function (formula, tabName, dr, dc) {
if (++this.steps > LIMIT.steps) return false;
try { var v = this.evaluate(parse(String(formula).replace(/^=/, "")), tabName, dr, dc); if (isRange(v)) v = v.rows[0][0]; var b = bool(v); return isErr(b) ? false : b; } catch (e) { return false; }
try { var tree = parse(String(formula).replace(/^=/, "")); if (this.steps + this.sureReads(tree, tabName, dr, dc) > LIMIT.steps) return false; var v = this.evaluate(tree, tabName, dr, dc); if (isRange(v)) v = v.rows[0][0]; var b = bool(v); return isErr(b) ? false : b; } catch (e) { return false; }
};
function expand(range) { // "A2:C9" -> list of {r,c}
var p = String(range).toUpperCase().replace(/\$/g, "").split(":"), s = span(parseAddr(p[0]), parseAddr(p[1] || p[0])), out = [];
Expand Down
19 changes: 19 additions & 0 deletions tests/hostile.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ module.exports = function ({ E, test, script, within, ROOT }) {
assert.strictEqual(one("=SUMPRODUCT((A1:A20=0)*(A1:T1=0))"), 400); // a small one still works
assert.ok(Date.now() - t0 < 4000, `took ${Date.now() - t0} ms`);
});
test("a formula whose ranges add up past the budget ends in #CALC! before it reads a cell of them", () => {
const passes = Math.ceil(E.LIMIT.steps / 200000) + 1, big = "SUMPRODUCT(" + Array(passes).fill("A1:T10000").join(",") + ")";
const counted = (cells) => {
const book = new E.Book({ tabs: [{ name: "T", cells: Object.assign({ A1: 1 }, cells) }] }), value = book.value;
book.reads = []; book.value = function (t, a) { this.reads.push(a); return value.call(this, t, a); };
return book;
};
let b = counted({ Z1: "=" + big });
assert.strictEqual(errOf(within(3000, () => b.value("T", "Z1"))), "#CALC!");
assert.strictEqual(b.reads.length, 1, `read ${b.reads.length} cells`); // the cell itself, and none of its 3.2 million
b = counted({ Z1: "=SUM(A1:A3)+A1" });
assert.strictEqual(b.value("T", "Z1"), 2); // within the budget, it reads what it names
assert.deepStrictEqual(b.reads, ["Z1", "A1", "A2", "A3", "A1"]);
b = counted({ Z1: "=IF(A1=1, 5, " + big + ")", Z2: "=IFERROR(A1, " + big + ")", Z3: "=IFS(A1=1, 7, TRUE, " + big + ")" });
assert.deepStrictEqual(["Z1", "Z2", "Z3"].map((a) => b.value("T", a)), [5, 1, 7]); // a part that may never be worked out isn't counted
b = counted({});
assert.strictEqual(within(3000, () => b.ruleAt("=" + big + ">0", "T", 0, 0)), false); // a highlight's test, the same
assert.strictEqual(b.reads.length, 0, `read ${b.reads.length} cells`);
});
test("text a formula makes is charged by its length, not only by its cells", () => {
const t = (b1) => within(3000, () => new E.Book({ tabs: [{ name: "T", cells: { B1: b1, C1: "=SUMPRODUCT(LEN(UPPER(A1:A2000&B1)))" } }] }).value("T", "C1"));
assert.strictEqual(t("x"), 2000);
Expand Down
Loading