@office-open/xlsx 0.10.0 → 0.10.2

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.
@@ -1,4 +1,4 @@
1
- import { a as SharedStrings } from "./comments-DxU57iWZ.mjs";
1
+ import { a as SharedStrings } from "./comments-CIu-wLTT.mjs";
2
2
  import { attr, attrNum, attrs, children, escapeXml, findChild, stringify, textOf } from "@office-open/xml";
3
3
  import { ChartCollection, Relationships, derivePasswordHash } from "@office-open/core";
4
4
  //#region src/parts/styles.ts
@@ -844,10 +844,24 @@ const chartsheetDesc = {
844
844
  };
845
845
  //#endregion
846
846
  //#region src/parts/drawing.ts
847
+ /** How a drawing is anchored to the worksheet (xdr:*Anchor element). */
848
+ const ANCHOR_TYPES = {
849
+ twoCell: "twoCell",
850
+ oneCell: "oneCell",
851
+ absolute: "absolute"
852
+ };
853
+ /** editAs behavior for twoCellAnchor (ST_EditAs). */
854
+ const EDIT_AS_TYPES = {
855
+ twoCell: "twoCell",
856
+ oneCell: "oneCell",
857
+ absolute: "absolute"
858
+ };
847
859
  const XDR_NS = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing";
848
860
  const A_NS = "http://schemas.openxmlformats.org/drawingml/2006/main";
849
861
  const R_NS$1 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
850
862
  const C_URI = "http://schemas.openxmlformats.org/drawingml/2006/chart";
863
+ const DEFAULT_EXTENT_CX = 4e5;
864
+ const DEFAULT_EXTENT_CY = 3e5;
851
865
  const drawingDesc = {
852
866
  kind: "custom",
853
867
  stringify(opts, _ctx) {
@@ -857,11 +871,11 @@ const drawingDesc = {
857
871
  const p = [`<wsDr xmlns="${XDR_NS}" xmlns:a="${A_NS}" xmlns:r="${R_NS$1}">`];
858
872
  let id = 1;
859
873
  for (const img of images) {
860
- p.push(`<twoCellAnchor editAs="oneCell"><from><col>${img.col - 1}</col><colOff>${img.colOffset ?? 0}</colOff><row>${img.row - 1}</row><rowOff>${img.rowOffset ?? 0}</rowOff></from>`, `<to><col>${img.col}</col><colOff>0</colOff><row>${img.row}</row><rowOff>0</rowOff></to>`, `<pic><nvPicPr><cNvPr id="${id}" name="Picture ${id}"/><cNvPicPr preferRelativeResize="1"/></nvPicPr>`, `<blipFill><a:blip r:embed="${img.rId}"/><a:stretch><a:fillRect/></a:stretch></blipFill>`, `<spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="400000" cy="300000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></spPr></pic>`, `<clientData fLocksWithSheet="${img.locksWithSheet !== false ? 1 : 0}" fPrintsWithSheet="${img.printsWithSheet !== false ? 1 : 0}"/></twoCellAnchor>`);
874
+ p.push(stringifyImage(img, id));
861
875
  id++;
862
876
  }
863
877
  for (const chart of charts) {
864
- p.push(`<twoCellAnchor editAs="oneCell"><from><col>${chart.col - 1}</col><colOff>${chart.colOffset ?? 0}</colOff><row>${chart.row - 1}</row><rowOff>${chart.rowOffset ?? 0}</rowOff></from>`, `<to><col>${chart.col + 8}</col><colOff>0</colOff><row>${chart.row + 15}</row><rowOff>0</rowOff></to>`, `<graphicFrame><nvGraphicFramePr><cNvPr id="${id}" name="Chart ${id}"/><cNvGraphicFramePr><a:graphicFrameLocks noGrp="1"/></cNvGraphicFramePr></nvGraphicFramePr>`, `<xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/></xfrm>`, `<a:graphic><a:graphicData uri="${C_URI}"><c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="${R_NS$1}" r:id="${chart.rId}"/></a:graphicData></a:graphic></graphicFrame>`, `<clientData fLocksWithSheet="${chart.locksWithSheet !== false ? 1 : 0}" fPrintsWithSheet="${chart.printsWithSheet !== false ? 1 : 0}"/></twoCellAnchor>`);
878
+ p.push(stringifyChart(chart, id));
865
879
  id++;
866
880
  }
867
881
  p.push("</wsDr>");
@@ -872,47 +886,17 @@ const drawingDesc = {
872
886
  const images = [];
873
887
  const charts = [];
874
888
  for (const anchor of el.elements ?? []) {
875
- if (anchor.name !== "twoCellAnchor") continue;
876
- const from = findChild(anchor, "from");
877
- findChild(anchor, "to");
878
- if (!from) continue;
879
- const col = readNumChild(from, "col") + 1;
880
- const colOffset = readNumChild(from, "colOff") || void 0;
881
- const row = readNumChild(from, "row") + 1;
882
- const rowOffset = readNumChild(from, "rowOff") || void 0;
889
+ const name = anchor.name;
890
+ if (name !== "twoCellAnchor" && name !== "oneCellAnchor" && name !== "absoluteAnchor") continue;
883
891
  const pic = findChild(anchor, "pic");
884
892
  if (pic) {
885
- const rId = findChild(findChild(pic, "blipFill") ?? pic, "a:blip")?.attributes?.["r:embed"];
886
- if (rId) {
887
- const clientData = findChild(anchor, "clientData");
888
- images.push({
889
- col,
890
- colOffset,
891
- row,
892
- rowOffset,
893
- rId,
894
- locksWithSheet: clientData?.attributes?.["fLocksWithSheet"] !== "0",
895
- printsWithSheet: clientData?.attributes?.["fPrintsWithSheet"] !== "0"
896
- });
897
- }
893
+ images.push(parseImageAnchor(anchor, pic, name));
898
894
  continue;
899
895
  }
900
896
  const graphicFrame = findChild(anchor, "graphicFrame");
901
897
  if (graphicFrame) {
902
- const graphicData = findChild(findChild(graphicFrame, "a:graphic") ?? graphicFrame, "a:graphicData");
903
- const rId = (graphicData ? findChild(graphicData, "c:chart") : void 0)?.attributes?.["r:id"];
904
- if (rId) {
905
- const clientData = findChild(anchor, "clientData");
906
- charts.push({
907
- col,
908
- colOffset,
909
- row,
910
- rowOffset,
911
- rId,
912
- locksWithSheet: clientData?.attributes?.["fLocksWithSheet"] !== "0",
913
- printsWithSheet: clientData?.attributes?.["fPrintsWithSheet"] !== "0"
914
- });
915
- }
898
+ const chart = parseChartAnchor(anchor, graphicFrame);
899
+ if (chart) charts.push(chart);
916
900
  }
917
901
  }
918
902
  if (images.length > 0) result.images = images;
@@ -920,12 +904,137 @@ const drawingDesc = {
920
904
  return result;
921
905
  }
922
906
  };
907
+ /** Marker cell (0-based col/row + EMU offsets). */
908
+ function markerXml(col, colOff, row, rowOff) {
909
+ return `<col>${col - 1}</col><colOff>${colOff}</colOff><row>${row - 1}</row><rowOff>${rowOff}</rowOff>`;
910
+ }
911
+ function clientDataXml(img) {
912
+ return `<clientData fLocksWithSheet="${img.locksWithSheet !== false ? 1 : 0}" fPrintsWithSheet="${img.printsWithSheet !== false ? 1 : 0}"/>`;
913
+ }
914
+ function picXml(rId, id, cx, cy) {
915
+ return `<pic><nvPicPr><cNvPr id="${id}" name="Picture ${id}"/><cNvPicPr preferRelativeResize="1"/></nvPicPr><blipFill><a:blip r:embed="${rId}"/><a:stretch><a:fillRect/></a:stretch></blipFill><spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="${cx}" cy="${cy}"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></spPr></pic>`;
916
+ }
917
+ function stringifyImage(img, id) {
918
+ const anchorType = img.anchorType ?? ANCHOR_TYPES.twoCell;
919
+ const cx = img.extentCx ?? DEFAULT_EXTENT_CX;
920
+ const cy = img.extentCy ?? DEFAULT_EXTENT_CY;
921
+ const pic = picXml(img.rId, id, cx, cy);
922
+ const clientData = clientDataXml(img);
923
+ if (anchorType === ANCHOR_TYPES.absolute) return `<absoluteAnchor><pos x="${img.absoluteX ?? 0}" y="${img.absoluteY ?? 0}"/><ext cx="${cx}" cy="${cy}"/>${pic}${clientData}</absoluteAnchor>`;
924
+ const from = markerXml(img.col, img.colOffset ?? 0, img.row, img.rowOffset ?? 0);
925
+ if (anchorType === ANCHOR_TYPES.oneCell) return `<oneCellAnchor><from>${from}</from><ext cx="${cx}" cy="${cy}"/>${pic}${clientData}</oneCellAnchor>`;
926
+ return `<twoCellAnchor editAs="${img.editAs ?? EDIT_AS_TYPES.oneCell}"><from>${from}</from><to>${markerXml(img.toCol ?? img.col + 1, img.toColOffset ?? 0, img.toRow ?? img.row + 1, img.toRowOffset ?? 0)}</to>${pic}${clientData}</twoCellAnchor>`;
927
+ }
928
+ function stringifyChart(chart, id) {
929
+ const from = markerXml(chart.col, chart.colOffset ?? 0, chart.row, chart.rowOffset ?? 0);
930
+ const to = markerXml(chart.col + 9, 0, chart.row + 16, 0);
931
+ const clientData = clientDataXml(chart);
932
+ return `<twoCellAnchor editAs="oneCell"><from>${from}</from><to>${to}</to><graphicFrame><nvGraphicFramePr><cNvPr id="${id}" name="Chart ${id}"/><cNvGraphicFramePr><a:graphicFrameLocks noGrp="1"/></cNvGraphicFramePr></nvGraphicFramePr><xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/></xfrm><a:graphic><a:graphicData uri="${C_URI}"><c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="${R_NS$1}" r:id="${chart.rId}"/></a:graphicData></a:graphic></graphicFrame>${clientData}</twoCellAnchor>`;
933
+ }
923
934
  function readNumChild(el, tag) {
924
935
  const child = findChild(el, tag);
925
936
  if (!child?.elements?.length) return 0;
926
937
  const n = Number(child.elements[0]?.text ?? "");
927
938
  return Number.isNaN(n) ? 0 : n;
928
939
  }
940
+ function readMarker(el) {
941
+ return {
942
+ col: readNumChild(el, "col") + 1,
943
+ colOffset: readNumChild(el, "colOff") || void 0,
944
+ row: readNumChild(el, "row") + 1,
945
+ rowOffset: readNumChild(el, "rowOff") || void 0
946
+ };
947
+ }
948
+ function readPicRId(pic) {
949
+ return findChild(findChild(pic, "blipFill") ?? pic, "a:blip")?.attributes?.["r:embed"];
950
+ }
951
+ /** Picture extent from pic/spPr/a:xfrm/a:ext (actual image size in EMU). */
952
+ function readPicExtent(pic) {
953
+ const spPr = findChild(pic, "spPr");
954
+ const xfrm = spPr ? findChild(spPr, "a:xfrm") : void 0;
955
+ const ext = xfrm ? findChild(xfrm, "a:ext") : void 0;
956
+ if (!ext?.attributes) return {};
957
+ const cx = Number(ext.attributes["cx"]);
958
+ const cy = Number(ext.attributes["cy"]);
959
+ return {
960
+ cx: Number.isNaN(cx) ? void 0 : cx,
961
+ cy: Number.isNaN(cy) ? void 0 : cy
962
+ };
963
+ }
964
+ function parseImageAnchor(anchor, pic, name) {
965
+ const result = {
966
+ col: 1,
967
+ row: 1,
968
+ rId: readPicRId(pic) ?? ""
969
+ };
970
+ const ext = readPicExtent(pic);
971
+ if (ext.cx !== void 0) result.extentCx = ext.cx;
972
+ if (ext.cy !== void 0) result.extentCy = ext.cy;
973
+ const clientData = findChild(anchor, "clientData");
974
+ if (clientData?.attributes) {
975
+ if (clientData.attributes["fLocksWithSheet"] !== void 0) result.locksWithSheet = clientData.attributes["fLocksWithSheet"] !== "0";
976
+ if (clientData.attributes["fPrintsWithSheet"] !== void 0) result.printsWithSheet = clientData.attributes["fPrintsWithSheet"] !== "0";
977
+ }
978
+ if (name === "absoluteAnchor") {
979
+ result.anchorType = ANCHOR_TYPES.absolute;
980
+ const pos = findChild(anchor, "pos");
981
+ if (pos?.attributes) {
982
+ const x = Number(pos.attributes["x"]);
983
+ const y = Number(pos.attributes["y"]);
984
+ if (!Number.isNaN(x)) result.absoluteX = x;
985
+ if (!Number.isNaN(y)) result.absoluteY = y;
986
+ }
987
+ return result;
988
+ }
989
+ const from = findChild(anchor, "from");
990
+ if (from) {
991
+ const m = readMarker(from);
992
+ result.col = m.col;
993
+ result.row = m.row;
994
+ if (m.colOffset !== void 0) result.colOffset = m.colOffset;
995
+ if (m.rowOffset !== void 0) result.rowOffset = m.rowOffset;
996
+ }
997
+ if (name === "oneCellAnchor") {
998
+ result.anchorType = ANCHOR_TYPES.oneCell;
999
+ return result;
1000
+ }
1001
+ result.anchorType = ANCHOR_TYPES.twoCell;
1002
+ const to = findChild(anchor, "to");
1003
+ if (to) {
1004
+ const m = readMarker(to);
1005
+ result.toCol = m.col;
1006
+ result.toRow = m.row;
1007
+ if (m.colOffset !== void 0) result.toColOffset = m.colOffset;
1008
+ if (m.rowOffset !== void 0) result.toRowOffset = m.rowOffset;
1009
+ }
1010
+ const editAs = anchor.attributes?.["editAs"];
1011
+ if (editAs) result.editAs = editAs;
1012
+ return result;
1013
+ }
1014
+ function parseChartAnchor(anchor, graphicFrame) {
1015
+ const graphicData = findChild(findChild(graphicFrame, "a:graphic") ?? graphicFrame, "a:graphicData");
1016
+ const rId = (graphicData ? findChild(graphicData, "c:chart") : void 0)?.attributes?.["r:id"];
1017
+ if (!rId) return void 0;
1018
+ const result = {
1019
+ col: 1,
1020
+ row: 1,
1021
+ rId
1022
+ };
1023
+ const from = findChild(anchor, "from");
1024
+ if (from) {
1025
+ const m = readMarker(from);
1026
+ result.col = m.col;
1027
+ result.row = m.row;
1028
+ if (m.colOffset !== void 0) result.colOffset = m.colOffset;
1029
+ if (m.rowOffset !== void 0) result.rowOffset = m.rowOffset;
1030
+ }
1031
+ const clientData = findChild(anchor, "clientData");
1032
+ if (clientData?.attributes) {
1033
+ if (clientData.attributes["fLocksWithSheet"] !== void 0) result.locksWithSheet = clientData.attributes["fLocksWithSheet"] !== "0";
1034
+ if (clientData.attributes["fPrintsWithSheet"] !== void 0) result.printsWithSheet = clientData.attributes["fPrintsWithSheet"] !== "0";
1035
+ }
1036
+ return result;
1037
+ }
929
1038
  //#endregion
930
1039
  //#region src/parts/external-link.ts
931
1040
  const externalLinkDesc = {
@@ -3998,4 +4107,4 @@ function resolveWsTarget(wsPath, target) {
3998
4107
  //#endregion
3999
4108
  export { stylesDesc as C, Styles as S, collectUniqueValues as _, usersDesc as a, chartsheetDesc as b, workbookDesc as c, tableDesc as d, pivotCacheDefDesc as f, aggregate as g, PivotFilterType as h, revisionLogDesc as i, TableType as l, pivotTableDesc as m, XlsxWriteContext as n, buildExternalReferencesXml as o, pivotCacheRecordsDesc as p, revisionHeadersDesc as r, buildTablePartsXml as s, XlsxReadContext as t, TotalsRowFunction as u, externalLinkDesc as v, calcChainDesc as x, drawingDesc as y };
4000
4109
 
4001
- //# sourceMappingURL=context-e2jg7QPd.mjs.map
4110
+ //# sourceMappingURL=context-Btk-2AKp.mjs.map