2017-04-17 02:08:23 +00:00
|
|
|
function decode_row(rowstr/*:string*/)/*:number*/ { return parseInt(unfix_row(rowstr),10) - 1; }
|
|
|
|
function encode_row(row/*:number*/)/*:string*/ { return "" + (row + 1); }
|
|
|
|
function fix_row(cstr/*:string*/)/*:string*/ { return cstr.replace(/([A-Z]|^)(\d+)$/,"$1$$$2"); }
|
|
|
|
function unfix_row(cstr/*:string*/)/*:string*/ { return cstr.replace(/\$(\d+)$/,"$1"); }
|
|
|
|
|
|
|
|
function decode_col(colstr/*:string*/)/*:number*/ { var c = unfix_col(colstr), d = 0, i = 0; for(; i !== c.length; ++i) d = 26*d + c.charCodeAt(i) - 64; return d - 1; }
|
2019-08-31 19:29:50 +00:00
|
|
|
function encode_col(col/*:number*/)/*:string*/ { if(col < 0) throw new Error("invalid column " + col); var s=""; for(++col; col; col=Math.floor((col-1)/26)) s = String.fromCharCode(((col-1)%26) + 65) + s; return s; }
|
2017-04-17 02:08:23 +00:00
|
|
|
function fix_col(cstr/*:string*/)/*:string*/ { return cstr.replace(/^([A-Z])/,"$$$1"); }
|
|
|
|
function unfix_col(cstr/*:string*/)/*:string*/ { return cstr.replace(/^\$([A-Z])/,"$1"); }
|
|
|
|
|
|
|
|
function split_cell(cstr/*:string*/)/*:Array<string>*/ { return cstr.replace(/(\$?[A-Z]*)(\$?\d*)/,"$1,$2").split(","); }
|
2020-05-16 19:45:54 +00:00
|
|
|
function decode_cell(cstr/*:string*/)/*:CellAddress*/ {
|
|
|
|
var R = 0, C = 0;
|
|
|
|
for(var i = 0; i < cstr.length; ++i) {
|
|
|
|
var cc = cstr.charCodeAt(i);
|
|
|
|
if(cc >= 48 && cc <= 57) R = 10 * R + (cc - 48);
|
|
|
|
else if(cc >= 65 && cc <= 90) C = 26 * C + (cc - 64);
|
|
|
|
}
|
|
|
|
return { c: C - 1, r:R - 1 };
|
|
|
|
}
|
|
|
|
function encode_cell(cell/*:CellAddress*/)/*:string*/ {
|
|
|
|
var col = cell.c + 1;
|
|
|
|
var s="";
|
|
|
|
for(; col; col=((col-1)/26)|0) s = String.fromCharCode(((col-1)%26) + 65) + s;
|
|
|
|
return s + (cell.r + 1);
|
|
|
|
}
|
2020-07-31 03:38:44 +00:00
|
|
|
function decode_range(range/*:string*/)/*:Range*/ {
|
|
|
|
var idx = range.indexOf(":");
|
|
|
|
if(idx == -1) return { s: decode_cell(range), e: decode_cell(range) };
|
|
|
|
return { s: decode_cell(range.slice(0, idx)), e: decode_cell(range.slice(idx + 1)) };
|
|
|
|
}
|
2017-04-17 02:08:23 +00:00
|
|
|
/*# if only one arg, it is assumed to be a Range. If 2 args, both are cell addresses */
|
|
|
|
function encode_range(cs/*:CellAddrSpec|Range*/,ce/*:?CellAddrSpec*/)/*:string*/ {
|
|
|
|
if(typeof ce === 'undefined' || typeof ce === 'number') {
|
|
|
|
/*:: if(!(cs instanceof Range)) throw "unreachable"; */
|
|
|
|
return encode_range(cs.s, cs.e);
|
|
|
|
}
|
|
|
|
/*:: if((cs instanceof Range)) throw "unreachable"; */
|
|
|
|
if(typeof cs !== 'string') cs = encode_cell((cs/*:any*/));
|
|
|
|
if(typeof ce !== 'string') ce = encode_cell((ce/*:any*/));
|
|
|
|
/*:: if(typeof cs !== 'string') throw "unreachable"; */
|
|
|
|
/*:: if(typeof ce !== 'string') throw "unreachable"; */
|
|
|
|
return cs == ce ? cs : cs + ":" + ce;
|
|
|
|
}
|
2022-05-30 08:40:51 +00:00
|
|
|
function fix_range(a1/*:string*/)/*:string*/ {
|
|
|
|
var s = decode_range(a1);
|
|
|
|
return "$" + encode_col(s.s.c) + "$" + encode_row(s.s.r) + ":$" + encode_col(s.e.c) + "$" + encode_row(s.e.r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// List of invalid characters needs to be tested further
|
|
|
|
function formula_quote_sheet_name(sname/*:string*/, opts)/*:string*/ {
|
|
|
|
if(!sname && !(opts && opts.biff <= 5 && opts.biff >= 2)) throw new Error("empty sheet name");
|
|
|
|
if (/[^\w\u4E00-\u9FFF\u3040-\u30FF]/.test(sname)) return "'" + sname.replace(/'/g, "''") + "'";
|
|
|
|
return sname;
|
|
|
|
}
|
2017-04-17 02:08:23 +00:00
|
|
|
|
|
|
|
function safe_decode_range(range/*:string*/)/*:Range*/ {
|
|
|
|
var o = {s:{c:0,r:0},e:{c:0,r:0}};
|
|
|
|
var idx = 0, i = 0, cc = 0;
|
|
|
|
var len = range.length;
|
|
|
|
for(idx = 0; i < len; ++i) {
|
|
|
|
if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
|
|
|
|
idx = 26*idx + cc;
|
|
|
|
}
|
|
|
|
o.s.c = --idx;
|
|
|
|
|
|
|
|
for(idx = 0; i < len; ++i) {
|
|
|
|
if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
|
|
|
|
idx = 10*idx + cc;
|
|
|
|
}
|
|
|
|
o.s.r = --idx;
|
|
|
|
|
2022-01-10 18:45:50 +00:00
|
|
|
if(i === len || cc != 10) { o.e.c=o.s.c; o.e.r=o.s.r; return o; }
|
|
|
|
++i;
|
2017-04-17 02:08:23 +00:00
|
|
|
|
|
|
|
for(idx = 0; i != len; ++i) {
|
|
|
|
if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
|
|
|
|
idx = 26*idx + cc;
|
|
|
|
}
|
|
|
|
o.e.c = --idx;
|
|
|
|
|
|
|
|
for(idx = 0; i != len; ++i) {
|
|
|
|
if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
|
|
|
|
idx = 10*idx + cc;
|
|
|
|
}
|
|
|
|
o.e.r = --idx;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
function safe_format_cell(cell/*:Cell*/, v/*:any*/) {
|
|
|
|
var q = (cell.t == 'd' && v instanceof Date);
|
2022-03-20 01:54:41 +00:00
|
|
|
if(cell.z != null) try { return (cell.w = SSF_format(cell.z, q ? datenum(v) : v)); } catch(e) { }
|
|
|
|
try { return (cell.w = SSF_format((cell.XF||{}).numFmtId||(q ? 14 : 0), q ? datenum(v) : v)); } catch(e) { return ''+v; }
|
2017-04-17 02:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function format_cell(cell/*:Cell*/, v/*:any*/, o/*:any*/) {
|
|
|
|
if(cell == null || cell.t == null || cell.t == 'z') return "";
|
|
|
|
if(cell.w !== undefined) return cell.w;
|
|
|
|
if(cell.t == 'd' && !cell.z && o && o.dateNF) cell.z = o.dateNF;
|
2021-09-26 22:51:19 +00:00
|
|
|
if(cell.t == "e") return BErr[cell.v] || cell.v;
|
2017-07-27 20:07:51 +00:00
|
|
|
if(v == undefined) return safe_format_cell(cell, cell.v);
|
|
|
|
return safe_format_cell(cell, v);
|
2017-04-17 02:08:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 01:36:40 +00:00
|
|
|
function sheet_to_workbook(sheet/*:Worksheet*/, opts)/*:Workbook*/ {
|
|
|
|
var n = opts && opts.sheet ? opts.sheet : "Sheet1";
|
|
|
|
var sheets = {}; sheets[n] = sheet;
|
|
|
|
return { SheetNames: [n], Sheets: sheets };
|
|
|
|
}
|
|
|
|
|
2023-10-11 20:22:13 +00:00
|
|
|
function sheet_new(opts) {
|
|
|
|
var out = {};
|
|
|
|
var o = opts || {};
|
|
|
|
if(o.dense) out["!data"] = [];
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-01-09 07:36:02 +00:00
|
|
|
function sheet_add_aoa(_ws/*:?Worksheet*/, data/*:AOA*/, opts/*:?any*/)/*:Worksheet*/ {
|
2017-03-25 01:36:40 +00:00
|
|
|
var o = opts || {};
|
2022-10-24 01:05:59 +00:00
|
|
|
var dense = _ws ? (_ws["!data"] != null) : o.dense;
|
2018-01-09 07:36:02 +00:00
|
|
|
if(DENSE != null && dense == null) dense = DENSE;
|
2022-10-24 01:05:59 +00:00
|
|
|
var ws/*:Worksheet*/ = _ws || ({}/*:any*/);
|
|
|
|
if(dense && !ws["!data"]) ws["!data"] = [];
|
2018-01-09 07:36:02 +00:00
|
|
|
var _R = 0, _C = 0;
|
|
|
|
if(ws && o.origin != null) {
|
|
|
|
if(typeof o.origin == 'number') _R = o.origin;
|
|
|
|
else {
|
|
|
|
var _origin/*:CellAddress*/ = typeof o.origin == "string" ? decode_cell(o.origin) : o.origin;
|
|
|
|
_R = _origin.r; _C = _origin.c;
|
|
|
|
}
|
|
|
|
}
|
2017-03-25 01:36:40 +00:00
|
|
|
var range/*:Range*/ = ({s: {c:10000000, r:10000000}, e: {c:0, r:0}}/*:any*/);
|
2024-04-05 01:20:28 +00:00
|
|
|
if(ws["!ref"]){
|
2018-01-09 07:36:02 +00:00
|
|
|
var _range = safe_decode_range(ws['!ref']);
|
|
|
|
range.s.c = _range.s.c;
|
|
|
|
range.s.r = _range.s.r;
|
|
|
|
range.e.c = Math.max(range.e.c, _range.e.c);
|
|
|
|
range.e.r = Math.max(range.e.r, _range.e.r);
|
2024-04-05 01:20:28 +00:00
|
|
|
if(_R == -1) range.e.r = _R = (ws["!ref"] ? _range.e.r + 1 : 0);
|
|
|
|
} else {
|
|
|
|
range.s.c = range.e.c = range.s.r = range.e.r = 0;
|
2018-01-09 07:36:02 +00:00
|
|
|
}
|
2024-04-05 01:20:28 +00:00
|
|
|
var row = [], seen = false;
|
2017-03-25 01:36:40 +00:00
|
|
|
for(var R = 0; R != data.length; ++R) {
|
2018-08-15 19:22:47 +00:00
|
|
|
if(!data[R]) continue;
|
|
|
|
if(!Array.isArray(data[R])) throw new Error("aoa_to_sheet expects an array of arrays");
|
2022-10-24 01:05:59 +00:00
|
|
|
var __R = _R + R, __Rstr = "" + (__R + 1);
|
|
|
|
if(dense) {
|
|
|
|
if(!ws["!data"][__R]) ws["!data"][__R] = [];
|
|
|
|
row = ws["!data"][__R];
|
|
|
|
}
|
2017-03-25 01:36:40 +00:00
|
|
|
for(var C = 0; C != data[R].length; ++C) {
|
|
|
|
if(typeof data[R][C] === 'undefined') continue;
|
|
|
|
var cell/*:Cell*/ = ({v: data[R][C] }/*:any*/);
|
2022-10-24 01:05:59 +00:00
|
|
|
var __C = _C + C;
|
2018-01-09 07:36:02 +00:00
|
|
|
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;
|
2024-04-05 01:20:28 +00:00
|
|
|
seen = true;
|
2018-09-19 10:58:14 +00:00
|
|
|
if(data[R][C] && typeof data[R][C] === 'object' && !Array.isArray(data[R][C]) && !(data[R][C] instanceof Date)) cell = data[R][C];
|
|
|
|
else {
|
|
|
|
if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; }
|
2021-09-30 07:28:03 +00:00
|
|
|
if(cell.v === null) {
|
|
|
|
if(cell.f) cell.t = 'n';
|
|
|
|
else if(o.nullError) { cell.t = 'e'; cell.v = 0; }
|
|
|
|
else if(!o.sheetStubs) continue;
|
|
|
|
else cell.t = 'z';
|
|
|
|
}
|
2024-04-27 23:49:12 +00:00
|
|
|
else if(typeof cell.v === 'number') {
|
|
|
|
if(isFinite(cell.v)) cell.t = 'n';
|
|
|
|
else if(isNaN(cell.v)) { cell.t = 'e'; cell.v = 0x0F; /* #VALUE! */ }
|
|
|
|
else { cell.t = 'e'; cell.v = 0x07; /*# DIV/0 */ }
|
|
|
|
}
|
2018-09-19 10:58:14 +00:00
|
|
|
else if(typeof cell.v === 'boolean') cell.t = 'b';
|
|
|
|
else if(cell.v instanceof Date) {
|
2022-03-20 01:54:41 +00:00
|
|
|
cell.z = o.dateNF || table_fmt[14];
|
2023-06-23 09:48:47 +00:00
|
|
|
if(!o.UTC) cell.v = local_to_utc(cell.v);
|
2022-06-06 23:05:27 +00:00
|
|
|
if(o.cellDates) { cell.t = 'd'; cell.w = SSF_format(cell.z, datenum(cell.v, o.date1904)); }
|
|
|
|
else { cell.t = 'n'; cell.v = datenum(cell.v, o.date1904); cell.w = SSF_format(cell.z, cell.v); }
|
2018-09-19 10:58:14 +00:00
|
|
|
}
|
|
|
|
else cell.t = 's';
|
2017-03-25 01:36:40 +00:00
|
|
|
}
|
2018-01-09 07:36:02 +00:00
|
|
|
if(dense) {
|
2022-10-24 01:05:59 +00:00
|
|
|
if(row[__C] && row[__C].z) cell.z = row[__C].z;
|
|
|
|
row[__C] = cell;
|
2017-04-08 06:55:35 +00:00
|
|
|
} else {
|
2022-10-24 01:05:59 +00:00
|
|
|
var cell_ref = encode_col(__C) + __Rstr/*:any*/;
|
2019-12-23 03:19:56 +00:00
|
|
|
if(ws[cell_ref] && ws[cell_ref].z) cell.z = ws[cell_ref].z;
|
2017-04-08 06:55:35 +00:00
|
|
|
ws[cell_ref] = cell;
|
|
|
|
}
|
2017-03-25 01:36:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-05 01:20:28 +00:00
|
|
|
if(seen && range.s.c < 10400000) ws['!ref'] = encode_range(range);
|
2017-03-25 01:36:40 +00:00
|
|
|
return ws;
|
|
|
|
}
|
2018-01-09 07:36:02 +00:00
|
|
|
function aoa_to_sheet(data/*:AOA*/, opts/*:?any*/)/*:Worksheet*/ { return sheet_add_aoa(null, data, opts); }
|