@alosha/xlsx 0.3.1 → 0.4.0

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/dist/compat.js CHANGED
@@ -1,5 +1,5 @@
1
- import { UnsupportedFeatureError, FormulaType, Workbook, Cell, Row, Column, Worksheet } from './chunk-6XXKAKLS.js';
2
- export { AloshaXlsxError, Cell, CellType, Column, DEFAULT_COLUMN_WIDTH, DuplicateWorksheetNameError, FormulaType, InvalidAddressError, InvalidCellValueError, InvalidPackageError, InvalidRangeError, InvalidRowNumberError, InvalidWorksheetNameError, MergeConflictError, Range, ReservedWorksheetNameError, Row, UnsupportedFeatureError, Worksheet, WorksheetNameTooLongError, colLetterToNumber, colNumberToLetter, createSharedStrings, createStyles, dateToSerial, decodeAddress, decodeEx, detectType, effectiveType, encodeAddress, fromStored, isAloshaXlsxError, readWorkbookBuffer, readWorkbookFile, resolveCellStyle, serialToDate, toCsv, toStored, toText, validateAddress, version, writeWorkbookBuffer, writeWorkbookFile } from './chunk-6XXKAKLS.js';
1
+ import { UnsupportedFeatureError, FormulaType, Workbook, Cell, Row, Column, Worksheet } from './chunk-C67HH2MR.js';
2
+ export { AloshaXlsxError, Cell, CellType, Column, DEFAULT_COLUMN_WIDTH, DuplicateWorksheetNameError, FormulaType, InvalidAddressError, InvalidCellValueError, InvalidPackageError, InvalidRangeError, InvalidRowNumberError, InvalidWorksheetNameError, MergeConflictError, Range, ReservedWorksheetNameError, Row, UnsupportedFeatureError, Worksheet, WorksheetNameTooLongError, colLetterToNumber, colNumberToLetter, createSharedStrings, createStyles, dateToSerial, decodeAddress, decodeEx, detectType, effectiveType, encodeAddress, fromStored, isAloshaXlsxError, readWorkbookBuffer, readWorkbookFile, resolveCellStyle, serialToDate, toCsv, toStored, toText, validateAddress, version, writeWorkbookBuffer, writeWorkbookFile } from './chunk-C67HH2MR.js';
3
3
 
4
4
  // src/compat/value-type.ts
5
5
  var ValueType = {
package/dist/index.cjs CHANGED
@@ -1694,7 +1694,8 @@ var REL_TYPE = {
1694
1694
  worksheet: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
1695
1695
  styles: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
1696
1696
  sharedStrings: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
1697
- theme: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"
1697
+ theme: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
1698
+ hyperlink: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
1698
1699
  };
1699
1700
  var PART_PATH = {
1700
1701
  contentTypes: "[Content_Types].xml",
@@ -1995,11 +1996,23 @@ function openPackage(bytes) {
1995
1996
  relsMap.set(rel.id, { type: rel.type, target: resolveRelTarget(workbookDir, rel.target) });
1996
1997
  }
1997
1998
  }
1999
+ const partRels = (partPath) => {
2000
+ const dir = dirname(partPath);
2001
+ const relsXml = text(relsPathFor(partPath));
2002
+ const map = /* @__PURE__ */ new Map();
2003
+ if (relsXml === void 0) return map;
2004
+ for (const rel of parseRels(relsXml)) {
2005
+ const target = rel.mode === "External" ? rel.target : resolveRelTarget(dir, rel.target);
2006
+ map.set(rel.id, { type: rel.type, target, mode: rel.mode });
2007
+ }
2008
+ return map;
2009
+ };
1998
2010
  return {
1999
2011
  entry,
2000
2012
  text,
2001
2013
  workbookRels: () => relsMap,
2002
- workbookPath: () => workbookPath
2014
+ workbookPath: () => workbookPath,
2015
+ partRels
2003
2016
  };
2004
2017
  }
2005
2018
  function readZip(bytes) {
@@ -2021,7 +2034,8 @@ function parseRels(xml) {
2021
2034
  rels.push({
2022
2035
  id: child.attrs.Id ?? "",
2023
2036
  type: child.attrs.Type ?? "",
2024
- target: child.attrs.Target ?? ""
2037
+ target: child.attrs.Target ?? "",
2038
+ mode: child.attrs.TargetMode === "External" ? "External" : "Internal"
2025
2039
  });
2026
2040
  });
2027
2041
  }
@@ -2925,6 +2939,7 @@ function parseWorksheet(ws, xml, ctx) {
2925
2939
  while (ev && !(ev.kind === "open" && localName(ev.name) === "worksheet")) ev = c.next();
2926
2940
  if (ev?.kind !== "open" || ev.selfClosing) return;
2927
2941
  const merges = [];
2942
+ const hyperlinks = [];
2928
2943
  c.eachChild(ev.name, (child) => {
2929
2944
  switch (localName(child.name)) {
2930
2945
  case "cols":
@@ -2936,11 +2951,17 @@ function parseWorksheet(ws, xml, ctx) {
2936
2951
  case "mergeCells":
2937
2952
  if (!child.selfClosing) collectMerges(c, child.name, merges);
2938
2953
  break;
2954
+ case "hyperlinks":
2955
+ if (!child.selfClosing) collectHyperlinks(c, child.name, hyperlinks);
2956
+ break;
2939
2957
  default:
2940
2958
  if (!child.selfClosing) c.textUntilClose(child.name);
2941
2959
  }
2942
2960
  });
2943
2961
  for (const ref of merges) ws.mergeCells(ref);
2962
+ if (ctx.rels) {
2963
+ for (const link of hyperlinks) applyHyperlink(ws, link, ctx.rels);
2964
+ }
2944
2965
  }
2945
2966
  function parseCols(c, container, ws, ctx) {
2946
2967
  c.eachChild(container, (col) => {
@@ -3081,6 +3102,35 @@ function collectMerges(c, container, merges) {
3081
3102
  }
3082
3103
  });
3083
3104
  }
3105
+ function collectHyperlinks(c, container, hyperlinks) {
3106
+ c.eachChild(container, (child) => {
3107
+ if (localName(child.name) !== "hyperlink") {
3108
+ if (!child.selfClosing) c.textUntilClose(child.name);
3109
+ return;
3110
+ }
3111
+ const ref = child.attrs.ref;
3112
+ const rId = relId2(child);
3113
+ if (ref === void 0 || rId === "") return;
3114
+ const tooltip = child.attrs.tooltip;
3115
+ hyperlinks.push(tooltip !== void 0 ? { ref, rId, tooltip } : { ref, rId });
3116
+ });
3117
+ }
3118
+ function applyHyperlink(ws, link, rels) {
3119
+ const rel = rels.get(link.rId);
3120
+ if (!rel) return;
3121
+ const topLeft = link.ref.split(":")[0] ?? link.ref;
3122
+ const cell = ws.getCell(topLeft);
3123
+ const text = typeof cell.value === "string" ? cell.value : "";
3124
+ cell.value = link.tooltip !== void 0 ? { text, hyperlink: rel.target, tooltip: link.tooltip } : { text, hyperlink: rel.target };
3125
+ }
3126
+ function relId2(el) {
3127
+ const direct = el.attrs["r:id"];
3128
+ if (direct !== void 0) return direct;
3129
+ for (const key of Object.keys(el.attrs)) {
3130
+ if (localName(key) === "id") return el.attrs[key] ?? "";
3131
+ }
3132
+ return "";
3133
+ }
3084
3134
  function decodeBool(raw) {
3085
3135
  if (raw === "1" || raw === "true" || raw === "TRUE") return true;
3086
3136
  if (raw === "0" || raw === "false" || raw === "FALSE") return false;
@@ -3118,7 +3168,9 @@ function readWorkbookBuffer(bytes, _options) {
3118
3168
  const target = rels.get(sheet.rId)?.target;
3119
3169
  if (target === void 0) continue;
3120
3170
  const sheetXml = pkg.text(target);
3121
- if (sheetXml !== void 0) parseWorksheet(ws, sheetXml, ctx);
3171
+ if (sheetXml !== void 0) {
3172
+ parseWorksheet(ws, sheetXml, { ...ctx, rels: pkg.partRels(target) });
3173
+ }
3122
3174
  }
3123
3175
  const coreXml = pkg.text(PART_PATH.coreProps);
3124
3176
  if (coreXml !== void 0) applyCoreProps(wb, coreXml);
@@ -3384,6 +3436,22 @@ function renderWorkbookRels(rels) {
3384
3436
  return w.toString();
3385
3437
  }
3386
3438
 
3439
+ // src/xlsx/parts/worksheet-rels.ts
3440
+ function renderWorksheetRels(rels) {
3441
+ const w = new XmlWriter();
3442
+ w.open("Relationships", { xmlns: NS.packageRels });
3443
+ for (const rel of rels) {
3444
+ w.leaf("Relationship", {
3445
+ Id: rel.rId,
3446
+ Type: REL_TYPE.hyperlink,
3447
+ Target: rel.target,
3448
+ TargetMode: rel.mode
3449
+ });
3450
+ }
3451
+ w.close("Relationships");
3452
+ return w.toString();
3453
+ }
3454
+
3387
3455
  // src/xlsx/parts/worksheet.ts
3388
3456
  var DEFAULT_DATE_NUMFMT = "mm-dd-yy";
3389
3457
  function renderWorksheet(ws, ctx) {
@@ -3401,11 +3469,13 @@ function renderWorksheet(ws, ctx) {
3401
3469
  defaultRowHeight: model.properties.defaultRowHeight ?? 15,
3402
3470
  defaultColWidth: model.properties.defaultColWidth
3403
3471
  });
3472
+ const hyperlinks = [];
3404
3473
  renderCols(w, model.columns, ctx);
3405
- renderSheetData(w, model.rows, ctx);
3474
+ renderSheetData(w, model.rows, ctx, hyperlinks);
3406
3475
  renderMergeCells(w, model.merges);
3476
+ const rels = renderHyperlinks(w, hyperlinks);
3407
3477
  w.close("worksheet");
3408
- return w.toString();
3478
+ return { xml: w.toString(), rels };
3409
3479
  }
3410
3480
  function renderCols(w, columns, ctx) {
3411
3481
  if (!columns || columns.length === 0) return;
@@ -3431,13 +3501,13 @@ function renderCols(w, columns, ctx) {
3431
3501
  for (const f of fragments) w.raw(f);
3432
3502
  w.close("cols");
3433
3503
  }
3434
- function renderSheetData(w, rows, ctx) {
3504
+ function renderSheetData(w, rows, ctx, hyperlinks) {
3435
3505
  w.open("sheetData");
3436
3506
  for (const row of rows) {
3437
3507
  const styleId = row.style ? ctx.styles.addStyle(row.style) : 0;
3438
3508
  let cellsXml = "";
3439
3509
  for (const cell of row.cells) {
3440
- cellsXml += renderCell(cell, ctx);
3510
+ cellsXml += renderCell(cell, ctx, hyperlinks);
3441
3511
  }
3442
3512
  const hasAttrs = row.height !== void 0 || row.hidden || (row.outlineLevel ?? 0) !== 0 || styleId !== 0;
3443
3513
  if (cellsXml === "" && !hasAttrs) continue;
@@ -3455,7 +3525,7 @@ function renderSheetData(w, rows, ctx) {
3455
3525
  }
3456
3526
  w.close("sheetData");
3457
3527
  }
3458
- function renderCell(cell, ctx) {
3528
+ function renderCell(cell, ctx, hyperlinks) {
3459
3529
  const styleId = cell.style ? ctx.styles.addStyle(cell.style) : 0;
3460
3530
  const r = cell.address;
3461
3531
  switch (cell.type) {
@@ -3474,8 +3544,11 @@ function renderCell(cell, ctx) {
3474
3544
  return valueCell(r, styleId, "e", cell.value.error);
3475
3545
  case 2 /* String */:
3476
3546
  return stringCell(r, styleId, cell.value, ctx);
3477
- case 7 /* Hyperlink */:
3478
- return stringCell(r, styleId, cell.value.text, ctx);
3547
+ case 7 /* Hyperlink */: {
3548
+ const link = cell.value;
3549
+ hyperlinks.push({ ref: r, target: link.hyperlink, tooltip: link.tooltip });
3550
+ return stringCell(r, styleId, link.text, ctx);
3551
+ }
3479
3552
  case 6 /* RichText */:
3480
3553
  return richTextCell(r, styleId, cell.value, ctx);
3481
3554
  case 8 /* Formula */:
@@ -3549,6 +3622,18 @@ function renderMergeCells(w, merges) {
3549
3622
  for (const ref of merges) w.leaf("mergeCell", { ref });
3550
3623
  w.close("mergeCells");
3551
3624
  }
3625
+ function renderHyperlinks(w, hyperlinks) {
3626
+ if (hyperlinks.length === 0) return void 0;
3627
+ const rels = [];
3628
+ w.open("hyperlinks");
3629
+ hyperlinks.forEach((h, i) => {
3630
+ const rId = `rId${i + 1}`;
3631
+ w.leaf("hyperlink", { ref: h.ref, "r:id": rId, tooltip: h.tooltip });
3632
+ rels.push({ rId, target: h.target, mode: "External" });
3633
+ });
3634
+ w.close("hyperlinks");
3635
+ return renderWorksheetRels(rels);
3636
+ }
3552
3637
 
3553
3638
  // src/xlsx/write-workbook.ts
3554
3639
  function writeWorkbookBuffer(wb, options) {
@@ -3612,7 +3697,10 @@ function writeWorkbookBuffer(wb, options) {
3612
3697
  put(PART_PATH.styles, styles.toXml());
3613
3698
  if (sharedStringsXml !== void 0) put(PART_PATH.sharedStrings, sharedStringsXml);
3614
3699
  put(PART_PATH.theme, renderTheme());
3615
- for (const [file, xml] of worksheetXml) put(`xl/worksheets/${file}`, xml);
3700
+ for (const [file, { xml, rels }] of worksheetXml) {
3701
+ put(`xl/worksheets/${file}`, xml);
3702
+ if (rels !== void 0) put(`xl/worksheets/_rels/${file}.rels`, rels);
3703
+ }
3616
3704
  return writeZipArchive(entries);
3617
3705
  }
3618
3706
  async function writeWorkbookFile(wb, path, options) {