refactoring the csv generation code

XLSX.utils.sheet_to_csv
This commit is contained in:
SheetJS 2013-02-20 10:25:02 -05:00
parent 077056d984
commit a9dd6def72
3 changed files with 47 additions and 55 deletions

View File

@ -25,22 +25,5 @@ if(xlsx.SheetNames.indexOf(sheetname)===-1) {
process.exit(1);
}
function stringify(val) {
switch(val.t){
case 'n': return val.v;
case 's': case 'str': return JSON.stringify(val.v);
default: throw 'unrecognized type ' + val.t;
}
}
var sheet = xlsx.Sheets[sheetname];
if(sheet["!ref"]) {
var r = utils.decode_range(sheet["!ref"]);
for(var R = r.s.r; R <= r.e.r; ++R) {
var row = [];
for(var C = r.s.c; C <= r.e.c; ++C) {
var val = sheet[utils.encode_cell({c:C,r:R})];
row.push(val ? stringify(val) : "");
}
console.log(row.join(","));
}
}
console.log(XLSX.utils.sheet_to_csv(sheet));

View File

@ -20,47 +20,30 @@
<script src="xlsx.js"></script>
<script>
function get_radio_value( radioName ) {
var radios = document.getElementsByName( radioName );
for( var i = 0; i < radios.length; i++ ) {
if( radios[i].checked ) {
return radios[i].value;
}
}
var radios = document.getElementsByName( radioName );
for( var i = 0; i < radios.length; i++ ) {
if( radios[i].checked ) {
return radios[i].value;
}
}
}
//Row object array form:
//Each row is an object with column headers as keys
function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var rObjArr = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if(rObjArr.length > 0){
result[sheetName] = rObjArr;
}
});
return result;
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var rObjArr = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if(rObjArr.length > 0){
result[sheetName] = rObjArr;
}
});
return result;
}
function to_csv(sheet) {
function stringify(val) {
switch(val.t){
case 'n': return val.v;
case 's': case 'str': return JSON.stringify(val.v);
default: throw 'unrecognized type ' + val.t;
}
}
if(sheet["!ref"]) {
var r = XLSX.utils.decode_range(sheet["!ref"]);
for(var R = r.s.r; R <= r.e.r; ++R) {
var row = [];
for(var C = r.s.c; C <= r.e.c; ++C) {
var val = sheet[XLSX.utils.encode_cell({c:C,r:R})];
row.push(val ? stringify(val) : "");
}
out.innerText += (row.join(",") + "\n");
}
}
return XLSX.utils.sheet_to_csv(sheet);
}
var drop = document.getElementById('drop');
@ -75,11 +58,13 @@ function handleDrop(e) {
reader.onload = function(e) {
var data = e.target.result;
var xlsx = XLSX.read(data, {type: 'binary'});
if(get_radio_value("format") === "json"){
out.innerText = JSON.stringify(to_json(xlsx), 2, 2);
} else {
to_csv(xlsx.Sheets[xlsx.SheetNames[0]]);
}
var output = "";
if(get_radio_value("format") === "json"){
output = JSON.stringify(to_json(xlsx), 2, 2);
} else {
output = to_csv(xlsx.Sheets[xlsx.SheetNames[0]]);
}
out.innerText = output;
};
reader.readAsBinaryString(f);
}

24
xlsx.js
View File

@ -363,6 +363,29 @@ function sheet_to_row_object_array(sheet){
return outSheet;
}
function sheet_to_csv(sheet) {
var stringify = function stringify(val) {
switch(val.t){
case 'n': return val.v;
case 's': case 'str': return JSON.stringify(val.v);
default: throw 'unrecognized type ' + val.t;
}
}
var out = "";
if(sheet["!ref"]) {
var r = utils.decode_range(sheet["!ref"]);
for(var R = r.s.r; R <= r.e.r; ++R) {
var row = [];
for(var C = r.s.c; C <= r.e.c; ++C) {
var val = sheet[utils.encode_cell({c:C,r:R})];
row.push(val ? stringify(val) : "");
}
out += row.join(",") + "\n";
}
}
return out;
}
var utils = {
encode_col: encode_col,
encode_row: encode_row,
@ -372,6 +395,7 @@ var utils = {
split_cell: split_cell,
decode_cell: decode_cell,
decode_range: decode_range,
sheet_to_csv: sheet_to_csv,
sheet_to_row_object_array: sheet_to_row_object_array
};