forked from sheetjs/sheetjs
192 lines
6.3 KiB
JavaScript
192 lines
6.3 KiB
JavaScript
function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/){
|
|
if(sheet == null || sheet["!ref"] == null) return [];
|
|
var val = {t:'n',v:0}, header = 0, offset = 1, hdr/*:Array<any>*/ = [], isempty = true, v=0, vv="";
|
|
var r = {s:{r:0,c:0},e:{r:0,c:0}};
|
|
var o = opts != null ? opts : {};
|
|
var raw = o.raw;
|
|
var defval = o.defval;
|
|
var range = o.range != null ? o.range : sheet["!ref"];
|
|
if(o.header === 1) header = 1;
|
|
else if(o.header === "A") header = 2;
|
|
else if(Array.isArray(o.header)) header = 3;
|
|
switch(typeof range) {
|
|
case 'string': r = safe_decode_range(range); break;
|
|
case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
|
|
default: r = range;
|
|
}
|
|
if(header > 0) offset = 0;
|
|
var rr = encode_row(r.s.r);
|
|
var cols = new Array(r.e.c-r.s.c+1);
|
|
var out = new Array(r.e.r-r.s.r-offset+1);
|
|
var outi = 0, counter = 0;
|
|
var dense = Array.isArray(sheet);
|
|
var R = r.s.r, C = 0, CC = 0;
|
|
if(!sheet[R]) sheet[R] = [];
|
|
for(C = r.s.c; C <= r.e.c; ++C) {
|
|
cols[C] = encode_col(C);
|
|
val = dense ? sheet[R][C] : sheet[cols[C] + rr];
|
|
switch(header) {
|
|
case 1: hdr[C] = C - r.s.c; break;
|
|
case 2: hdr[C] = cols[C]; break;
|
|
case 3: hdr[C] = o.header[C - r.s.c]; break;
|
|
default:
|
|
if(val == null) continue;
|
|
vv = v = format_cell(val, null, o);
|
|
counter = 0;
|
|
for(CC = 0; CC < hdr.length; ++CC) if(hdr[CC] == vv) vv = v + "_" + (++counter);
|
|
hdr[C] = vv;
|
|
}
|
|
}
|
|
var row = (header === 1) ? [] : {};
|
|
for (R = r.s.r + offset; R <= r.e.r; ++R) {
|
|
rr = encode_row(R);
|
|
isempty = true;
|
|
if(header === 1) row = [];
|
|
else {
|
|
row = {};
|
|
if(Object.defineProperty) try { Object.defineProperty(row, '__rowNum__', {value:R, enumerable:false}); } catch(e) { row.__rowNum__ = R; }
|
|
else row.__rowNum__ = R;
|
|
}
|
|
if(!dense || sheet[R]) for (C = r.s.c; C <= r.e.c; ++C) {
|
|
val = dense ? sheet[R][C] : sheet[cols[C] + rr];
|
|
if(val === undefined || val.t === undefined) {
|
|
if(defval === undefined) continue;
|
|
if(hdr[C] != null) { row[hdr[C]] = defval; isempty = false; }
|
|
continue;
|
|
}
|
|
v = val.v;
|
|
switch(val.t){
|
|
case 'z': if(v == null) break; continue;
|
|
case 'e': continue;
|
|
case 's': case 'd': case 'b': case 'n': break;
|
|
default: throw new Error('unrecognized type ' + val.t);
|
|
}
|
|
if(hdr[C] != null) {
|
|
if(v == null) {
|
|
if(defval !== undefined) row[hdr[C]] = defval;
|
|
else if(raw && v === null) row[hdr[C]] = null;
|
|
else continue;
|
|
} else {
|
|
row[hdr[C]] = raw ? v : format_cell(val,v,o);
|
|
}
|
|
isempty = false;
|
|
}
|
|
}
|
|
if((isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) out[outi++] = row;
|
|
}
|
|
out.length = outi;
|
|
return out;
|
|
}
|
|
|
|
var qreg = /"/g;
|
|
function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Array<string>*/, fs/*:number*/, rs/*:number*/, FS/*:string*/, o/*:Sheet2CSVOpts*/)/*:?string*/ {
|
|
var isempty = true;
|
|
var row = "", txt = "", rr = encode_row(R);
|
|
for(var C = r.s.c; C <= r.e.c; ++C) {
|
|
var val = o.dense ? (sheet[R]||[])[C]: sheet[cols[C] + rr];
|
|
if(val == null) txt = "";
|
|
else if(val.v != null) {
|
|
isempty = false;
|
|
txt = ''+format_cell(val, null, o);
|
|
for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34) {
|
|
txt = "\"" + txt.replace(qreg, '""') + "\""; break; }
|
|
} else if(val.f != null && !val.F) {
|
|
isempty = false;
|
|
txt = '=' + val.f; if(txt.indexOf(",") >= 0) txt = '"' + txt.replace(qreg, '""') + '"';
|
|
} else txt = "";
|
|
/* NOTE: Excel CSV does not support array formulae */
|
|
row += (C === r.s.c ? "" : FS) + txt;
|
|
}
|
|
if(o.blankrows === false && isempty) return null;
|
|
return row;
|
|
}
|
|
|
|
function sheet_to_csv(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/)/*:string*/ {
|
|
var out = [];
|
|
var o = opts == null ? {} : opts;
|
|
if(sheet == null || sheet["!ref"] == null) return "";
|
|
var r = safe_decode_range(sheet["!ref"]);
|
|
var FS = o.FS !== undefined ? o.FS : ",", fs = FS.charCodeAt(0);
|
|
var RS = o.RS !== undefined ? o.RS : "\n", rs = RS.charCodeAt(0);
|
|
var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
|
|
var row = "", cols = [];
|
|
o.dense = Array.isArray(sheet);
|
|
for(var C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
|
|
for(var R = r.s.r; R <= r.e.r; ++R) {
|
|
row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
|
|
if(row == null) { continue; }
|
|
if(o.strip) row = row.replace(endregex,"");
|
|
out.push(row + RS);
|
|
}
|
|
delete o.dense;
|
|
return out.join("");
|
|
}
|
|
|
|
function sheet_to_txt(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/) {
|
|
if(!opts) opts = {}; opts.FS = "\t"; opts.RS = "\n";
|
|
var s = sheet_to_csv(sheet, opts);
|
|
if(typeof cptable == 'undefined') return s;
|
|
var o = cptable.utils.encode(1200, s);
|
|
return "\xff\xfe" + o;
|
|
}
|
|
|
|
function sheet_to_formulae(sheet/*:Worksheet*/)/*:Array<string>*/ {
|
|
var y = "", x, val="";
|
|
if(sheet == null || sheet["!ref"] == null) return [];
|
|
var r = safe_decode_range(sheet['!ref']), rr = "", cols = [], C;
|
|
var cmds = new Array((r.e.r-r.s.r+1)*(r.e.c-r.s.c+1));
|
|
var i = 0;
|
|
var dense = Array.isArray(sheet);
|
|
for(C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
|
|
for(var R = r.s.r; R <= r.e.r; ++R) {
|
|
rr = encode_row(R);
|
|
for(C = r.s.c; C <= r.e.c; ++C) {
|
|
y = cols[C] + rr;
|
|
x = dense ? (sheet[R]||[])[C] : sheet[y];
|
|
val = "";
|
|
if(x === undefined) continue;
|
|
else if(x.F != null) {
|
|
y = x.F;
|
|
if(!x.f) continue;
|
|
val = x.f;
|
|
if(y.indexOf(":") == -1) y = y + ":" + y;
|
|
}
|
|
if(x.f != null) val = x.f;
|
|
else if(x.t == 'z') continue;
|
|
else if(x.t == 'n' && x.v != null) val = "" + x.v;
|
|
else if(x.t == 'b') val = x.v ? "TRUE" : "FALSE";
|
|
else if(x.w !== undefined) val = "'" + x.w;
|
|
else if(x.v === undefined) continue;
|
|
else if(x.t == 's') val = "'" + x.v;
|
|
else val = ""+x.v;
|
|
cmds[i++] = y + "=" + val;
|
|
}
|
|
}
|
|
cmds.length = i;
|
|
return cmds;
|
|
}
|
|
|
|
var utils = {
|
|
encode_col: encode_col,
|
|
encode_row: encode_row,
|
|
encode_cell: encode_cell,
|
|
encode_range: encode_range,
|
|
decode_col: decode_col,
|
|
decode_row: decode_row,
|
|
split_cell: split_cell,
|
|
decode_cell: decode_cell,
|
|
decode_range: decode_range,
|
|
format_cell: format_cell,
|
|
get_formulae: sheet_to_formulae,
|
|
make_csv: sheet_to_csv,
|
|
make_json: sheet_to_json,
|
|
make_formulae: sheet_to_formulae,
|
|
aoa_to_sheet: aoa_to_sheet,
|
|
table_to_sheet: parse_dom_table,
|
|
table_to_book: table_to_book,
|
|
sheet_to_csv: sheet_to_csv,
|
|
sheet_to_json: sheet_to_json,
|
|
sheet_to_formulae: sheet_to_formulae,
|
|
sheet_to_row_object_array: sheet_to_json
|
|
};
|