Skip to content

Instantly share code, notes, and snippets.

@ScreamZ
Last active March 14, 2017 16:24
Show Gist options
  • Save ScreamZ/ab1c5fe817208c8d5615cd664d1e2fc5 to your computer and use it in GitHub Desktop.
Save ScreamZ/ab1c5fe817208c8d5615cd664d1e2fc5 to your computer and use it in GitHub Desktop.
In-browser js spreadsheets generation library
<script>
$('#export').on('click', function () {
var tableContent = taffyData[0].content;
var excelSheetElements = {
name: 'My fees', // Name of the sheet (tabs)
data: tableContent.map(function (el) { // An array of array containing data (each element is a line)
return [el.date, el.label, el.amount]
})
};
foncia.export_excel.exportDocument('My site - My fees ' + new Date().toLocaleDateString(), excelSheetElements)
});
</script>
var screamz = screamz || {};
screamz.spreadsheets_export = (function ($, ns) {
/**
* An element that will be exposed for Excel download
* @typedef {Object} ExcelSheetElement
*
* @property {string} name - Indicates the sheet name in the workbook.
* @property {array} data - The line that needs to be printed
*/
/**
* Trigger the download of an Excel's like document based on the data you provided.
*
* @param {string} documentName - The downloaded document name.
* @param {ExcelSheetElement|ExcelSheetElement[]} excelSheetElements - The data or an array of data.
*/
ns.generateSpreadSheet = function generateSpreadSheet (documentName, excelSheetElements) {
if (!Array.isArray(excelSheetElements)) {
excelSheetElements = [excelSheetElements];
}
var workBook = new Workbook();
excelSheetElements.forEach(function (el) {
var workSheet = sheet_from_array_of_arrays(el.data);
workBook.SheetNames.push(el.name);
workBook.Sheets[el.name] = workSheet;
});
var wbout = XLSX.write(workBook, {bookType: 'xlsx', bookSST: true, type: 'binary'});
saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), documentName + ".xlsx")
};
function datenum(v) {
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}
function sheet_from_array_of_arrays(data) {
var ws = {};
var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}};
for (var R = 0; R != data.length; ++R) {
for (var C = 0; C != data[R].length; ++C) {
if (range.s.r > R) range.s.r = R;
if (range.s.c > C) range.s.c = C;
if (range.e.r < R) range.e.r = R;
if (range.e.c < C) range.e.c = C;
var cell = {v: data[R][C]};
if (cell.v == null) continue;
var cell_ref = XLSX.utils.encode_cell({c: C, r: R});
if (typeof cell.v === 'number') cell.t = 'n';
else if (typeof cell.v === 'boolean') cell.t = 'b';
else if (cell.v instanceof Date) {
cell.t = 'n';
cell.z = XLSX.SSF._table[14];
cell.v = datenum(cell.v);
}
else cell.t = 's';
ws[cell_ref] = cell;
}
}
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
}
function Workbook() {
if (!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
return ns;
})(jQuery, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment