styled-exceljs 0.21.0 → 0.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/xlsx.mjs CHANGED
@@ -3,7 +3,7 @@
3
3
  /*exported XLSX */
4
4
  /*global process:false, Buffer:false, ArrayBuffer:false, DataView:false, Deno:false */
5
5
  var XLSX = {};
6
- XLSX.version = '0.21.0';
6
+ XLSX.version = '0.21.1';
7
7
  var current_codepage = 1200, current_ansi = 1252;
8
8
  /*:: declare var cptable:any; */
9
9
  /*global cptable:true, window */
@@ -11238,6 +11238,188 @@ function process_col(coll/*:ColInfo*/) {
11238
11238
  if(coll.customWidth) delete coll.customWidth;
11239
11239
  }
11240
11240
 
11241
+ var XLSX_COL_WIDTH_MAX = 255, XLSX_COL_WIDTH_PADDING = 5;
11242
+ function clamp_col_width(width) {
11243
+ return width > XLSX_COL_WIDTH_MAX ? XLSX_COL_WIDTH_MAX : width < 0 ? 0 : width;
11244
+ }
11245
+ function px2col(px) {
11246
+ var width = char2width(px2char(Math.max(0, px)));
11247
+ return clamp_col_width(width);
11248
+ }
11249
+ function col2px(col) {
11250
+ if(!col) return 64;
11251
+ if(col.wpx != null) return col.wpx;
11252
+ if(col.width != null) return width2px(col.width);
11253
+ if(col.wch != null) return width2px(char2width(col.wch));
11254
+ return 64;
11255
+ }
11256
+ function set_col_width_from_px(col, px) {
11257
+ if(!col) col = ({}/*:any*/);
11258
+ col.wpx = Math.max(0, Math.ceil(px));
11259
+ col.wch = px2char(col.wpx);
11260
+ col.width = px2col(col.wpx);
11261
+ col.MDW = MDW;
11262
+ col.bestFit = true;
11263
+ col.customWidth = true;
11264
+ return col;
11265
+ }
11266
+ function style_font_size_pt(style) {
11267
+ var font = style && style.font || {};
11268
+ var sz = +font.sz;
11269
+ return sz > 0 ? sz : 11;
11270
+ }
11271
+ function style_font_family(style) {
11272
+ var font = style && style.font || {};
11273
+ return font.name || "Calibri";
11274
+ }
11275
+ function css_font_from_style(style) {
11276
+ var font = style && style.font || {};
11277
+ var parts = [];
11278
+ if(font.italic) parts.push("italic");
11279
+ if(font.bold) parts.push("bold");
11280
+ parts.push(style_font_size_pt(style) + "pt");
11281
+ var name = style_font_family(style);
11282
+ if(/[,\s'"]/.test(name)) name = '"' + String(name).replace(/"/g, '\\"') + '"';
11283
+ parts.push(name);
11284
+ return parts.join(" ");
11285
+ }
11286
+ function fallback_char_width(ch, fpx) {
11287
+ var cc = ch.charCodeAt(0);
11288
+ if(cc == 9 || cc == 32) return fpx * 0.33;
11289
+ if(cc >= 48 && cc <= 57) return fpx * 0.52;
11290
+ if(cc >= 65 && cc <= 90) return fpx * 0.62;
11291
+ if(cc >= 97 && cc <= 122) {
11292
+ if("iljtfr".indexOf(ch) > -1) return fpx * 0.28;
11293
+ if("mw".indexOf(ch) > -1) return fpx * 0.82;
11294
+ return fpx * 0.50;
11295
+ }
11296
+ if(cc >= 0x2E80) return fpx;
11297
+ return fpx * 0.52;
11298
+ }
11299
+ function fallback_text_width(text, style) {
11300
+ var fpx = pt2px_browser(style_font_size_pt(style)), width = 0;
11301
+ for(var i = 0; i < text.length; ++i) width += fallback_char_width(text.charAt(i), fpx);
11302
+ return width;
11303
+ }
11304
+ function measure_text_width(text/*:string*/, style/*:?CellStyle*/, opts/*:?any*/)/*:number*/ {
11305
+ var o = opts || {};
11306
+ var str = text == null ? "" : String(text);
11307
+ var lines = str.split(/\r\n|\n|\r/g), max = 0, i = 0, w = 0;
11308
+ if(o.measureText) for(i = 0; i < lines.length; ++i) {
11309
+ w = +o.measureText(lines[i], css_font_from_style(style), style || {});
11310
+ if(isFinite(w) && w > max) max = w;
11311
+ }
11312
+ else {
11313
+ var canvas = o.canvas, ctx = null;
11314
+ if(!canvas && typeof document !== "undefined" && document.createElement) try { canvas = document.createElement("canvas"); } catch(e) {}
11315
+ try { ctx = canvas && canvas.getContext && canvas.getContext("2d"); } catch(e) { ctx = null; }
11316
+ if(ctx && ctx.measureText) {
11317
+ ctx.font = css_font_from_style(style);
11318
+ for(i = 0; i < lines.length; ++i) {
11319
+ w = ctx.measureText(lines[i]).width;
11320
+ if(isFinite(w) && w > max) max = w;
11321
+ }
11322
+ } else for(i = 0; i < lines.length; ++i) {
11323
+ w = fallback_text_width(lines[i], style || {});
11324
+ if(isFinite(w) && w > max) max = w;
11325
+ }
11326
+ }
11327
+ return max;
11328
+ }
11329
+ function auto_fit_cell_text(cell/*:Cell*/, opts/*:?any*/)/*:string*/ {
11330
+ if(!cell || cell.v == null) return "";
11331
+ if(cell.w == null && cell.t != 'z') format_cell(cell);
11332
+ return String(cell.w != null ? cell.w : cell.v);
11333
+ }
11334
+ function text_width_segments(text, wrap) {
11335
+ var lines = String(text == null ? "" : text).split(/\r\n|\n|\r/g), out = [], i = 0, j = 0, parts = null;
11336
+ if(!wrap) return lines;
11337
+ for(i = 0; i < lines.length; ++i) {
11338
+ parts = lines[i].split(/[\t \u00A0\-\/]+/);
11339
+ for(j = 0; j < parts.length; ++j) if(parts[j]) out.push(parts[j]);
11340
+ if(!parts.length || (parts.length == 1 && !parts[0])) out.push(lines[i]);
11341
+ }
11342
+ return out.length ? out : [""];
11343
+ }
11344
+ function effective_cell_style(ws/*:Worksheet*/, cell/*:?Cell*/, R/*:number*/, C/*:number*/) {
11345
+ var out = ({}/*:any*/), cols = ws["!cols"] || [], rows = ws["!rows"] || [];
11346
+ if(cols[C] && cols[C].s) extend_style_obj(out, cols[C].s);
11347
+ if(rows[R] && rows[R].s) extend_style_obj(out, rows[R].s);
11348
+ if(cell && cell.s) extend_style_obj(out, cell.s);
11349
+ return out;
11350
+ }
11351
+ function measure_cell_text_width(cell/*:Cell*/, style/*:CellStyle*/, opts/*:?any*/)/*:number*/ {
11352
+ var o = opts || {}, text = auto_fit_cell_text(cell, o), align = style && style.alignment || {};
11353
+ var wrap = !!align.wrapText, segments = text_width_segments(text, wrap), max = 0, w = 0;
11354
+ for(var i = 0; i < segments.length; ++i) {
11355
+ w = measure_text_width(segments[i], style, o);
11356
+ if(w > max) max = w;
11357
+ }
11358
+ var indent = +align.indent || 0;
11359
+ if(indent > 0) max += indent * 3 * Math.max(1, MDW);
11360
+ var rotation = +align.textRotation || 0;
11361
+ if(rotation == 255) rotation = 90;
11362
+ if(rotation > 90) rotation = 90 - rotation;
11363
+ if(rotation < 0) rotation = -rotation;
11364
+ if(rotation > 0 && rotation < 90) {
11365
+ var rad = rotation * Math.PI / 180, fpx = pt2px_browser(style_font_size_pt(style));
11366
+ max = Math.abs(max * Math.cos(rad)) + Math.abs(fpx * Math.sin(rad));
11367
+ } else if(rotation >= 90) max = Math.max(MDW + XLSX_COL_WIDTH_PADDING, pt2px_browser(style_font_size_pt(style)) + 2);
11368
+ return max + (o.padding != null ? +o.padding : XLSX_COL_WIDTH_PADDING);
11369
+ }
11370
+ function merge_start_map(merges) {
11371
+ var out = {};
11372
+ for(var i = 0; i < merges.length; ++i) out[merges[i].s.r + ":" + merges[i].s.c] = merges[i];
11373
+ return out;
11374
+ }
11375
+ function is_covered_merge(merges, R, C) {
11376
+ for(var i = 0; i < merges.length; ++i) {
11377
+ var m = merges[i];
11378
+ if(m.s.r <= R && R <= m.e.r && m.s.c <= C && C <= m.e.c) return !(m.s.r == R && m.s.c == C);
11379
+ }
11380
+ return false;
11381
+ }
11382
+ function auto_fit_columns(ws/*:Worksheet*/, opts/*:?any*/)/*:Array<ColInfo>*/ {
11383
+ var o = opts || {}, oldMDW = MDW;
11384
+ if(o.MDW) MDW = +o.MDW || MDW;
11385
+ var r = o.range ? (typeof o.range == "string" ? decode_range(o.range) : o.range) : decode_range(ws["!ref"] || "A1");
11386
+ var base = ws["!cols"] || [], rows = ws["!rows"] || [], cols = [], widths = [];
11387
+ var minpx = o.minPx != null ? +o.minPx : o.min != null ? width2px(char2width(+o.min)) : 0;
11388
+ var maxpx = o.maxPx != null ? +o.maxPx : width2px(XLSX_COL_WIDTH_MAX);
11389
+ var merges = o.includeMerged === false ? [] : (ws["!merges"] || []);
11390
+ var starts = merge_start_map(merges), dense = ws["!data"] != null;
11391
+ var C = 0, R = 0, span = 0, m = null, j = 0, cell = null, style = null, need = 0, have = 0;
11392
+ for(C = r.s.c; C <= r.e.c; ++C) {
11393
+ cols[C] = dup(base[C] || {});
11394
+ if(cols[C] && cols[C].hidden && o.skipHidden) continue;
11395
+ widths[C] = cols[C] && (cols[C].wpx != null || cols[C].width != null || cols[C].wch != null) ? col2px(cols[C]) : minpx;
11396
+ }
11397
+ for(R = r.s.r; R <= r.e.r; ++R) {
11398
+ if(rows[R] && rows[R].hidden && o.skipHidden) continue;
11399
+ for(C = r.s.c; C <= r.e.c; ++C) {
11400
+ if(cols[C] && cols[C].hidden && o.skipHidden) continue;
11401
+ if(is_covered_merge(merges, R, C)) continue;
11402
+ cell = dense ? (ws["!data"][R]||[])[C] : ws[encode_cell({r:R,c:C})];
11403
+ if(!cell || cell.v == null) continue;
11404
+ style = effective_cell_style(ws, cell, R, C);
11405
+ need = measure_cell_text_width(cell, style, o);
11406
+ if(style && style.alignment && style.alignment.shrinkToFit && widths[C]) need = Math.min(need, widths[C]);
11407
+ m = starts[R + ":" + C];
11408
+ span = m ? m.e.c - m.s.c + 1 : 1;
11409
+ if(span > 1) {
11410
+ have = 0;
11411
+ for(j = 0; j < span; ++j) have += widths[C+j] || minpx;
11412
+ if(need > have) for(j = 0; j < span; ++j) widths[C+j] = Math.min(maxpx, Math.max(widths[C+j] || minpx, (widths[C+j] || minpx) + (need - have) / span));
11413
+ } else widths[C] = Math.min(maxpx, Math.max(widths[C] || minpx, need));
11414
+ }
11415
+ }
11416
+ for(C = r.s.c; C <= r.e.c; ++C) if(widths[C] != null) cols[C] = set_col_width_from_px(cols[C] || {}, Math.min(maxpx, Math.max(minpx, widths[C])));
11417
+ if(o.set === false) { MDW = oldMDW; return cols; }
11418
+ ws["!cols"] = cols;
11419
+ MDW = oldMDW;
11420
+ return cols;
11421
+ }
11422
+
11241
11423
  var DEF_PPI = 96, PPI = DEF_PPI;
11242
11424
  function px2pt(px) { return px * 96 / PPI; }
11243
11425
  function pt2px(pt) { return pt * PPI / 96; }
@@ -16297,8 +16479,11 @@ function col_obj_w(C/*:number*/, col) {
16297
16479
  else if(col.wch != null) wch = col.wch;
16298
16480
  if(wch > -1) { p.width = char2width(wch); p.customWidth = 1; }
16299
16481
  else if(col.width != null) p.width = col.width;
16482
+ if(p.width != null) p.width = clamp_col_width(p.width);
16300
16483
  if(col.hidden) p.hidden = true;
16301
16484
  if(col.level != null) { p.outlineLevel = p.level = col.level; }
16485
+ if(col.bestFit || col.bestfit) p.bestFit = true;
16486
+ if(col.customWidth || col.customwidth) p.customWidth = 1;
16302
16487
  return p;
16303
16488
  }
16304
16489
 
@@ -16656,6 +16841,8 @@ function parse_ws_xml_cols(columns, cols) {
16656
16841
  for(var coli = 0; coli != cols.length; ++coli) {
16657
16842
  var coll = parsexmltag(cols[coli], true);
16658
16843
  if(coll.hidden) coll.hidden = parsexmlbool(coll.hidden);
16844
+ if(coll.bestFit) coll.bestFit = parsexmlbool(coll.bestFit);
16845
+ if(coll.customWidth) coll.customWidth = parsexmlbool(coll.customWidth);
16659
16846
  var colm=parseInt(coll.min, 10)-1, colM=parseInt(coll.max,10)-1;
16660
16847
  if(coll.outlineLevel) coll.level = (+coll.outlineLevel || 0);
16661
16848
  delete coll.min; delete coll.max; coll.width = +coll.width;
@@ -23813,8 +24000,6 @@ function html_cell_style(cell/*:Cell*/, opts/*:Sheet2HTMLOpts*/) {
23813
24000
  var alignment = s.alignment || {};
23814
24001
  if(alignment.horizontal) css.push("text-align:" + alignment.horizontal);
23815
24002
  if(alignment.vertical) css.push("vertical-align:" + alignment.vertical);
23816
- if(alignment.wrapText) css.push("white-space:normal");
23817
- if(alignment.shrinkToFit) css.push("font-size:smaller");
23818
24003
  if(alignment.textRotation != null && alignment.textRotation !== 0) {
23819
24004
  var deg = alignment.textRotation == 255 ? 90 : alignment.textRotation > 90 ? 90 - alignment.textRotation : alignment.textRotation;
23820
24005
  css.push("transform:rotate(" + deg + "deg)");
@@ -23827,12 +24012,29 @@ function html_cell_style(cell/*:Cell*/, opts/*:Sheet2HTMLOpts*/) {
23827
24012
  add_html_border(css, "bottom", border.bottom);
23828
24013
  return css.join(";");
23829
24014
  }
24015
+ function html_cell_layout_style(cell/*:Cell*/, opts/*:Sheet2HTMLOpts*/, C/*:number*/, CS/*:number*/, cols/*:Array<ColInfo>*/) {
24016
+ if(!opts || !cell) return "";
24017
+ var s = cell.s || {}, alignment = s.alignment || {}, css = [];
24018
+ if(opts.browserPixels || opts.autoFit) css.push("box-sizing:border-box;padding:0 2px;min-width:0");
24019
+ if(alignment.wrapText) css.push("white-space:pre-wrap;overflow-wrap:normal;word-break:normal");
24020
+ else {
24021
+ css.push("white-space:pre");
24022
+ if(alignment.shrinkToFit || opts.overflow == "clip" || opts.overflow == "hidden") css.push("overflow:hidden;text-overflow:clip");
24023
+ else css.push("overflow:visible");
24024
+ }
24025
+ if(alignment.shrinkToFit && cols && C != null) {
24026
+ var have = 0, span = CS || 1;
24027
+ for(var j = 0; j < span; ++j) have += html_col_width(cols[C+j]);
24028
+ var need = measure_cell_text_width(cell, s, opts);
24029
+ if(have > 0 && need > have) {
24030
+ var scale = Math.max(0.25, Math.min(1, have / need));
24031
+ css.push("font-size:" + Math.max(1, style_font_size_pt(s) * scale) + "pt");
24032
+ }
24033
+ }
24034
+ return css.join(";");
24035
+ }
23830
24036
  function html_col_width(col) {
23831
- if(!col) return 64;
23832
- if(col.wpx != null) return col.wpx;
23833
- if(col.width != null) return width2px(col.width);
23834
- if(col.wch != null) return width2px(char2width(col.wch));
23835
- return 64;
24037
+ return col2px(col);
23836
24038
  }
23837
24039
  function html_row_height(row) {
23838
24040
  if(!row) return 20;
@@ -23847,7 +24049,7 @@ function make_html_row(ws/*:Worksheet*/, r/*:Range*/, R/*:number*/, o/*:Sheet2HT
23847
24049
  var sp = ({}/*:any*/);
23848
24050
  var dense = ws["!data"] != null;
23849
24051
  var row = (ws["!rows"]||[])[R];
23850
- var cols = ws["!cols"] || [];
24052
+ var cols = o && o._htmlCols || ws["!cols"] || [];
23851
24053
  for(var C = r.s.c; C <= r.e.c; ++C) {
23852
24054
  var RS = 0, CS = 0;
23853
24055
  for(var j = 0; j < M.length; ++j) {
@@ -23890,6 +24092,8 @@ function make_html_row(ws/*:Worksheet*/, r/*:Range*/, R/*:number*/, o/*:Sheet2HT
23890
24092
  if(cell.l && (cell.l.Target || "#").charAt(0) != "#" && (!o.sanitizeLinks || (cell.l.Target || "").slice(0, 11).toLowerCase() != 'javascript:')) w = '<a href="' + escapehtml(cell.l.Target) +'">' + w + '</a>';
23891
24093
  }
23892
24094
  var cstyle = html_cell_style(stylecell, o);
24095
+ var lstyle = html_cell_layout_style(stylecell, o, C, CS, cols);
24096
+ if(lstyle) cstyle = cstyle ? cstyle + ";" + lstyle : lstyle;
23893
24097
  if(cstyle) sp.style = cstyle;
23894
24098
  sp.id = (o.id || "sjs") + "-" + coord;
23895
24099
  oo.push(writextag('td', w, sp));
@@ -23921,9 +24125,9 @@ function html_to_workbook(str/*:string*/, opts)/*:Workbook*/ {
23921
24125
  }
23922
24126
 
23923
24127
  var HTML_EMU_PER_PIXEL = 9525;
23924
- function html_anchor_pos(ws, anchor) {
24128
+ function html_anchor_pos(ws, anchor, opts) {
23925
24129
  var out = {left:0, top:0, width:480, height:288};
23926
- var cols = ws["!cols"] || [], rows = ws["!rows"] || [];
24130
+ var cols = opts && opts._htmlCols || ws["!cols"] || [], rows = ws["!rows"] || [];
23927
24131
  var from = anchor && anchor.from || {col:0,row:0,colOff:0,rowOff:0};
23928
24132
  var to = anchor && anchor.to;
23929
24133
  for(var C = 0; C < (from.col||0); ++C) out.left += html_col_width(cols[C]);
@@ -24036,11 +24240,11 @@ function render_html_drawings(ws, opts) {
24036
24240
  var out = [], drawings = ws["!drawings"] || {}, charts = ws["!charts"] || [];
24037
24241
  if(opts && opts.drawings && drawings.images) drawings.images.forEach(function(img) {
24038
24242
  if(!img || !img.dataURI) return;
24039
- var pos = html_anchor_pos(ws, img.anchor);
24243
+ var pos = html_anchor_pos(ws, img.anchor, opts);
24040
24244
  out.push('<img class="sjs-drawing-image" src="' + img.dataURI + '" style="' + html_abs_style(pos) + '"/>');
24041
24245
  });
24042
24246
  if(opts && opts.charts) charts.forEach(function(chart) {
24043
- var pos = html_anchor_pos(ws, chart.anchor);
24247
+ var pos = html_anchor_pos(ws, chart.anchor, opts);
24044
24248
  out.push('<div class="sjs-chart" style="' + html_abs_style(pos) + '">' + render_chart_svg(chart, pos.width, pos.height) + '</div>');
24045
24249
  });
24046
24250
  if(!out.length) return "";
@@ -24048,11 +24252,16 @@ function render_html_drawings(ws, opts) {
24048
24252
  }
24049
24253
  function make_html_preamble(ws/*:Worksheet*/, R/*:Range*/, o/*:Sheet2HTMLOpts*/)/*:string*/ {
24050
24254
  var out/*:Array<string>*/ = [];
24051
- var table = '<table' + (o && o.id ? ' id="' + o.id + '"' : "") + '>';
24052
- if(o && o.browserPixels && ws["!cols"]) {
24255
+ var tattr = ({}/*:any*/), tstyle = [];
24256
+ if(o && o.id) tattr.id = o.id;
24257
+ if(o && (o.browserPixels || o.autoFit)) tstyle.push("border-collapse:collapse;table-layout:fixed");
24258
+ if(tstyle.length) tattr.style = tstyle.join(";");
24259
+ var table = writextag("table", "", tattr).replace(/<\/table>$/, "");
24260
+ var cols = o && o._htmlCols || ws["!cols"];
24261
+ if(o && (o.browserPixels || o.autoFit) && cols) {
24053
24262
  out.push("<colgroup>");
24054
24263
  for(var C = R.s.c; C <= R.e.c; ++C) {
24055
- var col = ws["!cols"][C], style = [];
24264
+ var col = cols[C], style = [];
24056
24265
  style.push("width:" + html_col_width(col) + "px");
24057
24266
  if(col && col.hidden) style.push("display:none");
24058
24267
  out.push(writextag("col", null, {style:style.join(";")}));
@@ -24063,7 +24272,16 @@ function make_html_preamble(ws/*:Worksheet*/, R/*:Range*/, o/*:Sheet2HTMLOpts*/)
24063
24272
  }
24064
24273
 
24065
24274
  function sheet_to_html(ws/*:Worksheet*/, opts/*:?Sheet2HTMLOpts*//*, wb:?Workbook*/)/*:string*/ {
24066
- var o = opts || {};
24275
+ var o = {};
24276
+ if(opts) for(var k in opts) if(Object.prototype.hasOwnProperty.call(opts, k)) o[k] = opts[k];
24277
+ if(o.autoFit) {
24278
+ var af = {set:false};
24279
+ if(typeof o.autoFit == "object") for(k in o.autoFit) if(Object.prototype.hasOwnProperty.call(o.autoFit, k)) af[k] = o.autoFit[k];
24280
+ if(o.measureText && af.measureText == null) af.measureText = o.measureText;
24281
+ if(o.canvas && af.canvas == null) af.canvas = o.canvas;
24282
+ o._htmlCols = auto_fit_columns(ws, af);
24283
+ if(o.browserPixels == null) o.browserPixels = true;
24284
+ }
24067
24285
  var header = o.header != null ? o.header : HTML_BEGIN;
24068
24286
  var footer = o.footer != null ? o.footer : HTML_END;
24069
24287
  var out/*:Array<string>*/ = [header];
@@ -29302,6 +29520,9 @@ var utils/*:any*/ = {
29302
29520
  sheet_to_formulae: sheet_to_formulae,
29303
29521
  sheet_to_row_object_array: sheet_to_json,
29304
29522
  validate_merges: validate_merges,
29523
+ measure_text_width: measure_text_width,
29524
+ auto_fit_columns: auto_fit_columns,
29525
+ autofit_columns: auto_fit_columns,
29305
29526
  col_width_to_px: width2px,
29306
29527
  px_to_col_width: function(px) { return char2width(px2char(px)); },
29307
29528
  row_height_to_px: pt2px_browser,