diff --git a/Makefile b/Makefile index e8f0e7a..483d037 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,6 @@ update: git show master:tests/mocha.css > tests/mocha.css git show master:tests/mocha.js > tests/mocha.js git show master:tests/xhr-hack.js > tests/xhr-hack.js + git show master:tests/write.js > tests/write.js + git show master:tests/write.html > tests/write.html git show master:demos/datagrid/index.html > datagrid.html diff --git a/tests/write.html b/tests/write.html new file mode 100644 index 0000000..c0fcb26 --- /dev/null +++ b/tests/write.html @@ -0,0 +1,40 @@ + + + + +
+++ + + + diff --git a/tests/write.js b/tests/write.js new file mode 100644 index 0000000..abb48b8 --- /dev/null +++ b/tests/write.js @@ -0,0 +1,213 @@ +/* writing feature test -- look for TEST: in comments */ +/* vim: set ts=2 ft=javascript: */ + +if(typeof console === 'undefined') console = {log: function(){}}; + +var ext = typeof process !== 'undefined' && !!process.argv[2]; + +/* original data */ +var data = [ + [1, 2, 3], + [true, false, null, "sheetjs"], + ["foo bar", "baz", new Date("2014-02-19T14:30Z"), "0.3"], + ["baz", null, "\u0BEE", 3.14159], + ["hidden"], + ["visible"] +]; + +var ws_name = "SheetJS"; + +var wscols = [ + {wch: 6}, // "characters" + {wpx: 50}, // "pixels" + , + {hidden: true} // hide column +]; + +/* At 96 PPI, 1 pt = 1 px */ +var wsrows = [ + {hpt: 12}, // "points" + {hpx: 16}, // "pixels" + , + {hpx: 24, level:3}, + {hidden: true}, // hide row + {hidden: false} +]; + +console.log("Sheet Name: " + ws_name); +console.log("Data: "); +var i = 0; +for(i = 0; i !== data.length; ++i) console.log(data[i]); +console.log("Columns :"); +for(i = 0; i !== wscols.length; ++i) console.log(wscols[i]); + +/* require XLSX */ +if(typeof XLSX === "undefined") { try { XLSX = require('./'); } catch(e) { XLSX = require('../'); } } + +/* blank workbook constructor */ +/* +var wb = { SheetNames: [], Sheets: {} }; +*/ +var wb = XLSX.utils.book_new(); + +/* convert an array of arrays in JS to a CSF spreadsheet */ +var ws = XLSX.utils.aoa_to_sheet(data, {cellDates:true}); + +/* TEST: add worksheet to workbook */ +/* +wb.SheetNames.push(ws_name); +wb.Sheets[ws_name] = ws; +*/ +XLSX.utils.book_append_sheet(wb, ws, ws_name); + +/* TEST: simple formula */ +ws['C1'].f = "A1+B1"; +ws['C2'] = {t:'n', f:"A1+B1"}; + +/* TEST: single-cell array formula */ +/* +ws['D1'] = {t:'n', f:"SUM(A1:C1*A1:C1)", F:"D1:D1"}; +*/ +XLSX.utils.sheet_set_array_formula(ws, 'D1:D1', "SUM(A1:C1*A1:C1)"); + +/* TEST: multi-cell array formula */ +/* +ws['E1'] = {t:'n', f:"TRANSPOSE(A1:D1)", F:"E1:E4"}; +ws['E2'] = {t:'n', F:"E1:E4"}; +ws['E3'] = {t:'n', F:"E1:E4"}; +ws['E4'] = {t:'n', F:"E1:E4"}; +*/ +XLSX.utils.sheet_set_array_formula(ws, 'E1:E4', "TRANSPOSE(A1:D1)"); +ws["!ref"] = "A1:E6"; + +/* TEST: column props */ +ws['!cols'] = wscols; + +/* TEST: row props */ +ws['!rows'] = wsrows; + +/* TEST: hyperlink note: Excel does not automatically style hyperlinks */ +/* +ws['A4'].l = { Target: "#E2" }; +*/ +XLSX.utils.cell_set_internal_link(ws['A4'], "E2"); +/* +ws['A3'].l = { Target: "http://sheetjs.com", Tooltip: "Visit usSheetJS JS-XLSX In-Browser Export Demo
+Example Code + + /* Generate Workbook */ + var wb = XLSX.utils.book_new(); + var ws = XLSX.utils.aoa_to_sheet([["a","b"],[1,2,3]]); + XLSX.utils.book_append_sheet(wb, ws, "SheetJS"); + + /* Trigger Download with `writeFile` */ + XLSX.writeFile(wb, "SheetJS.xlsb", {compression:true}); + +Download Generation Methods: + +- IE6-9 require ActiveX and Windows Script support. + The IE_SaveFile function from the included shim uses VBScript. + +- IE10-11 use msSaveBlob API. + +- When supported, `saveAs` will be used. + +- When available, modern browsers use `URL.createObjectURL`. + +