2017-08-09 22:38:23 +00:00
|
|
|
function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/) {
|
2017-04-09 04:03:19 +00:00
|
|
|
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}};
|
2017-08-09 22:38:23 +00:00
|
|
|
var o = opts || {};
|
2014-07-28 13:22:32 +00:00
|
|
|
var raw = o.raw;
|
2017-03-23 05:54:27 +00:00
|
|
|
var defval = o.defval;
|
2017-04-09 04:03:19 +00:00
|
|
|
var range = o.range != null ? o.range : sheet["!ref"];
|
2014-06-29 18:29:45 +00:00
|
|
|
if(o.header === 1) header = 1;
|
|
|
|
else if(o.header === "A") header = 2;
|
|
|
|
else if(Array.isArray(o.header)) header = 3;
|
2014-05-29 22:30:03 +00:00
|
|
|
switch(typeof range) {
|
2014-06-29 18:29:45 +00:00
|
|
|
case 'string': r = safe_decode_range(range); break;
|
|
|
|
case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
|
2014-05-29 22:30:03 +00:00
|
|
|
default: r = range;
|
|
|
|
}
|
2014-06-29 18:29:45 +00:00
|
|
|
if(header > 0) offset = 0;
|
|
|
|
var rr = encode_row(r.s.r);
|
2017-08-09 22:38:23 +00:00
|
|
|
var cols/*:Array<string>*/ = [];
|
|
|
|
var out/*:Array<any>*/ = [];
|
2017-04-09 04:03:19 +00:00
|
|
|
var outi = 0, counter = 0;
|
2017-04-08 06:55:35 +00:00
|
|
|
var dense = Array.isArray(sheet);
|
2017-04-09 04:03:19 +00:00
|
|
|
var R = r.s.r, C = 0, CC = 0;
|
2017-05-11 07:29:59 +00:00
|
|
|
if(dense && !sheet[R]) sheet[R] = [];
|
2014-06-29 18:29:45 +00:00
|
|
|
for(C = r.s.c; C <= r.e.c; ++C) {
|
|
|
|
cols[C] = encode_col(C);
|
2017-04-09 04:03:19 +00:00
|
|
|
val = dense ? sheet[R][C] : sheet[cols[C] + rr];
|
2014-06-29 18:29:45 +00:00
|
|
|
switch(header) {
|
2017-04-26 15:32:42 +00:00
|
|
|
case 1: hdr[C] = C - r.s.c; break;
|
2014-06-29 18:29:45 +00:00
|
|
|
case 2: hdr[C] = cols[C]; break;
|
|
|
|
case 3: hdr[C] = o.header[C - r.s.c]; break;
|
|
|
|
default:
|
2017-12-14 11:11:57 +00:00
|
|
|
if(val == null) val = {w: "__EMPTY", t: "s"};
|
2017-03-25 01:36:40 +00:00
|
|
|
vv = v = format_cell(val, null, o);
|
2017-04-09 04:03:19 +00:00
|
|
|
counter = 0;
|
|
|
|
for(CC = 0; CC < hdr.length; ++CC) if(hdr[CC] == vv) vv = v + "_" + (++counter);
|
2015-03-29 02:12:16 +00:00
|
|
|
hdr[C] = vv;
|
2014-06-29 18:29:45 +00:00
|
|
|
}
|
2014-01-23 06:20:19 +00:00
|
|
|
}
|
2017-08-09 22:38:23 +00:00
|
|
|
var row/*:any*/ = (header === 1) ? [] : {};
|
2014-05-29 22:30:03 +00:00
|
|
|
for (R = r.s.r + offset; R <= r.e.r; ++R) {
|
2014-06-29 18:29:45 +00:00
|
|
|
rr = encode_row(R);
|
2014-01-23 06:20:19 +00:00
|
|
|
isempty = true;
|
2015-04-02 20:32:22 +00:00
|
|
|
if(header === 1) row = [];
|
|
|
|
else {
|
|
|
|
row = {};
|
2017-03-23 01:18:40 +00:00
|
|
|
if(Object.defineProperty) try { Object.defineProperty(row, '__rowNum__', {value:R, enumerable:false}); } catch(e) { row.__rowNum__ = R; }
|
2015-04-02 20:32:22 +00:00
|
|
|
else row.__rowNum__ = R;
|
|
|
|
}
|
2017-04-09 04:03:19 +00:00
|
|
|
if(!dense || sheet[R]) for (C = r.s.c; C <= r.e.c; ++C) {
|
|
|
|
val = dense ? sheet[R][C] : sheet[cols[C] + rr];
|
2017-03-23 05:54:27 +00:00
|
|
|
if(val === undefined || val.t === undefined) {
|
|
|
|
if(defval === undefined) continue;
|
|
|
|
if(hdr[C] != null) { row[hdr[C]] = defval; isempty = false; }
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-29 18:29:45 +00:00
|
|
|
v = val.v;
|
2014-05-03 18:51:10 +00:00
|
|
|
switch(val.t){
|
2017-03-23 05:54:27 +00:00
|
|
|
case 'z': if(v == null) break; continue;
|
2014-05-03 18:51:10 +00:00
|
|
|
case 'e': continue;
|
2017-03-21 20:44:35 +00:00
|
|
|
case 's': case 'd': case 'b': case 'n': break;
|
2017-03-23 01:18:40 +00:00
|
|
|
default: throw new Error('unrecognized type ' + val.t);
|
2013-03-31 22:56:45 +00:00
|
|
|
}
|
2017-03-23 05:54:27 +00:00
|
|
|
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 {
|
2017-03-25 01:36:40 +00:00
|
|
|
row[hdr[C]] = raw ? v : format_cell(val,v,o);
|
2017-03-23 05:54:27 +00:00
|
|
|
}
|
2014-05-03 18:51:10 +00:00
|
|
|
isempty = false;
|
|
|
|
}
|
2013-03-31 22:56:45 +00:00
|
|
|
}
|
2017-03-25 01:36:40 +00:00
|
|
|
if((isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) out[outi++] = row;
|
2013-03-31 22:56:45 +00:00
|
|
|
}
|
2014-07-28 13:22:32 +00:00
|
|
|
out.length = outi;
|
2014-01-23 06:20:19 +00:00
|
|
|
return out;
|
2013-03-31 22:56:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 04:32:13 +00:00
|
|
|
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;
|
2017-08-09 22:38:23 +00:00
|
|
|
var row/*:Array<string>*/ = [], txt = "", rr = encode_row(R);
|
2017-04-16 04:32:13 +00:00
|
|
|
for(var C = r.s.c; C <= r.e.c; ++C) {
|
2017-08-02 16:41:44 +00:00
|
|
|
if (!cols[C]) continue;
|
2017-04-16 04:32:13 +00:00
|
|
|
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);
|
2017-05-11 18:23:21 +00:00
|
|
|
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; }
|
|
|
|
if(txt == "ID") txt = '"ID"';
|
2017-04-16 04:32:13 +00:00
|
|
|
} 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 */
|
2017-08-02 16:41:44 +00:00
|
|
|
row.push(txt);
|
2017-04-16 04:32:13 +00:00
|
|
|
}
|
|
|
|
if(o.blankrows === false && isempty) return null;
|
2017-08-02 16:41:44 +00:00
|
|
|
return row.join(FS);
|
2017-04-16 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sheet_to_csv(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/)/*:string*/ {
|
2017-12-30 05:40:35 +00:00
|
|
|
var out/*:Array<string>*/ = [];
|
2014-06-29 18:29:45 +00:00
|
|
|
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);
|
2017-03-25 01:36:40 +00:00
|
|
|
var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
|
2017-12-30 05:40:35 +00:00
|
|
|
var row = "", cols/*:Array<string>*/ = [];
|
2017-04-16 04:32:13 +00:00
|
|
|
o.dense = Array.isArray(sheet);
|
2017-12-30 05:40:35 +00:00
|
|
|
var colinfo/*:Array<ColInfo>*/ = o.skipHidden && sheet["!cols"] || [];
|
|
|
|
var rowinfo/*:Array<ColInfo>*/ = o.skipHidden && sheet["!rows"] || [];
|
|
|
|
for(var C = r.s.c; C <= r.e.c; ++C) if (!((colinfo[C]||{}).hidden)) cols[C] = encode_col(C);
|
2017-04-16 04:32:13 +00:00
|
|
|
for(var R = r.s.r; R <= r.e.r; ++R) {
|
2017-12-30 05:40:35 +00:00
|
|
|
if ((rowinfo[R]||{}).hidden) continue;
|
2017-04-16 04:32:13 +00:00
|
|
|
row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
|
|
|
|
if(row == null) { continue; }
|
2017-03-22 07:50:11 +00:00
|
|
|
if(o.strip) row = row.replace(endregex,"");
|
2017-04-20 03:24:48 +00:00
|
|
|
out.push(row + RS);
|
2013-03-31 22:56:45 +00:00
|
|
|
}
|
2017-04-16 04:32:13 +00:00
|
|
|
delete o.dense;
|
2017-04-20 03:24:48 +00:00
|
|
|
return out.join("");
|
2013-03-31 22:56:45 +00:00
|
|
|
}
|
2017-04-08 06:55:35 +00:00
|
|
|
|
2017-04-03 00:16:03 +00:00
|
|
|
function sheet_to_txt(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/) {
|
|
|
|
if(!opts) opts = {}; opts.FS = "\t"; opts.RS = "\n";
|
|
|
|
var s = sheet_to_csv(sheet, opts);
|
2017-09-30 06:18:11 +00:00
|
|
|
if(typeof cptable == 'undefined' || opts.type == 'string') return s;
|
|
|
|
var o = cptable.utils.encode(1200, s, 'str');
|
2017-04-03 00:16:03 +00:00
|
|
|
return "\xff\xfe" + o;
|
|
|
|
}
|
2013-03-31 22:56:45 +00:00
|
|
|
|
2017-02-19 20:36:32 +00:00
|
|
|
function sheet_to_formulae(sheet/*:Worksheet*/)/*:Array<string>*/ {
|
|
|
|
var y = "", x, val="";
|
|
|
|
if(sheet == null || sheet["!ref"] == null) return [];
|
2017-12-30 05:40:35 +00:00
|
|
|
var r = safe_decode_range(sheet['!ref']), rr = "", cols/*:Array<string>*/ = [], C;
|
2017-07-05 22:27:54 +00:00
|
|
|
var cmds/*:Array<string>*/ = [];
|
2017-04-08 06:55:35 +00:00
|
|
|
var dense = Array.isArray(sheet);
|
2014-07-28 13:22:32 +00:00
|
|
|
for(C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
|
2014-06-29 18:29:45 +00:00
|
|
|
for(var R = r.s.r; R <= r.e.r; ++R) {
|
|
|
|
rr = encode_row(R);
|
2014-07-28 13:22:32 +00:00
|
|
|
for(C = r.s.c; C <= r.e.c; ++C) {
|
2014-06-29 18:29:45 +00:00
|
|
|
y = cols[C] + rr;
|
2017-04-08 06:55:35 +00:00
|
|
|
x = dense ? (sheet[R]||[])[C] : sheet[y];
|
2014-06-29 18:29:45 +00:00
|
|
|
val = "";
|
|
|
|
if(x === undefined) continue;
|
2017-02-19 20:36:32 +00:00
|
|
|
else if(x.F != null) {
|
|
|
|
y = x.F;
|
|
|
|
if(!x.f) continue;
|
|
|
|
val = x.f;
|
|
|
|
if(y.indexOf(":") == -1) y = y + ":" + y;
|
|
|
|
}
|
2014-06-29 18:29:45 +00:00
|
|
|
if(x.f != null) val = x.f;
|
2017-03-15 08:19:02 +00:00
|
|
|
else if(x.t == 'z') continue;
|
2017-02-19 20:36:32 +00:00
|
|
|
else if(x.t == 'n' && x.v != null) val = "" + x.v;
|
|
|
|
else if(x.t == 'b') val = x.v ? "TRUE" : "FALSE";
|
2014-06-29 18:29:45 +00:00
|
|
|
else if(x.w !== undefined) val = "'" + x.w;
|
|
|
|
else if(x.v === undefined) continue;
|
2017-02-19 20:36:32 +00:00
|
|
|
else if(x.t == 's') val = "'" + x.v;
|
2014-06-29 18:29:45 +00:00
|
|
|
else val = ""+x.v;
|
2017-07-05 22:27:54 +00:00
|
|
|
cmds[cmds.length] = y + "=" + val;
|
2014-06-29 18:29:45 +00:00
|
|
|
}
|
2013-04-17 01:32:20 +00:00
|
|
|
}
|
2013-04-13 17:00:01 +00:00
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
|
2017-05-11 18:23:21 +00:00
|
|
|
function json_to_sheet(js/*:Array<any>*/, opts)/*:Worksheet*/ {
|
|
|
|
var o = opts || {};
|
|
|
|
var ws = ({}/*:any*/);
|
2017-05-17 04:23:36 +00:00
|
|
|
var cell/*:Cell*/;
|
2017-05-11 18:23:21 +00:00
|
|
|
var range/*:Range*/ = ({s: {c:0, r:0}, e: {c:0, r:js.length}}/*:any*/);
|
2017-12-30 05:40:35 +00:00
|
|
|
var hdr/*:Array<string>*/ = o.header || [], C = 0;
|
2017-05-11 18:23:21 +00:00
|
|
|
|
2017-10-17 00:14:32 +00:00
|
|
|
js.forEach(function (JS, R) {
|
|
|
|
keys(JS).filter(function(x) { return JS.hasOwnProperty(x); }).forEach(function(k) {
|
2017-05-11 18:23:21 +00:00
|
|
|
if((C=hdr.indexOf(k)) == -1) hdr[C=hdr.length] = k;
|
2017-10-17 00:14:32 +00:00
|
|
|
var v = JS[k];
|
2017-05-11 18:23:21 +00:00
|
|
|
var t = 'z';
|
2017-05-17 04:23:36 +00:00
|
|
|
var z = "";
|
2017-05-11 18:23:21 +00:00
|
|
|
if(typeof v == 'number') t = 'n';
|
|
|
|
else if(typeof v == 'boolean') t = 'b';
|
|
|
|
else if(typeof v == 'string') t = 's';
|
2017-05-17 04:23:36 +00:00
|
|
|
else if(v instanceof Date) {
|
|
|
|
t = 'd';
|
|
|
|
if(!o.cellDates) { t = 'n'; v = datenum(v); }
|
|
|
|
z = o.dateNF || SSF._table[14];
|
|
|
|
}
|
|
|
|
ws[encode_cell({c:C,r:R+1})] = cell = ({t:t, v:v}/*:any*/);
|
|
|
|
if(z) cell.z = z;
|
2017-05-11 18:23:21 +00:00
|
|
|
});
|
2017-10-17 00:14:32 +00:00
|
|
|
});
|
2017-05-11 18:23:21 +00:00
|
|
|
range.e.c = hdr.length - 1;
|
|
|
|
for(C = 0; C < hdr.length; ++C) ws[encode_col(C) + "1"] = {t:'s', v:hdr[C]};
|
|
|
|
ws['!ref'] = encode_range(range);
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2017-05-13 18:21:22 +00:00
|
|
|
var utils/*:any*/ = {
|
2013-03-31 22:56:45 +00:00
|
|
|
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,
|
2014-06-29 18:29:45 +00:00
|
|
|
format_cell: format_cell,
|
|
|
|
get_formulae: sheet_to_formulae,
|
2013-10-30 22:26:31 +00:00
|
|
|
make_csv: sheet_to_csv,
|
2014-05-29 22:30:03 +00:00
|
|
|
make_json: sheet_to_json,
|
2014-06-29 18:29:45 +00:00
|
|
|
make_formulae: sheet_to_formulae,
|
2017-03-25 01:36:40 +00:00
|
|
|
aoa_to_sheet: aoa_to_sheet,
|
2017-05-11 18:23:21 +00:00
|
|
|
json_to_sheet: json_to_sheet,
|
2017-03-29 19:14:15 +00:00
|
|
|
table_to_sheet: parse_dom_table,
|
|
|
|
table_to_book: table_to_book,
|
2014-06-29 18:29:45 +00:00
|
|
|
sheet_to_csv: sheet_to_csv,
|
2017-12-01 05:48:10 +00:00
|
|
|
sheet_to_txt: sheet_to_txt,
|
2014-05-29 22:30:03 +00:00
|
|
|
sheet_to_json: sheet_to_json,
|
2017-05-16 17:45:35 +00:00
|
|
|
sheet_to_html: HTML_.from_sheet,
|
2017-12-04 04:41:41 +00:00
|
|
|
sheet_to_dif: DIF.from_sheet,
|
|
|
|
sheet_to_slk: SYLK.from_sheet,
|
|
|
|
sheet_to_eth: ETH.from_sheet,
|
2014-06-29 18:29:45 +00:00
|
|
|
sheet_to_formulae: sheet_to_formulae,
|
2017-03-25 01:36:40 +00:00
|
|
|
sheet_to_row_object_array: sheet_to_json
|
2013-03-31 22:56:45 +00:00
|
|
|
};
|
2017-05-11 07:29:59 +00:00
|
|
|
|