Skip to content

Instantly share code, notes, and snippets.

View chrispsn's full-sized avatar

Chris Pearson chrispsn

View GitHub Profile
@chrispsn
chrispsn / Utils_GlobalsMgmt.js
Last active July 2, 2021 03:32
Example of using JavaScript (specifically JScript) to automate Excel. To execute, run 'cscript excel.js' at Command Prompt.
var Utils_GlobalsMgmt = {
temp_disable_screenupdating: function(excel_app, func) {
var original_setting = excel_app.ScreenUpdating
excel_app.ScreenUpdating = false;
var output = func();
excel_app.ScreenUpdating = original_setting;
return output;
},
@chrispsn
chrispsn / google_closure_jscript_test.js
Last active August 30, 2016 12:05
Google Closure in JScript. I can't believe this works
function get_text_in_file(filepath) {
var FS = new ActiveXObject("Scripting.FileSystemObject");
return FS.OpenTextFile(filepath, 1).ReadAll(); // 1 = for reading
}
function require(filepath) {
eval(get_text_in_file(filepath));
return true;
}
@chrispsn
chrispsn / test_obj_of_arrays.js
Last active August 8, 2017 12:47
Testing ways to implement recordsets in vanilla JavaScript
/*
An object literal containing arrays and computed properties
can act as a recordset.
This would be a pain to edit in a text editor for long arrays,
but Mesh should make it intuitive via the grid interface.
*/
const data = {
name: ['John Doe', 'Jane Smith'],
age: [30, 29],
get adult_yrs() {return this.age.map(x => x - 18)},
@chrispsn
chrispsn / object_self_refs.js
Created November 11, 2017 22:47
Is there a way to allow branches of a nested object to look at other branches of the parent?
'use strict';
module.exports = (function() {
// It would be good to get rid of the my_module declaration entirely
const my_module = {
a: 'abc',
get b() {return 123},
get c() {return my_module.a},
get c2() {return this.a},
get d() {return {
@chrispsn
chrispsn / use_spreadsheet_as_function.js
Last active January 6, 2018 13:33
Objects with self-referencing getters can be used like functions in JavaScript. Because Mesh spreadsheets are just objects, we can use this to turn spreadsheets into functions.
// Define a sheet (ATTACHMENTS omitted for brevity)
const calc = {
term: 3,
rate: 0.05,
start_value: 100,
get end_value() {return this.start_value * (1 + this.rate) ** this.term}
}
// Simple example of using a sheet as a template -
// equivalent in Excel would be overwriting a cell,
@chrispsn
chrispsn / mesh_file_structure.js
Last active January 6, 2018 15:09
Mesh file structure (WIP).
// The below format takes the 'store each sheet in one file' approach.
//
// I suspect we will end up deciding having multiple sheets in one file would be bad practice
// (eg too much code to read in one file, and better coding style to keep things modular).
//
// A single-sheet-per-file approach would likely discourage inter-sheet references
// (as in, imports or requires), because of the overhead of having an extra import.
// What I *think* would happen instead is you'd have:
// - an 'assumptions' sheet which is full of hardcodes,
// - a 'calcs' sheet that contains only the assumptions it needs for its calcs, and
@chrispsn
chrispsn / mesh_cache.js
Created January 19, 2018 05:18
Mesh may cache results of sheet computations in a private property of the object literal.
'use strict';
const ROOT = {
get _cache() {
// These two lines let the proxy get access to the sheet
const calc = this;
delete calc._cache;
return (calc._cache = new Proxy({}, {
get(c, k) {
// TODO how does speed compare to try/catch?
@chrispsn
chrispsn / alt_mesh_structure.js
Last active April 28, 2018 12:23
Possible alternate Mesh sheet structure. Minimal boilerplate, with auto-caching (albeit shallow) of calcs, and optimised for diff readability.
const ROOT = [
['hello', [1, 1], $ => `world`],
['calc', [0, 0], $ => $['hello']],
].reduce((o,[k,g,v]) => Object.defineProperty(o, k, {
get: () => {
console.log('Recording', k);
delete o[k];
return o[k]=v(o)
},
configurable:true
@chrispsn
chrispsn / parent.js
Last active March 24, 2018 12:24
Playing with Web Workers so we can get rid of sheet references in Mesh while not polluting the window scope.
var myWorker = new Worker('worker.js');
myWorker.onmessage = (e) => console.log(e.data);
myWorker.postMessage("Hello!");
@chrispsn
chrispsn / alt_table_structure.js
Created April 28, 2018 08:00
Restructuring Table as a class.
class Table extends Array {
static get [Symbol.species]() { return Array; } // Fixes map method
constructor() {
super();
Object.defineProperty(this, '_iteratorPairs', {value: []});
Object.defineProperty(this, '_headings', {value: []});
for (let {heading:h, iterable:i} of arguments) {
this._headings.push(h);
this._iteratorPairs.push([h, i[Symbol.iterator]()]);
Object.defineProperty(this, h, {get: () => this.map(r => r[h])});