Skip to content

Instantly share code, notes, and snippets.

View pietersv's full-sized avatar

Pieter Sheth-Voss pietersv

View GitHub Profile
var xlsx = require('xlsx');
var wb = {
"Sheets": {
"Sheet1": {
"!ref": "A1:C7",
"A1": {
"t": "s",
"v": "This is a merged cell",
"r": "<t>This is a merged cell</t>",
@pietersv
pietersv / gist:53333458e834deb7a67b
Created February 26, 2015 04:39
Extract theme and styles as XML from a Microsoft Excel .xlsx spreadsheet
var AdmZip = require('adm-zip');
var zip = new AdmZip(__dirname + '/../tinker/format.xlsx');
var themeXml = zip.readAsText("xl/theme/theme1.xml");
var stylesXml = zip.readAsText("xl/styles.xml");
{
"mode": "html"
}
"use strict";
var JSZip = require('jszip');
var fs = require("fs");
var FILE1 = process.argv[2];
var FILE2 = process.argv[3];
var zip1 = new JSZip(fs.readFileSync(FILE1));
var zip2 = new JSZip(fs.readFileSync(FILE2));
Object.keys(zip1.files).forEach(function (key) {
@pietersv
pietersv / gist:b349214cda48a124ee5b
Last active November 4, 2015 04:22
Count pages of each Microsoft Word .docx file in a directory
var JSZip = require('jszip');
var fs = require('fs');
var cheerio = require('cheerio');
fs.readdir(DIRECTORY, function (err, files) {
files.filter(function (file) {return file.substr(-5).toLowerCase() == ".docx";}).forEach(function (file) {
fs.readFile(DIRECTORY + file, function (err, content) {
var zip = new JSZip();
zip.load(content);
var xml = zip.file('word/document.xml').asText();
@pietersv
pietersv / console.save.js
Created November 18, 2015 01:50
Save JSON from console
//http://bgrins.github.io/devtools-snippets/#console-save
(function(console){
console.save = function(data, filename){
if(!data) return console.error('Console.save: No data');
if(!filename) filename = 'console.json'
if(typeof data === "object") data = JSON.stringify(data, undefined, 4)
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
@pietersv
pietersv / gist:6e8b9f40ce47494c01b09b3b07c41708
Created May 11, 2020 23:06
Set Protobi foreground bar colors
In CSS:
```css
.protobi .element .bar.fg {
background-color: #688c40
}
```
In Javascript:
```js
var styleSheet = _.find(document.styleSheets, {"href": "http://localhost:3000/protobi/style/protobi.css"})
@pietersv
pietersv / bar-color.md
Last active May 11, 2020 23:09
Change bar colors programmatically

In CSS:

.protobi .element .bar.fg {
   background-color: #688c40
}

In Javascript:

var styleSheet = _.find(document.styleSheets, {"href": "http://localhost:3000/protobi/style/protobi.css"})
@pietersv
pietersv / rim_weights.md
Last active June 12, 2020 12:44
Calculate rim weights

Rim weighting algorithm

The function below calculates a single respondent-level weight variable such that when applied as a weight, the marginal distribution of multiple elements yields the desired proportions. This can be useful to ensure that the results of a survey sample reflect the population distribution.

Algorithms

Add this function within a Protobi data process:

/**
 * Calculate a single respondent-level  weight such that when weighting is applied 
 * multiple variables yield the desired marginal distribution
@pietersv
pietersv / ptlookup.md
Created July 2, 2020 16:45
Memoized functions to quckly convert z-value and t-values to tables

Memoized functions to quckly convert z-value and t-values to tables

t-value to p-value

    // memoized table of p values for t values, for df < 30 and t values between 0.1 and 5
    // the first index is the number of drgr
    var _ttable = [null,{"-2.3":0.532,"-1.6":0.563,"-1.2":0.593,"-0.9":0.621,"-0.7":0.648,"-0.5":0.672,"-0.4":0.694,"-0.2":0.715,"-0.1":0.733,"-0.0":0.75,"0.1":0.765,"0.2":0.779,"0.3":0.803,"0.4":0.813,"0.5":0.831,"0.6":0.846,"0.7":0.859,"0.8":0.869,"0.9":0.879,"1.0":0.891,"1.1":0.901,"1.2":0.909,"1.3":0.918,"1.4":0.926,"1.5":0.933,"1.6":0.937},{"-2.3":0.535,"-1.6":0.57,"-1.2":0.604,"-0.9":0.636,"-0.7":0.667,"-0.5":0.695,"-0.4":0.722,"-0.2":0.746,"-0.1":0.768,"-0.0":0.789,"0.1":0.807,"0.2":0.823,"0.3":0.852,"0.4":0.864,"0.5":0.884,"0.6":0.901,"0.7":0.915,"0.8":0.926,"0.9":0.935,"1.0":0.946,"1.1":0.955,"1.2":0.962,"1.3":0.969,"1.4":0.974,"1.5":0.979,"1.6":0.981},{"-2.3":0.537,"-1.6":0.573,"-1.2":0.608,"-0.9":0.642,"-0.7":0.674,"-0.5":0.705,"-0.4":0.733,"-0.2":0.759,"-0.1":0.783,"