Created
August 22, 2014 14:40
-
-
Save SheetJSDev/a38ce44ca4e4a1012f3b to your computer and use it in GitHub Desktop.
XLSX writing custom date formats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* this sample was run in nodejs */ | |
var XLSX = require('xlsx'); | |
/* build up a very simple workbook */ | |
var wb = { | |
SSF: XLSX.SSF.get_table(), | |
SheetNames: ["Sheet1"], | |
Sheets: { | |
Sheet1: { | |
'!ref': 'A1:A1', | |
A1: { | |
v: 12345, | |
t: 'n' | |
} | |
} | |
} | |
} | |
/* converts date to number type (in the future, will be embedded in the writer) */ | |
function datenum(v, date1904) { | |
if(date1904) v+=1462; | |
var epoch = Date.parse(v); | |
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); | |
} | |
/* version 0.7.9 does not allocate entries for custom formats, this is what it | |
should do (and it will do something like this in a later version) */ | |
function add_custom_format(wb, fmt) { | |
var i; | |
for(i = 0; i < 0x188; ++i) if(wb.SSF[i] === fmt) return; | |
for(i = 0xA4; i < 0x188; ++i) if(!wb.SSF[i]) { wb.SSF[i] = fmt; return; } | |
} | |
/* this function performs the required magic */ | |
function assign_date_with_format(wb, sheet, cell, date, fmt) { | |
/* create sheet if it does not exist */ | |
if(wb.SheetNames.indexOf(sheet) === -1) SheetNames.push(sheet); | |
if(!wb.Sheets[sheet]) wb.Sheets[sheet] = {}; | |
if(!wb.Sheets[sheet]['!ref']) wb.Sheets[sheet]['!ref'] = cell + ':' + cell; | |
/* update sheet range if the new cell is outside of range */ | |
var r = XLSX.utils.decode_range(wb.Sheets[sheet]['!ref']); | |
var c = XLSX.utils.decode_cell(cell); | |
wb.Sheets[sheet]['!ref'] = XLSX.utils.encode_range({ | |
s: {r: Math.min(r.s.r, c.r), c: Math.min(r.s.c, c.c) }, | |
e: {r: Math.max(r.e.r, c.r), c: Math.max(r.e.c, c.c) }, | |
}); | |
/* create cell if it does not exist */ | |
if(!wb.Sheets[sheet][cell]) wb.Sheets[sheet][cell] = {t:'n', v:0}; | |
/* load the format */ | |
add_custom_format(wb, fmt); | |
/* assign the date to the cell */ | |
wb.Sheets[sheet][cell].t = 'n'; | |
wb.Sheets[sheet][cell].v = datenum(date, false); | |
/* set the format */ | |
wb.Sheets[sheet][cell].z = fmt; | |
/* this call updates the formatted text field (`.w`) */ | |
XLSX.utils.format_cell(wb.Sheets[sheet][cell]); | |
} | |
/* sample format and data */ | |
var fmt = 'yyyy/m'; | |
var dates = [ | |
new Date(2014, 5, 22), | |
new Date(2014, 6, 22), | |
new Date(2014, 7, 22), | |
] | |
var sheet = 'Sheet1', cell = ''; | |
for(var d = 0; d < dates.length; ++d) { | |
cell = XLSX.utils.encode_cell({c:0, r:d}); | |
assign_date_with_format(wb, sheet, cell, dates[d], fmt); | |
} | |
console.log(wb.Sheets.Sheet1); | |
XLSX.writeFile(wb, 'testfmt.xlsx'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment