Skip to content

Instantly share code, notes, and snippets.

@joakim
Created December 12, 2023 12:05
Show Gist options
  • Save joakim/68f33a75657719ad90be70c46545ca4b to your computer and use it in GitHub Desktop.
Save joakim/68f33a75657719ad90be70c46545ca4b to your computer and use it in GitHub Desktop.
Evil Spreadsheet
// Inspired by this Python code: https://ralsina.me/weblog/posts/BB585.html
// Really really limited, but it works. "Look ma, no DAG!"
function evil(formula, sheet) {
with (sheet.functions) {
with (sheet.cells) {
return eval(formula);
}
}
}
class EvilSpreadsheet {
cells;
functions;
constructor(functions = {}, cells = {}) {
this.cells = cells;
this.functions = functions;
return new Proxy(this, {
get(sheet, cell) {
return evil(sheet.cells[String(cell).toUpperCase()], sheet);
},
set(sheet, cell, formula) {
sheet.cells[String(cell).toUpperCase()] = formula;
return true;
}
});
}
}
const sheet = new EvilSpreadsheet(Math);
sheet['a1'] = 6;
sheet['a2'] = 7;
sheet['a3'] = 'log(A1 * A2)';
sheet['a3']; // 3.7376696182833684
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment