2018-07-09 03:46:11 +00:00
|
|
|
/*::
|
|
|
|
type MJRObject = {
|
|
|
|
row: any;
|
|
|
|
isempty: boolean;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
function make_json_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Array<string>*/, header/*:number*/, hdr/*:Array<any>*/, dense/*:boolean*/, o/*:Sheet2JSONOpts*/)/*:MJRObject*/ {
|
|
|
|
var rr = encode_row(R);
|
2020-03-15 07:42:05 +00:00
|
|
|
var defval = o.defval, raw = o.raw || !Object.prototype.hasOwnProperty.call(o, "raw");
|
2018-07-09 03:46:11 +00:00
|
|
|
var isempty = true;
|
|
|
|
var row/*:any*/ = (header === 1) ? [] : {};
|
|
|
|
if(header !== 1) {
|
|
|
|
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 (var C = r.s.c; C <= r.e.c; ++C) {
|
|
|
|
var 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; }
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var v = val.v;
|
|
|
|
switch(val.t){
|
|
|
|
case 'z': if(v == null) break; continue;
|
2021-09-30 07:28:03 +00:00
|
|
|
case 'e': v = (v == 0 ? null : void 0); break;
|
2018-07-09 03:46:11 +00:00
|
|
|
case 's': case 'd': case 'b': case 'n': break;
|
|
|
|
default: throw new Error('unrecognized type ' + val.t);
|
|
|
|
}
|
|
|
|
if(hdr[C] != null) {
|
|
|
|
if(v == null) {
|
2021-09-30 07:28:03 +00:00
|
|
|
if(val.t == "e" && v === null) row[hdr[C]] = null;
|
|
|
|
else if(defval !== undefined) row[hdr[C]] = defval;
|
2018-07-09 03:46:11 +00:00
|
|
|
else if(raw && v === null) row[hdr[C]] = null;
|
|
|
|
else continue;
|
|
|
|
} else {
|
2022-03-20 05:29:24 +00:00
|
|
|
row[hdr[C]] = raw && (val.t !== "n" || (val.t === "n" && o.rawNumbers !== false)) ? v : format_cell(val,v,o);
|
2018-07-09 03:46:11 +00:00
|
|
|
}
|
|
|
|
if(v != null) isempty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { row: row, isempty: isempty };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 [];
|
2018-07-09 03:46:11 +00:00
|
|
|
var val = {t:'n',v:0}, header = 0, offset = 1, hdr/*:Array<any>*/ = [], v=0, vv="";
|
2017-04-09 04:03:19 +00:00
|
|
|
var r = {s:{r:0,c:0},e:{r:0,c:0}};
|
2017-08-09 22:38:23 +00:00
|
|
|
var o = opts || {};
|
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;
|
2019-08-15 06:19:06 +00:00
|
|
|
else if(o.header == null) header = 0;
|
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);
|
2022-03-10 05:31:13 +00:00
|
|
|
var R = r.s.r, C = 0;
|
|
|
|
var header_cnt = {};
|
2017-05-11 07:29:59 +00:00
|
|
|
if(dense && !sheet[R]) sheet[R] = [];
|
2022-03-12 00:24:23 +00:00
|
|
|
var colinfo/*:Array<ColInfo>*/ = o.skipHidden && sheet["!cols"] || [];
|
|
|
|
var rowinfo/*:Array<ColInfo>*/ = o.skipHidden && sheet["!rows"] || [];
|
2014-06-29 18:29:45 +00:00
|
|
|
for(C = r.s.c; C <= r.e.c; ++C) {
|
2022-03-12 00:24:23 +00:00
|
|
|
if(((colinfo[C]||{}).hidden)) continue;
|
2014-06-29 18:29:45 +00:00
|
|
|
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);
|
2022-03-10 05:31:13 +00:00
|
|
|
counter = header_cnt[v] || 0;
|
|
|
|
if(!counter) header_cnt[v] = 1;
|
|
|
|
else {
|
|
|
|
do { vv = v + "_" + (counter++); } while(header_cnt[vv]); header_cnt[v] = counter;
|
|
|
|
header_cnt[vv] = 1;
|
|
|
|
}
|
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
|
|
|
}
|
2014-05-29 22:30:03 +00:00
|
|
|
for (R = r.s.r + offset; R <= r.e.r; ++R) {
|
2022-03-12 00:24:23 +00:00
|
|
|
if ((rowinfo[R]||{}).hidden) continue;
|
2018-07-09 03:46:11 +00:00
|
|
|
var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o);
|
|
|
|
if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) out[outi++] = row.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;
|
2020-06-18 21:34:04 +00:00
|
|
|
txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o));
|
2020-06-18 01:34:04 +00:00
|
|
|
for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34 || o.forceQuotes) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; }
|
2017-05-11 18:23:21 +00:00
|
|
|
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);
|
2022-03-08 01:17:32 +00:00
|
|
|
var w = 0;
|
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,"");
|
2022-03-09 01:44:10 +00:00
|
|
|
if(row || (o.blankrows !== false)) out.push((w++ ? RS : "") + row);
|
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);
|
2022-03-11 05:29:05 +00:00
|
|
|
if(typeof $cptable == 'undefined' || opts.type == 'string') return s;
|
|
|
|
var o = $cptable.utils.encode(1200, s, 'str');
|
2018-02-03 20:46:32 +00:00
|
|
|
return String.fromCharCode(255) + String.fromCharCode(254) + o;
|
2017-04-03 00:16:03 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-09 07:36:02 +00:00
|
|
|
function sheet_add_json(_ws/*:?Worksheet*/, js/*:Array<any>*/, opts)/*:Worksheet*/ {
|
2017-05-11 18:23:21 +00:00
|
|
|
var o = opts || {};
|
2018-01-09 07:36:02 +00:00
|
|
|
var offset = +!o.skipHeader;
|
|
|
|
var ws/*:Worksheet*/ = _ws || ({}/*:any*/);
|
|
|
|
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-05-17 04:23:36 +00:00
|
|
|
var cell/*:Cell*/;
|
2018-01-09 07:36:02 +00:00
|
|
|
var range/*:Range*/ = ({s: {c:0, r:0}, e: {c:_C, r:_R + js.length - 1 + offset}}/*:any*/);
|
|
|
|
if(ws['!ref']) {
|
|
|
|
var _range = safe_decode_range(ws['!ref']);
|
|
|
|
range.e.c = Math.max(range.e.c, _range.e.c);
|
|
|
|
range.e.r = Math.max(range.e.r, _range.e.r);
|
2020-04-20 01:17:44 +00:00
|
|
|
if(_R == -1) { _R = _range.e.r + 1; range.e.r = _R + js.length - 1 + offset; }
|
2020-07-31 03:38:44 +00:00
|
|
|
} else {
|
|
|
|
if(_R == -1) { _R = 0; range.e.r = js.length - 1 + offset; }
|
2018-01-09 07:36:02 +00:00
|
|
|
}
|
2017-12-30 05:40:35 +00:00
|
|
|
var hdr/*:Array<string>*/ = o.header || [], C = 0;
|
2017-05-11 18:23:21 +00:00
|
|
|
|
2018-01-09 07:36:02 +00:00
|
|
|
js.forEach(function (JS, R/*:number*/) {
|
2018-02-28 10:41:49 +00:00
|
|
|
keys(JS).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 = "";
|
2019-11-01 03:09:14 +00:00
|
|
|
var ref = encode_cell({c:_C + C,r:_R + R + offset});
|
2022-03-11 05:29:05 +00:00
|
|
|
cell = ws_get_cell_stub(ws, ref);
|
2018-07-03 12:04:15 +00:00
|
|
|
if(v && typeof v === 'object' && !(v instanceof Date)){
|
2019-11-01 03:09:14 +00:00
|
|
|
ws[ref] = v;
|
2018-07-03 12:04:15 +00:00
|
|
|
} else {
|
|
|
|
if(typeof v == 'number') t = 'n';
|
|
|
|
else if(typeof v == 'boolean') t = 'b';
|
|
|
|
else if(typeof v == 'string') t = 's';
|
|
|
|
else if(v instanceof Date) {
|
|
|
|
t = 'd';
|
|
|
|
if(!o.cellDates) { t = 'n'; v = datenum(v); }
|
2022-05-25 01:45:55 +00:00
|
|
|
z = (cell.z && fmt_is_date(cell.z)) ? cell.z : (o.dateNF || table_fmt[14]);
|
2019-11-01 03:09:14 +00:00
|
|
|
}
|
2021-09-30 07:28:03 +00:00
|
|
|
else if(v === null && o.nullError) { t = 'e'; v = 0; }
|
2019-11-01 03:09:14 +00:00
|
|
|
if(!cell) ws[ref] = cell = ({t:t, v:v}/*:any*/);
|
|
|
|
else {
|
|
|
|
cell.t = t; cell.v = v;
|
|
|
|
delete cell.w; delete cell.R;
|
|
|
|
if(z) cell.z = z;
|
2018-07-03 12:04:15 +00:00
|
|
|
}
|
|
|
|
if(z) cell.z = z;
|
2017-05-17 04:23:36 +00:00
|
|
|
}
|
2017-05-11 18:23:21 +00:00
|
|
|
});
|
2017-10-17 00:14:32 +00:00
|
|
|
});
|
2018-01-09 07:36:02 +00:00
|
|
|
range.e.c = Math.max(range.e.c, _C + hdr.length - 1);
|
|
|
|
var __R = encode_row(_R);
|
|
|
|
if(offset) for(C = 0; C < hdr.length; ++C) ws[encode_col(C + _C) + __R] = {t:'s', v:hdr[C]};
|
2017-05-11 18:23:21 +00:00
|
|
|
ws['!ref'] = encode_range(range);
|
|
|
|
return ws;
|
|
|
|
}
|
2018-01-09 07:36:02 +00:00
|
|
|
function json_to_sheet(js/*:Array<any>*/, opts)/*:Worksheet*/ { return sheet_add_json(null, js, opts); }
|
2017-05-11 18:23:21 +00:00
|
|
|
|
2022-03-11 05:29:05 +00:00
|
|
|
/* get cell, creating a stub if necessary */
|
|
|
|
function ws_get_cell_stub(ws/*:Worksheet*/, R, C/*:?number*/)/*:Cell*/ {
|
|
|
|
/* A1 cell address */
|
|
|
|
if(typeof R == "string") {
|
|
|
|
/* dense */
|
|
|
|
if(Array.isArray(ws)) {
|
|
|
|
var RC = decode_cell(R);
|
|
|
|
if(!ws[RC.r]) ws[RC.r] = [];
|
|
|
|
return ws[RC.r][RC.c] || (ws[RC.r][RC.c] = {t:'z'});
|
|
|
|
}
|
|
|
|
return ws[R] || (ws[R] = {t:'z'});
|
|
|
|
}
|
|
|
|
/* cell address object */
|
|
|
|
if(typeof R != "number") return ws_get_cell_stub(ws, encode_cell(R));
|
|
|
|
/* R and C are 0-based indices */
|
|
|
|
return ws_get_cell_stub(ws, encode_cell({r:R,c:C||0}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find sheet index for given name / validate index */
|
|
|
|
function wb_sheet_idx(wb/*:Workbook*/, sh/*:number|string*/) {
|
|
|
|
if(typeof sh == "number") {
|
|
|
|
if(sh >= 0 && wb.SheetNames.length > sh) return sh;
|
|
|
|
throw new Error("Cannot find sheet # " + sh);
|
|
|
|
} else if(typeof sh == "string") {
|
|
|
|
var idx = wb.SheetNames.indexOf(sh);
|
|
|
|
if(idx > -1) return idx;
|
|
|
|
throw new Error("Cannot find sheet name |" + sh + "|");
|
|
|
|
} else throw new Error("Cannot find sheet |" + sh + "|");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* simple blank workbook object */
|
|
|
|
function book_new()/*:Workbook*/ {
|
|
|
|
return { SheetNames: [], Sheets: {} };
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a worksheet to the end of a given workbook */
|
2022-03-21 01:39:16 +00:00
|
|
|
function book_append_sheet(wb/*:Workbook*/, ws/*:Worksheet*/, name/*:?string*/, roll/*:?boolean*/)/*:string*/ {
|
|
|
|
var i = 1;
|
|
|
|
if(!name) for(; i <= 0xFFFF; ++i, name = undefined) if(wb.SheetNames.indexOf(name = "Sheet" + i) == -1) break;
|
2022-03-11 05:29:05 +00:00
|
|
|
if(!name || wb.SheetNames.length >= 0xFFFF) throw new Error("Too many worksheets");
|
2022-03-21 01:39:16 +00:00
|
|
|
if(roll && wb.SheetNames.indexOf(name) >= 0) {
|
|
|
|
var m = name.match(/(^.*?)(\d+)$/);
|
|
|
|
i = m && +m[2] || 0;
|
|
|
|
var root = m && m[1] || name;
|
|
|
|
for(++i; i <= 0xFFFF; ++i) if(wb.SheetNames.indexOf(name = root + i) == -1) break;
|
|
|
|
}
|
2022-03-11 05:29:05 +00:00
|
|
|
check_ws_name(name);
|
|
|
|
if(wb.SheetNames.indexOf(name) >= 0) throw new Error("Worksheet with name |" + name + "| already exists!");
|
|
|
|
|
|
|
|
wb.SheetNames.push(name);
|
|
|
|
wb.Sheets[name] = ws;
|
2022-03-21 01:39:16 +00:00
|
|
|
return name;
|
2022-03-11 05:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set sheet visibility (visible/hidden/very hidden) */
|
|
|
|
function book_set_sheet_visibility(wb/*:Workbook*/, sh/*:number|string*/, vis/*:number*/) {
|
|
|
|
if(!wb.Workbook) wb.Workbook = {};
|
|
|
|
if(!wb.Workbook.Sheets) wb.Workbook.Sheets = [];
|
|
|
|
|
|
|
|
var idx = wb_sheet_idx(wb, sh);
|
|
|
|
// $FlowIgnore
|
|
|
|
if(!wb.Workbook.Sheets[idx]) wb.Workbook.Sheets[idx] = {};
|
|
|
|
|
|
|
|
switch(vis) {
|
|
|
|
case 0: case 1: case 2: break;
|
|
|
|
default: throw new Error("Bad sheet visibility setting " + vis);
|
|
|
|
}
|
|
|
|
// $FlowIgnore
|
|
|
|
wb.Workbook.Sheets[idx].Hidden = vis;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set number format */
|
|
|
|
function cell_set_number_format(cell/*:Cell*/, fmt/*:string|number*/) {
|
|
|
|
cell.z = fmt;
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set cell hyperlink */
|
|
|
|
function cell_set_hyperlink(cell/*:Cell*/, target/*:string*/, tooltip/*:?string*/) {
|
|
|
|
if(!target) {
|
|
|
|
delete cell.l;
|
|
|
|
} else {
|
|
|
|
cell.l = ({ Target: target }/*:Hyperlink*/);
|
|
|
|
if(tooltip) cell.l.Tooltip = tooltip;
|
|
|
|
}
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
function cell_set_internal_link(cell/*:Cell*/, range/*:string*/, tooltip/*:?string*/) { return cell_set_hyperlink(cell, "#" + range, tooltip); }
|
|
|
|
|
|
|
|
/* add to cell comments */
|
|
|
|
function cell_add_comment(cell/*:Cell*/, text/*:string*/, author/*:?string*/) {
|
|
|
|
if(!cell.c) cell.c = [];
|
|
|
|
cell.c.push({t:text, a:author||"SheetJS"});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set array formula and flush related cells */
|
|
|
|
function sheet_set_array_formula(ws/*:Worksheet*/, range, formula/*:string*/, dynamic/*:boolean*/) {
|
|
|
|
var rng = typeof range != "string" ? range : safe_decode_range(range);
|
|
|
|
var rngstr = typeof range == "string" ? range : encode_range(range);
|
|
|
|
for(var R = rng.s.r; R <= rng.e.r; ++R) for(var C = rng.s.c; C <= rng.e.c; ++C) {
|
|
|
|
var cell = ws_get_cell_stub(ws, R, C);
|
|
|
|
cell.t = 'n';
|
|
|
|
cell.F = rngstr;
|
|
|
|
delete cell.v;
|
|
|
|
if(R == rng.s.r && C == rng.s.c) {
|
|
|
|
cell.f = formula;
|
|
|
|
if(dynamic) cell.D = true;
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 03:37:09 +00:00
|
|
|
var wsr = decode_range(ws["!ref"]);
|
|
|
|
if(wsr.s.r > rng.s.r) wsr.s.r = rng.s.r;
|
|
|
|
if(wsr.s.c > rng.s.c) wsr.s.c = rng.s.c;
|
|
|
|
if(wsr.e.r < rng.e.r) wsr.e.r = rng.e.r;
|
|
|
|
if(wsr.e.c < rng.e.c) wsr.e.c = rng.e.c;
|
2022-05-22 23:51:41 +00:00
|
|
|
ws["!ref"] = encode_range(wsr);
|
2022-03-11 05:29:05 +00:00
|
|
|
return ws;
|
|
|
|
}
|
2017-05-11 07:29:59 +00:00
|
|
|
|