Skip to content

Instantly share code, notes, and snippets.

@smalljam
Created August 19, 2015 09:34
Show Gist options
  • Save smalljam/b0e8b7a7e5a218b58e62 to your computer and use it in GitHub Desktop.
Save smalljam/b0e8b7a7e5a218b58e62 to your computer and use it in GitHub Desktop.
xlsx library patch, so we can make bold cells and align them to the right
var XLSX = require('xlsx')
function Workbook() {
this.SheetNames = ['Report'];
this.Sheets = {};
}
var wb = new Workbook();
var ws = {
B1: { v: 'Aug 2015', t: 's' , bold: true, alignRight: true},
C1: { v: 'Sep 2015', t: 's' , bold: true, alignRight: true},
D1: { v: 'Total', t: 's' , bold: true, alignRight: true},
A2: { v: 'Item 1', t: 's' },
B2: { v: 10, t: 'n' , z: '#,##0.00'},
C2: { v: 20, t: 'n' , z: '#,##0.00'},
D2: { f: '=SUM(B2,C2)' , t:'n', z: '#,##0.00', bold: true},
A3: { v: 'Item 2', t: 's'},
B3: { v: 0.20, t: 'n', z: '0.00%' },
C3: { v: 0.20, t: 'n', z: '0.00%' },
D3: { f: '=SUM(B3,C3)' , z: '0.00%', bold: true},
A5: { v: 'totals', t: 's', z: '#,##0.00', bold: true},
B5: { f: '=B2*B3' , z: '#,##0.00', bold: true},
C5: { f: '=C2*C3' , z: '#,##0.00', bold: true},
D5: { f: '=D2*D3' , z: '#,##0.00', bold: true},
'!ref': 'A1:D5',
'!cols': [
{wch:40},
{wch:20},
{wch:20},
{wch:20}
]
};
wb.Sheets['Report'] = ws;
/* write file */
var file = 'test.xlsx';
XLSX.writeFile(wb, file);
console.log(file, 'created');
--- node_modules/xlsx/xlsx.js 2015-04-03 01:52:56.000000000 +0300
+++ xlsx.my.js 2015-08-19 11:47:51.000000000 +0300
@@ -4740,7 +4740,11 @@
function write_cellXfs(cellXfs) {
var o = [];
o[o.length] = (writextag('cellXfs',null));
- cellXfs.forEach(function(c) { o[o.length] = (writextag('xf', null, c)); });
+
+ cellXfs.forEach(function(c) {
+ o[o.length] = (writextag('xf', c.applyAlignment == 1 ? '<alignment horizontal="right"/>' : null, c));
+ });
+
o[o.length] = ("</cellXfs>");
if(o.length === 2) return "";
o[0] = writextag('cellXfs',null, {count:o.length-2}).replace("/>",">");
@@ -4791,7 +4795,8 @@
function write_sty_xml(wb, opts) {
var o = [XML_HEADER, STYLES_XML_ROOT], w;
if((w = write_numFmts(wb.SSF)) != null) o[o.length] = w;
- o[o.length] = ('<fonts count="1"><font><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts>');
+ // o[o.length] = ('<fonts count="1"><font><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts>');
+ o[o.length] = ('<fonts count="2"><font><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font><font><b/><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts>');
o[o.length] = ('<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>');
o[o.length] = ('<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>');
o[o.length] = ('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>');
@@ -7215,17 +7220,18 @@
}
function get_cell_style(styles, cell, opts) {
- var z = opts.revssf[cell.z != null ? cell.z : "General"];
- for(var i = 0, len = styles.length; i != len; ++i) if(styles[i].numFmtId === z) return i;
- styles[len] = {
- numFmtId:z,
- fontId:0,
- fillId:0,
- borderId:0,
- xfId:0,
- applyNumberFormat:1
- };
- return len;
+ var z = opts.revssf[cell.z||"General"];
+ for(var i = 0; i != styles.length; ++i) if(styles[i].numFmtId === z && (styles[i].fontId === 1 ? cell.bold : !cell.bold) && (styles[i].applyAlignment === 1 ? cell.alignRight : !cell.alignRight)) return i;
+ styles[styles.length] = {
+ numFmtId: z,
+ fontId: cell.bold ? 1 : 0,
+ fillId: 0,
+ borderId: 0,
+ xfId: 0,
+ applyNumberFormat: 1,
+ applyAlignment: cell.alignRight ? 1 : 0
+ };
+ return styles.length-1;
}
function safe_format(p, fmtid, fillid, opts) {
@@ -7388,7 +7394,7 @@
}
function write_ws_xml_cell(cell, ref, ws, opts, idx, wb) {
- if(cell.v === undefined) return "";
+ if(cell.v === undefined && ! cell.f) return "";
var vv = "";
var oldt = cell.t, oldv = cell.v;
switch(cell.t) {
@@ -7405,7 +7411,12 @@
break;
default: vv = cell.v; break;
}
- var v = writetag('v', escapexml(vv)), o = {r:ref};
+ // var v = writetag('v', escapexml(vv)), o = {r:ref};
+ var o = { r: ref },
+ v = cell.f ?
+ writetag('f', escapexml(cell.f)) :
+ writetag('v', escapexml(vv));
+
/* TODO: cell style */
var os = get_cell_style(opts.cellXfs, cell, opts);
if(os !== 0) o.s = os;
@@ -7422,6 +7433,7 @@
o.t = "str"; break;
}
if(cell.t != oldt) { cell.t = oldt; cell.v = oldv; }
+
return writextag('c', v, o);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment