@8sheets/data-grid 1.0.2 → 1.1.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.
Files changed (99) hide show
  1. package/README.md +1 -1
  2. package/api/theme-core.js +2 -0
  3. package/calc/compile.js +1 -1
  4. package/calc/engine.js +1 -1
  5. package/calc/functions/info.js +1 -1
  6. package/calc/functions/math.js +1 -1
  7. package/calc/parser.js +3 -3
  8. package/calc/values.js +1 -1
  9. package/calcx/culture/custom.js +1 -0
  10. package/calcx/culture/format-locale.js +1 -1
  11. package/calcx/culture/separators.js +1 -0
  12. package/calcx/lang/convert.js +1 -0
  13. package/cells/axis-ops.js +1 -1
  14. package/cells/block.js +1 -0
  15. package/cells/book-css.js +2 -2
  16. package/cells/cell-defaults.js +1 -1
  17. package/cells/consolidate-links.js +1 -1
  18. package/cells/find-engine.js +2 -2
  19. package/cells/format-panes.js +3 -3
  20. package/cells/format-tabs.js +2 -2
  21. package/cells/outline-column.js +1 -1
  22. package/cells/panes.js +1 -1
  23. package/cells/selection-modes.js +1 -1
  24. package/cells/shift-check.js +1 -0
  25. package/cells/table-rows.js +1 -0
  26. package/cells/text-layout.js +9 -8
  27. package/cells/ui-kit.js +15 -0
  28. package/cells/view-options.js +1 -1
  29. package/celltypes/diagonal.js +1 -1
  30. package/celltypes/dropdowns.js +2 -2
  31. package/core/autofill.js +1 -1
  32. package/core/axis-styles.js +1 -0
  33. package/core/axis.js +1 -1
  34. package/core/culture.js +1 -0
  35. package/core/formula-refs.js +1 -1
  36. package/core/input.js +1 -1
  37. package/core/merge.js +1 -0
  38. package/core/numfmt.js +1 -1
  39. package/core/region-input.js +1 -0
  40. package/core/row-fit.js +1 -0
  41. package/core/sheet.js +1 -1
  42. package/core/structure.js +1 -0
  43. package/core/styles.js +1 -1
  44. package/core/tables.js +2 -2
  45. package/core/whatif.js +1 -1
  46. package/core/workbook.js +1 -1
  47. package/datasheets/data-core.js +1 -1
  48. package/datasheets/gantt-paint.js +1 -1
  49. package/datasheets/path-binding.js +2 -2
  50. package/datasheets/rules.js +1 -1
  51. package/datasheets/view.js +2 -2
  52. package/i18n/dom.js +1 -1
  53. package/i18n/en/cells.js +1 -1
  54. package/i18n/en/format.js +1 -1
  55. package/i18n/en/shell.js +2 -2
  56. package/i18n/pl/cells.js +1 -1
  57. package/i18n/pl/format.js +1 -1
  58. package/i18n/pl/shell.js +2 -2
  59. package/io/csv.js +8 -8
  60. package/io/plan.js +1 -1
  61. package/io/text-split.js +3 -3
  62. package/io/xlsx-write.js +1 -1
  63. package/objects/chart-model.js +1 -1
  64. package/package.json +1 -1
  65. package/pivot/engine.js +1 -1
  66. package/print/paginate.js +1 -1
  67. package/print/render-page.js +3 -3
  68. package/table-view/api.js +1 -1
  69. package/table-view/cell-editor.js +2 -1
  70. package/table-view/chart.js +1 -0
  71. package/table-view/column-drag.js +1 -1
  72. package/table-view/context-menu.js +1 -0
  73. package/table-view/editors.js +1 -0
  74. package/table-view/events.js +1 -1
  75. package/table-view/files.js +1 -1
  76. package/table-view/fill-handle.js +1 -0
  77. package/table-view/filter-builder.js +1 -0
  78. package/table-view/find.js +1 -0
  79. package/table-view/grid-keys.js +2 -2
  80. package/table-view/labels-de.js +1 -0
  81. package/table-view/labels-es.js +1 -0
  82. package/table-view/labels-fr.js +1 -0
  83. package/table-view/labels-ja.js +1 -0
  84. package/table-view/labels.js +1 -1
  85. package/table-view/model.js +1 -1
  86. package/table-view/mount.js +1 -1
  87. package/table-view/panel.js +1 -1
  88. package/table-view/pivot-spec.js +1 -0
  89. package/table-view/pivot.js +1 -1
  90. package/table-view/public.d.ts +277 -28
  91. package/table-view/reader.worker.js +1 -1
  92. package/table-view/remote.js +1 -1
  93. package/table-view/row-drag.js +1 -0
  94. package/table-view/sparkline.js +1 -0
  95. package/table-view/state-check.js +1 -0
  96. package/table-view/table-css.js +380 -0
  97. package/table-view/version.js +1 -1
  98. package/ui/dialogs.js +3 -3
  99. package/ui/menu-base.js +1 -1
package/core/input.js CHANGED
@@ -1 +1 @@
1
- import{BOOL as g,EMPTY as P,ERR as C,ERRORS as D,FORMULA as I,NUM as o,STR as v}from"./sheet.js";import{partsToSerial as l}from"./numfmt.js";const M=["#NULL!","#DIV/0!","#VALUE!","#REF!","#NAME?","#NUM!","#N/A"],L=["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"],F=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/,S=/^[+-]?\d{1,3}(?:,\d{3})+(?:\.\d*)?$/,A=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)[eE][+-]?\d+$/,Y=/^[+-]?(?:\d{1,3}(?:,\d{3})+|\d+)?(?:\.\d*)?%$/,y="(?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d*)?|\\.\\d+",j=/^([+-]?)[€£¥]\s?([+-]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d*)?)$/;let $={symbol:"$",symbolFirst:!0};function H(r){$={symbol:r.symbol,symbolFirst:r.symbolFirst}}const Z=r=>`"${r.replace(/"/g,"")}"`;function _(r){const e=r?"#,##0.00":"#,##0",n=Z($.symbol);return $.symbolFirst?`${n}${e}_);[Red]\\(${n}${e}\\)`:`${e} ${n}`}function q(r,e){return e===2?r%4===0&&(r%100!==0||r%400===0||r===1900)?29:28:[4,6,9,11].includes(e)?30:31}const d=r=>r.length<=2?(Number(r)<30?2e3:1900)+Number(r):Number(r),m=(r,e,n)=>r>=1900&&r<=9999&&e>=1&&e<=12&&n>=1&&n<=q(r,e),O=r=>{const e=r.toLowerCase().replace(/\.$/,"");if(e.length<3)return 0;const n=L.indexOf(e.slice(0,3));return n<0?0:new Intl.DateTimeFormat("en-US",{month:"long",timeZone:"UTC"}).format(new Date(Date.UTC(2024,n,15))).toLowerCase().startsWith(e)?n+1:0},b=()=>new Date().getFullYear(),p=(r,e,n)=>r===1900&&e===2&&n===29?60:l(r,e,n);function T(r){let e;if(e=/^(\d{1,2})([/-])(\d{1,2})\2(\d{1,4})$/.exec(r)){const[n,u,t]=[Number(e[1]),Number(e[3]),d(e[4])];return m(t,n,u)?{value:p(t,n,u),format:"m/d/yyyy"}:null}if(e=/^(\d{4})([/-])(\d{1,2})\2(\d{1,2})$/.exec(r)){const[n,u,t]=[Number(e[1]),Number(e[3]),Number(e[4])];return m(n,u,t)?{value:p(n,u,t),format:"m/d/yyyy"}:null}if(e=/^(\d{1,2})[/-](\d{1,2})$/.exec(r)){const[n,u,t]=[Number(e[1]),Number(e[2]),b()];if(m(t,n,u))return{value:p(t,n,u),format:"d-mmm"};const s=d(e[2]);return e[2].length!==1&&m(s,n,1)?{value:l(s,n,1),format:"mmm-yy"}:null}if(e=/^(\d{1,2})[-\s]+([A-Za-z]+\.?)(?:[-\s,]+(\d{2}|\d{4}))?$/.exec(r)){const n=O(e[2]),u=Number(e[1]),t=e[3]?d(e[3]):b();return n&&m(t,n,u)?{value:p(t,n,u),format:e[3]?"d-mmm-yy":"d-mmm"}:null}if(e=/^([A-Za-z]+\.?)[-\s]+(\d{1,4})(?:,?\s*(\d{2}|\d{4}))?$/.exec(r)){const n=O(e[1]);if(!n)return null;const u=Number(e[2]);if(e[3]){const s=d(e[3]);return m(s,n,u)&&e[2].length<=2?{value:l(s,n,u),format:"d-mmm-yy"}:null}if(e[2].length<=2&&m(b(),n,u))return{value:l(b(),n,u),format:"d-mmm"};const t=d(e[2]);return m(t,n,1)&&e[2].length!==3?{value:l(t,n,1),format:"mmm-yy"}:null}return null}function U(r){let e=/^(\d{1,4}):(\d{1,2})(?::(\d{1,2}))?(?:\s+([AaPp][Mm]?))?$/.exec(r),n,u=0,t=0,s;if(e)[n,u,t]=[Number(e[1]),Number(e[2]),Number(e[3]??0)],s=e[4];else if(e=/^(\d{1,2})\s+([AaPp][Mm]?)$/.exec(r))n=Number(e[1]),s=e[2];else return null;if(u>59||t>59)return null;if(s){if(n<1||n>12)return null;const f=s[0].toLowerCase()==="p";n=n%12+(f?12:0)}const i=(n*3600+u*60+t)/86400;return s?{value:i,format:e[3]!==void 0?"h:mm:ss AM/PM":"h:mm AM/PM"}:n>=24?{value:i,format:"[h]:mm:ss"}:{value:i,format:e[3]!==void 0?"h:mm:ss":"h:mm"}}function z(r){const e=/^(.+?)\s+(\d{1,2}(?::\d{1,2}(?::\d{1,2})?)?\s*(?:[AaPp][Mm]?)?)$/.exec(r);if(!e||!/:|[AaPp]/.test(e[2]))return null;const n=T(e[1]),u=U(e[2]);return!n||!u||u.value>=1?null:{value:n.value+u.value,format:"m/d/yyyy h:mm"}}function B(r){const e=/^([+-]?)(\d+) (\d+)\/(\d+)$/.exec(r);if(!e)return null;const n=Number(e[4]);if(n===0)return null;const u=(Number(e[2])+Number(e[3])/n)*(e[1]==="-"?-1:1),t="?".repeat(Math.min(3,e[4].length));return{value:u,format:`# ${t}/${t}`}}function N(r){let e=0,n="";for(const u of r.replace(/,/g,""))u>="0"&&u<="9"?((e>0||u!=="0")&&e++,n+=e>15?"0":u):((u==="e"||u==="E")&&(e=-1/0),n+=u);return Number(n)}const c=N;function W(r){if(r==="")return{kind:P};if(r[0]==="="&&r.length>1)return{kind:I,formula:r.slice(1)};if(r[0]==="'")return{kind:v,text:r.slice(1)};const e=r.trim(),n=r.toUpperCase();if(n==="TRUE")return{kind:g,value:1};if(n==="FALSE")return{kind:g,value:0};const u=M.indexOf(n);if(u>=0)return{kind:C,value:D.indexOf(M[u]),text:r};let t;if((F.test(e)||A.test(e))&&!Number.isFinite(N(e)))return{kind:v,text:r};if(F.test(e))return{kind:o,value:N(e)};if(S.test(e))return{kind:o,value:c(e),format:e.includes(".")?"#,##0.00":"#,##0"};if(A.test(e))return{kind:o,value:N(e),format:"0.00E+00"};if(Y.test(e)&&/\d/.test(e)){const a=e.slice(0,-1);return{kind:o,value:c(a)/100,format:/\.\d/.test(a)?"0.00%":"0%"}}if(t=/^\((\d{1,3}(?:,\d{3})+(?:\.\d*)?|\d+(?:\.\d*)?|\.\d+)\)$/.exec(e))return{kind:o,value:-c(t[1]),...t[1].includes(",")?{format:t[1].includes(".")?"#,##0.00":"#,##0"}:{}};const s=$.symbol.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),i=(a,k,w)=>{const x=c(k)*(a==="-"||w?-1:1);return{kind:o,value:x,format:_(/\.\d/.test(k))}};if(t=new RegExp(`^([+-]?)${s}\\s?([+-]?)(${y})$`).exec(e))return i(t[1]||t[2],t[3]);if(t=new RegExp(`^([+-]?)(${y})\\s?${s}$`).exec(e))return i(t[1],t[2]);if((t=new RegExp(`^\\(${s}\\s?(${y})\\)$`).exec(e))||(t=new RegExp(`^\\((${y})\\s?${s}\\)$`).exec(e)))return i("",t[1],!0);if(t=j.exec(e))return{kind:o,value:c(t[2])*(t[1]==="-"?-1:1)};const f=B(e);if(f)return{kind:o,...f};const E=T(e);if(E)return{kind:o,...E};const R=U(e);if(R)return{kind:o,...R};const h=z(e);return h?{kind:o,...h}:{kind:v,text:r}}export{W as parseInput,H as setEntryCurrency};
1
+ import{BOOL as g,EMPTY as C,ERR as D,ERRORS as I,FORMULA as L,NUM as s,STR as N}from"./sheet.js";import{partsToSerial as d}from"./numfmt.js";const M=["#NULL!","#DIV/0!","#VALUE!","#REF!","#NAME?","#NUM!","#N/A"],S=["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"],A=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/,Y=/^[+-]?\d{1,3}(?:,\d{3})+(?:\.\d*)?$/,F=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)[eE][+-]?\d+$/,j=/^[+-]?(?:\d{1,3}(?:,\d{3})+|\d+)?(?:\.\d*)?%$/,y="(?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d*)?|\\.\\d+",Z=/^([+-]?)[€£¥]\s?([+-]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d*)?)$/;let T={symbol:"$",symbolFirst:!0};function W(r){T={symbol:r.symbol,symbolFirst:r.symbolFirst}}const _=r=>`"${r.replace(/"/g,"")}"`;function q(r,e){const n=r?"#,##0.00":"#,##0",t=_(e.symbol);return e.symbolFirst?`${t}${n}_);[Red]\\(${t}${n}\\)`:`${n} ${t}`}function z(r,e){return e===2?r%4===0&&(r%100!==0||r%400===0||r===1900)?29:28:[4,6,9,11].includes(e)?30:31}const c=r=>r.length<=2?(Number(r)<30?2e3:1900)+Number(r):Number(r),m=(r,e,n)=>r>=1900&&r<=9999&&e>=1&&e<=12&&n>=1&&n<=z(r,e),O=r=>{const e=r.toLowerCase().replace(/\.$/,"");if(e.length<3)return 0;const n=S.indexOf(e.slice(0,3));return n<0?0:new Intl.DateTimeFormat("en-US",{month:"long",timeZone:"UTC"}).format(new Date(Date.UTC(2024,n,15))).toLowerCase().startsWith(e)?n+1:0},$=()=>new Date().getFullYear(),b=(r,e,n)=>r===1900&&e===2&&n===29?60:d(r,e,n);function U(r){let e;if(e=/^(\d{1,2})([/-])(\d{1,2})\2(\d{1,4})$/.exec(r)){const[n,t,o]=[Number(e[1]),Number(e[3]),c(e[4])];return m(o,n,t)?{value:b(o,n,t),format:"m/d/yyyy"}:null}if(e=/^(\d{4})([/-])(\d{1,2})\2(\d{1,2})$/.exec(r)){const[n,t,o]=[Number(e[1]),Number(e[3]),Number(e[4])];return m(n,t,o)?{value:b(n,t,o),format:"m/d/yyyy"}:null}if(e=/^(\d{1,2})[/-](\d{1,2})$/.exec(r)){const[n,t,o]=[Number(e[1]),Number(e[2]),$()];if(m(o,n,t))return{value:b(o,n,t),format:"d-mmm"};const u=c(e[2]);return e[2].length!==1&&m(u,n,1)?{value:d(u,n,1),format:"mmm-yy"}:null}if(e=/^(\d{1,2})[-\s]+([A-Za-z]+\.?)(?:[-\s,]+(\d{2}|\d{4}))?$/.exec(r)){const n=O(e[2]),t=Number(e[1]),o=e[3]?c(e[3]):$();return n&&m(o,n,t)?{value:b(o,n,t),format:e[3]?"d-mmm-yy":"d-mmm"}:null}if(e=/^([A-Za-z]+\.?)[-\s]+(\d{1,4})(?:,?\s*(\d{2}|\d{4}))?$/.exec(r)){const n=O(e[1]);if(!n)return null;const t=Number(e[2]);if(e[3]){const u=c(e[3]);return m(u,n,t)&&e[2].length<=2?{value:d(u,n,t),format:"d-mmm-yy"}:null}if(e[2].length<=2&&m($(),n,t))return{value:d($(),n,t),format:"d-mmm"};const o=c(e[2]);return m(o,n,1)&&e[2].length!==3?{value:d(o,n,1),format:"mmm-yy"}:null}return null}function x(r){let e=/^(\d{1,4}):(\d{1,2})(?::(\d{1,2}))?(?:\s+([AaPp][Mm]?))?$/.exec(r),n,t=0,o=0,u;if(e)[n,t,o]=[Number(e[1]),Number(e[2]),Number(e[3]??0)],u=e[4];else if(e=/^(\d{1,2})\s+([AaPp][Mm]?)$/.exec(r))n=Number(e[1]),u=e[2];else return null;if(t>59||o>59)return null;if(u){if(n<1||n>12)return null;const l=u[0].toLowerCase()==="p";n=n%12+(l?12:0)}const i=(n*3600+t*60+o)/86400;return u?{value:i,format:e[3]!==void 0?"h:mm:ss AM/PM":"h:mm AM/PM"}:n>=24?{value:i,format:"[h]:mm:ss"}:{value:i,format:e[3]!==void 0?"h:mm:ss":"h:mm"}}function B(r){const e=/^(.+?)\s+(\d{1,2}(?::\d{1,2}(?::\d{1,2})?)?\s*(?:[AaPp][Mm]?)?)$/.exec(r);if(!e||!/:|[AaPp]/.test(e[2]))return null;const n=U(e[1]),t=x(e[2]);return!n||!t||t.value>=1?null:{value:n.value+t.value,format:"m/d/yyyy h:mm"}}function G(r){const e=/^([+-]?)(\d+) (\d+)\/(\d+)$/.exec(r);if(!e)return null;const n=Number(e[4]);if(n===0)return null;const t=(Number(e[2])+Number(e[3])/n)*(e[1]==="-"?-1:1),o="?".repeat(Math.min(3,e[4].length));return{value:t,format:`# ${o}/${o}`}}function p(r){let e=0,n="";for(const t of r.replace(/,/g,""))t>="0"&&t<="9"?((e>0||t!=="0")&&e++,n+=e>15?"0":t):((t==="e"||t==="E")&&(e=-1/0),n+=t);return Number(n)}const f=p;function X(r){if(r.length<=32767)return r;const e=r.charCodeAt(32766);return r.slice(0,e>=55296&&e<=56319?32766:32767)}function J(r,e=T){if(r==="")return{kind:C};if(r[0]==="="&&r.length>1)return{kind:L,formula:r.slice(1)};if(r[0]==="'")return{kind:N,text:r.slice(1)};const n=r.trim(),t=r.toUpperCase();if(t==="TRUE")return{kind:g,value:1};if(t==="FALSE")return{kind:g,value:0};const o=M.indexOf(t);if(o>=0)return{kind:D,value:I.indexOf(M[o]),text:r};let u;if((A.test(n)||F.test(n))&&!Number.isFinite(p(n)))return{kind:N,text:r};if(A.test(n))return{kind:s,value:p(n)};if(Y.test(n))return{kind:s,value:f(n),format:n.includes(".")?"#,##0.00":"#,##0"};if(F.test(n))return{kind:s,value:p(n),format:"0.00E+00"};if(j.test(n)&&/\d/.test(n)){const a=n.slice(0,-1);return{kind:s,value:f(a)/100,format:/\.\d/.test(a)?"0.00%":"0%"}}if(u=/^\((\d{1,3}(?:,\d{3})+(?:\.\d*)?|\d+(?:\.\d*)?|\.\d+)\)$/.exec(n))return{kind:s,value:-f(u[1]),...u[1].includes(",")?{format:u[1].includes(".")?"#,##0.00":"#,##0"}:{}};const i=e.symbol.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),l=(a,k,w)=>{const P=f(k)*(a==="-"||w?-1:1);return{kind:s,value:P,format:q(/\.\d/.test(k),e)}};if(u=new RegExp(`^([+-]?)${i}\\s?([+-]?)(${y})$`).exec(n))return l(u[1]||u[2],u[3]);if(u=new RegExp(`^([+-]?)(${y})\\s?${i}$`).exec(n))return l(u[1],u[2]);if((u=new RegExp(`^\\(${i}\\s?(${y})\\)$`).exec(n))||(u=new RegExp(`^\\((${y})\\s?${i}\\)$`).exec(n)))return l("",u[1],!0);if(u=Z.exec(n))return{kind:s,value:f(u[2])*(u[1]==="-"?-1:1)};const v=G(n);if(v)return{kind:s,...v};const E=U(n);if(E)return{kind:s,...E};const h=x(n);if(h)return{kind:s,...h};const R=B(n);return R?{kind:s,...R}:{kind:N,text:r}}export{X as cellEntryText,J as parseInput,W as setEntryCurrency};
package/core/merge.js ADDED
@@ -0,0 +1 @@
1
+ import{EMPTY as g,CONTENT_MASK as v}from"./sheet.js";import{MAX_COLS as C}from"./address.js";import{formulaText as A,movedInfo as O}from"./formula-refs.js";import{tablesOf as S}from"./tables.js";const E=65536;function K(o,r){return(r.r1-r.r0+1)*(r.c1-r.c0+1)<=E?{r1:r.r1,c1:r.c1}:{r1:Math.max(r.r0,Math.min(r.r1,o.maxRow)),c1:Math.max(r.c0,Math.min(r.c1,o.maxCol))}}function h(o,r,e){const n=[],f=Math.min(r.r1,o.maxRow),c=Math.min(r.c1,o.maxCol);for(let u=r.r0;u<=f;u++)for(let t=r.c0;t<=c;t++)if((o.kind(u,t)&v)!==g&&(n.push({row:u,col:t}),n.length===e))return n;return n}function D(o,r=!1){if(!r)return[{r0:o.r0,c0:o.c0,r1:o.r1,c1:o.c1}];const e=[];for(let n=o.r0;n<=o.r1;n++)e.push({r0:n,c0:o.c0,r1:n,c1:o.c1});return e}function P(o,r,e){return S(o,r).some(n=>e.some(f=>f.r0<=n.r1&&n.r0<=f.r1&&f.c0<=n.c1&&n.c0<=f.c1))}function V(o,r){return r.some(e=>h(o,e,2).length>1)}function X(o,r,e,{center:n=!1}={}){const{sheet:f}=r;if(P(o,f,e))return null;const c=f.merges.slice(),u=[];for(const t of e)f.removeMergesIn(t);for(const t of e)b(o,r,t,n,u);return m(c,u)}function Y(o,r){return _(o,[r])}function _(o,r){const e=o.merges.slice();for(const n of r)o.removeMergesIn(n);return m(e)}function m(o,r=[],e=!0){return n=>{const f=n.merges;if(n.merges=o,n.mergeIndex=null,r.length>0){const c=new Map(n.valueMeta??[]);for(const{from:u,to:t,vm:M}of r)c.delete(e?t:u),c.set(e?u:t,M);n.valueMeta=c}return m(f,r,!e)}}function b(o,r,e,n,f){const{sheet:c}=r,{styles:u}=o,[t]=h(c,e,1),M=t??{row:e.r0,col:e.c0};let w=c.style(M.row,M.col);if(n&&(w=u.derive(w,{hAlign:"center"})),t!==void 0&&(t.row!==e.r0||t.col!==e.c0)){r.saveCell(e.r0,e.c0),c.setCell(e.r0,e.c0,c.kind(t.row,t.col)&v,c.value(t.row,t.col));const l=c.formula(t.row,t.col),s=A(l,t.row,t.col);c.setFormula(e.r0,e.c0,s===null?l:O(l,s,e.r0-t.row,e.c0-t.col)),l&&(o.formulasEdited=!0);const p=t.row*C+t.col,a=c.valueMeta?.get(p);if(a!==void 0){const d=e.r0*C+e.c0,i=new Map(c.valueMeta);i.delete(p),i.set(d,a),c.valueMeta=i,f.push({from:p,to:d,vm:a})}}const x=new Map,T=l=>{let s=x.get(l);return s===void 0&&(s=u.derive(w,{borders:u.get(l).borders??null}),x.set(l,s)),s},{r1:y,c1:I}=K(c,e);for(let l=e.r0;l<=y;l++)for(let s=e.c0;s<=I;s++){const a=(l!==e.r0||s!==e.c0)&&(c.kind(l,s)&v)!==g,d=c.style(l,s),i=T(d);!a&&i===d||(r.saveCell(l,s),a&&(c.setCell(l,s,g,0),c.setFormula(l,s,null)),i!==d&&c.setStyle(l,s,i))}c.addMerge({r0:e.r0,c0:e.c0,r1:e.r1,c1:e.c1})}export{X as mergeCells,V as mergeDropsValues,D as mergePieces,P as mergeTouchesTable,_ as unmergeAreas,Y as unmergeCells};
package/core/numfmt.js CHANGED
@@ -1 +1 @@
1
- import{calendarParts as X,dbnumDigits as Z,dbnumPlaces as H,eraAt as R,erasOf as P,hijriLocale as K,localeData as Q,mapDigits as V,numeralDigits as v,parseLocaleBracket as ee}from"../calcx/culture/format-locale.js";const y={decimal:".",group:",",months:["January","February","March","April","May","June","July","August","September","October","November","December"],days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],shortMonths:null,shortDays:null,shortDate:"m/d/yyyy",longDate:"dddd, mmmm d, yyyy",longTime:"h:mm:ss AM/PM",amDesignator:"AM",pmDesignator:"PM",tag:null,eras:null,numerals:null};function ge(t){Object.assign(y,t),G.clear()}const D="",be={0:"General",1:"0",2:"0.00",3:"#,##0",4:"#,##0.00",9:"0%",10:"0.00%",11:"0.00E+00",12:"# ?/?",13:"# ??/??",14:"m/d/yyyy",15:"d-mmm-yy",16:"d-mmm",17:"mmm-yy",18:"h:mm AM/PM",19:"h:mm:ss AM/PM",20:"h:mm",21:"h:mm:ss",22:"m/d/yyyy h:mm",37:"#,##0_);(#,##0)",38:"#,##0_);[Red](#,##0)",39:"#,##0.00_);(#,##0.00)",40:"#,##0.00_);[Red](#,##0.00)",41:'_(* #,##0_);_(* \\(#,##0\\);_(* "-"_);_(@_)',42:'_($* #,##0_);_($* \\(#,##0\\);_($* "-"_);_(@_)',43:'_(* #,##0.00_);_(* \\(#,##0.00\\);_(* "-"??_);_(@_)',44:'_($* #,##0.00_);_($* \\(#,##0.00\\);_($* "-"??_);_(@_)',45:"mm:ss",46:"[h]:mm:ss",47:"mm:ss.0",48:"##0.0E+0",49:"@"},B={black:"#000000",blue:"#0000FF",cyan:"#00FFFF",green:"#00FF00",magenta:"#FF00FF",red:"#FF0000",white:"#FFFFFF",yellow:"#FFFF00"},te=["000000","FFFFFF","FF0000","00FF00","0000FF","FFFF00","FF00FF","00FFFF","800000","008000","000080","808000","800080","008080","C0C0C0","808080","9999FF","993366","FFFFCC","CCFFFF","660066","FF8080","0066CC","CCCCFF","000080","FF00FF","FFFF00","00FFFF","800080","800000","008080","0000FF","00CCFF","CCFFFF","CCFFCC","FFFF99","99CCFF","FF99CC","CC99FF","FFCC99","3366FF","33CCCC","99CC00","FFCC00","FF9900","FF6600","666699","969696","003366","339966","003300","333300","993300","993366","333399","333333"];function U(t){const s=[[]];let n=s[0];const o=e=>{const i=n[n.length-1];i&&i.k==="lit"?i.s+=e:n.push({k:"lit",s:e})};for(let e=0;e<t.length;e++){const i=t[e];if((i==="G"||i==="g")&&t.slice(e,e+7).toLowerCase()==="general"){o("General"),e+=6;continue}switch(i){case";":n=[],s.push(n);break;case'"':{const a=t.indexOf('"',e+1);o(a<0?t.slice(e+1):t.slice(e+1,a)),e=a<0?t.length:a;break}case"\\":e+1<t.length&&o(t[++e]);break;case"_":e++,o(" ");break;case"*":n.push({k:"fill",c:t[++e]??" "});break;case"[":{const a=t.indexOf("]",e);if(a<0){o(i);break}const l=t.slice(e+1,a);e=a;const f=l.toLowerCase();if(/^dbnum[1-4]$/.test(f)){n.push({k:"dbnum",n:Number(f[5])});break}if(/^h+$/.test(f)||/^m+$/.test(f)||/^s+$/.test(f))n.push({k:"elapsed",c:f[0],n:f.length});else if(B[f])n.push({k:"color",v:B[f]});else if(/^color\s*\d+$/.test(f)){const d=te[Number(f.replace(/\D/g,""))-1];d&&n.push({k:"color",v:`#${d}`})}else if(/^(<=|>=|<>|<|>|=)\s*-?[\d.]+/.test(l)){const d=/^(<=|>=|<>|<|>|=)\s*(-?[\d.]+(?:[eE][+-]?\d+)?)/.exec(l);n.push({k:"cond",op:d[1],v:Number(d[2])})}else if(l[0]==="$"){const d=ee(l);d?.currency&&o(d.currency),d&&(d.tag||d.calendar||d.numerals||d.system||d.gannen)&&n.push({k:"loc",info:d})}break}case"@":n.push({k:"text"});break;case"#":case"0":case"?":n.push({k:"d",c:i});break;case".":n.push({k:"point"});break;case",":n.push({k:"comma"});break;case"%":n.push({k:"pct"});break;case"/":n.push({k:"slash"});break;case"E":case"e":{const a=t[e+1];if((a==="+"||a==="-")&&/[0#?]/.test(t[e+2]??""))n.push({k:"exp",plus:a==="+",upper:i==="E"}),e++;else if(i==="e"){let l=1;for(;t[e+l]==="e";)l++;n.push({k:"date",c:"e",n:l}),e+=l-1}else o(i);break}case"y":case"Y":case"m":case"M":case"d":case"D":case"h":case"H":case"s":case"S":{const a=i.toLowerCase();let l=1;for(;t[e+l]?.toLowerCase()===a;)l++;n.push({k:"date",c:a,n:l}),e+=l-1;break}case"g":case"G":case"b":case"B":{const a=i.toLowerCase();if(a==="b"&&t[e+1]==="2"){n.push({k:"hijri"}),e++;break}let l=1;for(;t[e+l]?.toLowerCase()===a;)l++;n.push({k:"date",c:a,n:l}),e+=l-1;break}case"A":case"a":{const a=t.slice(e,e+5).toUpperCase();if(/^AAA/.test(a)){let l=3;for(;t[e+l]?.toLowerCase()==="a";)l++;n.push({k:"date",c:"a",n:l}),e+=l-1}else a==="AM/PM"?(n.push({k:"ampm",long:!0}),e+=4):a.startsWith("A/P")?(n.push({k:"ampm",long:!1,lower:i==="a"}),e+=2):o(i);break}default:o(i)}}return s}function se(t){const s={tokens:t,color:null,cond:null,isDate:!1,isText:!1,isNum:!1,isGeneral:!1},n=[];for(const e of t)e.k==="color"?s.color=e.v:e.k==="cond"?s.cond=e:e.k==="loc"?s.loc=e.info:e.k==="dbnum"?s.dbnum=e.n:e.k==="hijri"?s.hijri=!0:n.push(e);if(s.loc?.system){n.length=0;for(const e of U(s.loc.system==="date"?y.longDate:y.longTime)[0])n.push(e)}s.tokens=n;const o=n.filter(e=>e.k==="lit").map(e=>e.s).join("");if(s.isDate=n.some(e=>e.k==="date"||e.k==="elapsed"||e.k==="ampm"),s.isText=n.some(e=>e.k==="text"),s.isNum=!s.isDate&&n.some(e=>e.k==="d"),/general/i.test(o)){s.isGeneral=!0;for(const e of n)e.k==="lit"&&(e.s=e.s.replace(/general/i,""))}return s.isDate?oe(s):s.isNum&&le(s),s.digits=ie(s),s}const ne=t=>t.op==="<"&&t.v<=0||t.op==="<="&&t.v<0||t.op==="="&&t.v<0;function q(t,s){const n=I(s);return t.dbnum?H(n,t.dbnum,t.loc?.tag??y.tag)??n:n}function ie(t){if(t.dbnum){const s=Z(t.dbnum,t.loc?.tag??y.tag);if(s)return s}return t.loc?.numerals>1?v(t.loc.numerals):y.numerals??null}function oe(t){if(t.tokens.some(e=>e.k==="date"&&e.c==="g")&&P(t.loc,y)){let e=!1;for(const i of t.tokens)i.k==="date"&&i.c==="g"?e=!0:e&&i.k==="date"&&i.c==="y"&&(i.c="e",i.n=2)}const s=t.tokens;let n=null;for(let e=0;e<s.length;e++){const i=s[e];if(i.k==="date"&&i.c==="m"&&i.n<=2){let a=null;for(let l=e+1;l<s.length;l++)if(s[l].k!=="lit"){a=s[l];break}(n==="h"||a&&a.k==="date"&&a.c==="s"||a&&a.k==="elapsed"&&a.c==="s")&&(i.c="n"),n=null}else i.k==="date"&&i.c==="h"||i.k==="elapsed"&&i.c==="h"?n="h":(i.k==="date"||i.k==="elapsed")&&(n=null)}t.hour12=s.some(e=>e.k==="ampm"),t.subDigits=0;const o=[];for(let e=0;e<s.length;e++){const i=s[e];if(i.k==="point"&&s[e+1]?.k==="d"){let a=0;for(;s[e+1+a]?.k==="d";)a++;o.push({k:"sub",n:a}),t.subDigits=Math.max(t.subDigits,a),e+=a}else i.k==="point"?o.push({k:"lit",s:"."}):i.k==="slash"?o.push({k:"lit",s:"/"}):i.k==="comma"?o.push({k:"lit",s:","}):i.k==="d"?o.push({k:"lit",s:i.c}):i.k==="pct"?o.push({k:"lit",s:"%"}):o.push(i)}t.tokens=o}function le(t){const s=t.tokens;let n=-1,o=-1,e=-1,i=-1,a=-1;s.forEach((c,k)=>{c.k==="d"?(o<0&&(o=k),n=k):c.k==="point"&&e<0?e=k:c.k==="exp"&&i<0?i=k:c.k==="slash"&&a<0&&o>=0&&(a=k)});let l=0,f=0,d=!1;const b=[];for(let c=0;c<s.length;c++){const k=s[c];if(k.k==="pct")l++,b.push({k:"lit",s:"%"});else if(k.k==="comma"){let m=!1;for(let h=c+1;h<s.length;h++){if(s[h].k==="d"){m=!0;break}if(s[h].k!=="comma")break}m&&c>o&&(e<0||c<e)?d=!0:c>o&&c<=n+8&&!m?f++:b.push({k:"lit",s:","})}else k.k==="slash"&&c!==a?b.push({k:"lit",s:"/"}):k.k==="point"&&c!==e?b.push({k:"lit",s:"."}):b.push(k)}t.tokens=b,t.pct=l,t.scale=f,t.grouping=d,t.fraction=a>=0,t.scientific=i>=0;let u="int";const p={int:0,dec:0,exp:0,num:0,den:0};let g="";for(const c of b)c.k==="point"?u="dec":c.k==="exp"?u="exp":c.k==="slash"?u="den":c.k==="d"?(c.part=u,p[u]++,u==="den"&&(g+=c.c)):c.k==="lit"&&u==="den"&&/^\d+$/.test(c.s)&&(g+=c.s);if(t.fraction){let k=b.findIndex(h=>h.k==="slash")-1,m=!1;for(;k>=0;){const h=b[k];if(h.k==="d")h.part="num",p.num++,p.int--,m=!0;else if(m)break;k--}t.fixedDen=/^\d+$/.test(g)?Number(g):0,t.denDigits=p.den}t.counts=p}function j(t,s){if(Number.isInteger(t)&&t<1e15)return{int:t===0?"":String(t),dec:"0".repeat(s)};const n=t.toExponential(14),o=n.indexOf("e"),e=n[0]+n.slice(2,o),a=Number(n.slice(o+1))+1+s;let l,f=!1;if(a<0?l="":a>=e.length?l=e+"0".repeat(a-e.length):(f=e.charCodeAt(a)>=53,l=e.slice(0,a)),f){let g=l.length-1;const c=l.split("");for(;g>=0;)if(c[g]==="9")c[g]="0",g--;else{c[g]=String.fromCharCode(c[g].charCodeAt(0)+1);break}l=c.join(""),g<0&&(l="1"+l)}const b=l.length-s;let u,p;return b<=0?(u="",p="0".repeat(-b)+l):(u=l.slice(0,b),p=l.slice(b)),u=u.replace(/^0+/,""),{int:u,dec:p}}function ae(t){if(t.length<=3)return t;let s="",n=t.length%3;for(n>0&&(s=t.slice(0,n));n<t.length;n+=3)s+=(s?y.group:"")+t.slice(n,n+3);return s}function re(t,s){if(s.fixedDen>0)return[Math.round(t*s.fixedDen),s.fixedDen];const n=10**s.denDigits-1;let[o,e,i,a]=[0,1,1,0],l=t;for(let f=0;f<32;f++){const d=Math.floor(l),b=d*i+o,u=d*a+e;if(u>n)break;[o,e,i,a]=[i,a,b,u];const p=l-d;if(p<1e-10)break;l=1/p}return[i,a||1]}function ce(t,s){let n=s;t.pct&&(n*=100**t.pct),t.scale&&(n/=1e3**t.scale);const{counts:o}=t;let e="",i="",a="",l=!1,f="",d="";if(t.fraction){const r=o.int>0?Math.floor(n):0,[F,M]=re(n-r,t);let S=r,w=F;w===M&&o.int>0&&(S+=1,w=0),e=S===0?w===0&&o.int>0?"0":"":String(S),f=w===0&&o.int>0?"":String(w),d=w===0&&o.int>0?"":String(M)}else if(t.scientific){let r=n===0?0:Math.floor(Math.log10(n));const F=Math.max(o.int,1);F>1&&(r=Math.floor(r/F)*F);let M=j(n/10**r,o.dec);M.int.length>F&&(r+=F>1?F:1,M=j(n/10**r,o.dec)),e=n===0&&F>1?"0".repeat(F):M.int,i=M.dec,l=r<0,a=String(Math.abs(r))}else{const r=j(n,o.dec);e=r.int,i=r.dec}let b=i.length,u="";const p=t.tokens,g=[];for(const r of p)r.k==="d"&&r.part==="int"&&g.push(r);const c=new Map;let k=e.length-1;for(let r=g.length-1;r>=0;r--){const F=g[r];if(k>=0){let M=e[k--];r===0&&k>=0&&(M=e.slice(0,k+1)+M,k=-1),c.set(F,M)}else c.set(F,F.c==="0"?"0":F.c==="?"?" ":"")}if(t.grouping&&g.length){let r="";for(const S of g)r+=c.get(S);const F=/^ */.exec(r)[0],M=ae(r.slice(F.length));c.forEach((S,w)=>c.set(w,"")),c.set(g[0],F+M)}const m=[];for(const r of p)r.k==="d"&&r.part==="dec"&&m.push(r);for(let r=m.length-1;r>=0&&!(m[r].c==="0"||i[r]!=="0");r--)b=r;let h=0,C=0;const x=p.filter(r=>r.k==="d"&&r.part==="exp").length,$=a.padStart(x,"0");let L=0;const O=p.filter(r=>r.k==="d"&&r.part==="num").length,T=f.padStart(O," ");let A=0;const N=p.filter(r=>r.k==="d"&&r.part==="den").length,E=d.padEnd(N," "),W=t.fraction&&f==="";for(const r of p)switch(r.k){case"lit":u+=r.s===""?"":r.s;break;case"fill":u+=D+r.c+D;break;case"point":g.length===0&&!t.fraction&&!t.scientific&&e!=="0"&&!u.includes(y.decimal)&&(u+=e),u+=y.decimal;break;case"exp":u+=(r.upper?"E":"e")+(l?"-":r.plus?"+":"");break;case"slash":u+=W?" ":"/";break;case"d":switch(r.part){case"int":u+=c.get(r);break;case"dec":{const F=h++;F<b?u+=i[F]:r.c==="?"?u+=" ":r.c==="0"&&(u+="0");break}case"exp":C===0&&(u+=$),C++;break;case"num":{L===0?u+=T.slice(0,T.length-O+1):u+=T[T.length-O+L],L++;break}case"den":{A<N&&(u+=E[A]),A++,A===N&&E.length>N&&(u+=E.slice(N));break}default:}break;default:}return u}function fe(t){let s=Math.floor(t),n=t-s,o=Math.round(n*864e5);o>=864e5&&(s+=1,o-=864e5);let e,i,a;if(s===0)e=1900,i=1,a=0;else if(s===60)e=1900,i=2,a=29;else{const f=(s<60?s+1:s)-25569+719468,d=Math.floor(f/146097),b=f-d*146097,u=Math.floor((b-Math.floor(b/1460)+Math.floor(b/36524)-Math.floor(b/146096))/365),p=b-(365*u+Math.floor(u/4)-Math.floor(u/100)),g=Math.floor((5*p+2)/153);a=p-Math.floor((153*g+2)/5)+1,i=g<10?g+3:g-9,e=u+d*400+(i<=2?1:0)}const l=s===0?6:(s-1)%7;return{year:e,month:i,day:a,weekday:l,ms:o,serial:t}}function Fe(t,s){const n=t.trim().split(/[^0-9]+/).filter(Boolean).map(Number);if(n.length!==3)return null;const o=f=>n[s.indexOf(f)];let e=o("Y");const i=o("M"),a=o("D");if(e<100&&(e+=e<30?2e3:1900),!(i>=1&&i<=12&&a>=1&&a<=new Date(Date.UTC(e,i,0)).getUTCDate()))return null;const l=ue(e,i,a);return Number.isFinite(l)?l:null}function ue(t,s,n){const o=s<=2?t-1:t,e=s<=2?s+9:s-3,i=Math.floor(o/400),a=o-i*400,l=Math.floor((153*e+2)/5)+n-1,f=a*365+Math.floor(a/4)-Math.floor(a/100)+l,d=i*146097+f-719468+25569;return d<61?d-1:d}const _=t=>t<10?"0"+t:String(t);function J(t,s){const n=86400*10**(t.subDigits??0);s=Math.round(s*n)/n;const o=fe(s),e=o.ms;let i=Math.floor(e/1e3);const a=e-i*1e3;let l="";t.subDigits>0?(l=(a/1e3).toFixed(t.subDigits).slice(2),l.length===0&&(l="")):a>=500&&(i+=1);let f=Math.floor(i/3600);const d=Math.floor(i%3600/60),b=i%60;f>=24&&(f=23);const u=t.hour12?f%12===0?12:f%12:f,{loc:p}=t,g=t.hijri&&!p?.tag,c=p?.tag?Q(p.tag):g?K():y,k=p?.calendar||g?X(g?"hijri":p.calendar,s,o,p?.tag??null)??o:o;let m="";for(const h of t.tokens)switch(h.k){case"lit":m+=h.s;break;case"fill":m+=D+h.c+D;break;case"date":switch(h.c){case"y":m+=h.n<=2?_(k.year%100):String(k.year);break;case"e":{const C=P(p,y);if(C===null){m+=String(k.year);break}const x=o.year-R(C,s).firstYear+1;x===1&&p?.gannen?m+="元":m+=h.n===1?x:_(x);break}case"g":{const C=P(p,y);if(C===null)break;const x=R(C,s);m+=h.n===1?x.symbol:h.n===2?x.abbreviation:x.name;break}case"b":{const C=p?.tag?o.year:o.year+543;m+=h.n<=2?_(C%100):String(C);break}case"a":m+=h.n===3?c.shortDays?.[o.weekday]??c.days[o.weekday].slice(0,3):c.days[o.weekday];break;case"m":h.n===1?m+=k.month:h.n===2?m+=_(k.month):k.monthName?m+=h.n===5?k.monthName[0]:k.monthName:h.n===3?m+=c.shortMonths?.[k.month-1]??c.months[k.month-1].slice(0,3):h.n===5?m+=c.months[k.month-1][0]:m+=c.months[k.month-1];break;case"d":h.n===1?m+=k.day:h.n===2?m+=_(k.day):h.n===3?m+=c.shortDays?.[o.weekday]??c.days[o.weekday].slice(0,3):m+=c.days[o.weekday];break;case"h":m+=h.n===1?u:_(u);break;case"n":m+=h.n===1?d:_(d);break;case"s":m+=h.n===1?b:_(b);break;default:}break;case"elapsed":{const x=Math.floor(s)*86400+i,$=h.c==="h"?Math.floor(x/3600):h.c==="m"?Math.floor(x/60):x;m+=String($).padStart(h.n,"0");break}case"ampm":h.long?m+=f<12?p?.tag?c.am:y.amDesignator:p?.tag?c.pm:y.pmDesignator:m+=h.lower?f<12?"a":"p":f<12?"A":"P";break;case"sub":m+=y.decimal+l.slice(0,h.n);break;default:}return m}function I(t,s=11){if(t===0)return"0";const n=Math.abs(t);let o;if(Number.isInteger(t)&&n<1e11)o=String(t);else if(n>=1e11||n<1e-9){const e=t.toExponential(Math.max(s-6,0)),i=e.indexOf("e");let a=e.slice(0,i);a.includes(".")&&(a=a.replace(/\.?0+$/,""));const l=Number(e.slice(i+1)),f=Math.abs(l);o=`${a}E${l<0?"-":"+"}${f<10?"0":""}${f}`}else{const e=n>=1?Math.floor(Math.log10(n))+1:1,i=Math.max(s-e-(n<1?1:0),0),a=n<1?-Math.floor(Math.log10(n))-1:0;o=Number(t.toPrecision(15)).toFixed(Math.min(Math.max(i,0),20)),a>=i&&i>=0&&Number(o)===0&&(o=t.toExponential(s-6)),o.includes(".")&&(o=o.replace(/\.?0+$/,""))}return y.decimal==="."?o:o.replace(".",y.decimal)}class he{constructor(s){this.code=s;const n=U(s);this.sections=n.slice(0,4).map(se);const o=this.sections[0];if(this.general=this.sections.length===1&&o.isGeneral&&!o.dbnum&&o.tokens.every(e=>e.k==="lit"&&e.s===""),this.isDate=this.sections.some(e=>e.isDate),this.isText=this.sections.length===1&&o.isText,this.sections.some(e=>e.digits)){const e=this.number;this.number=i=>{const a=this.sections[this._pick(i)],l=e.call(this,i);return a.digits?V(l,a.digits):l}}}_pick(s){const n=this.sections;if(n[0].cond!==null||n[1]&&n[1].cond!==null){for(let i=0;i<Math.min(n.length,3);i++){const a=n[i].cond;if(a===null){if(i===n.length-1||i===2||n[i].isText&&i===3)return i;continue}if(ke(a,s))return i}return Math.min(n.length-1,2)}const e=n.length>1&&n.length<4&&n[n.length-1].isText?n.length-1:n.length;return e===1?0:e===2?s<0?1:0:s>0?0:s<0?1:2}number(s){if(this.general)return I(s);const n=this._pick(s),o=this.sections[n],e=this.sections[0].cond!==null||this.sections[1]&&this.sections[1].cond!==null,i=s<0&&(o.cond!==null?!ne(o.cond):n===0||e);if(o.isDate){const l=s<0&&n>0?-s:s;return Y(o,l)?"#".repeat(8):l<0?`-${J(o,-l)}`:J(o,l)}if(o.isNum){const l=ce(o,Math.abs(s));return i&&de(l)?"-"+l:l}if(o.isGeneral){let l="";for(const f of o.tokens)f.k==="lit"?l+=f.s===""?q(o,Math.abs(s)):f.s.replace("",q(o,Math.abs(s))):f.k==="fill"&&(l+=D+f.c+D);return s<0&&n===0?"-"+l:l}let a=i?"-":"";for(const l of o.tokens)l.k==="lit"?a+=l.s:l.k==="fill"?a+=D+l.c+D:l.k==="pct"?a+="%":l.k==="text"&&(a+=I(Math.abs(s)));return a}dateOverflow(s){if(this.general)return!1;const n=this._pick(s),o=this.sections[n];return!!o.isDate&&Y(o,s<0&&n>0?-s:s)}text(s){const n=this.sections,o=n.length===4?n[3]:n.length===1&&n[0].isText?n[0]:null;if(o===null)return s;let e="";for(const i of o.tokens)i.k==="lit"?e+=i.s:i.k==="text"?e+=s:i.k==="fill"&&(e+=D+i.c+D);return e}color(s){return this.sections[this._pick(s)].color}}const me=2958466;function Y(t,s){return t.tokens.some(o=>o.k==="date"||o.k==="ampm")?s<0||s>=me:!1}function de(t){for(let s=0;s<t.length;s++){const n=t.charCodeAt(s);if(n>=49&&n<=57)return!0}return!1}function ke(t,s){switch(t.op){case"<":return s<t.v;case"<=":return s<=t.v;case">":return s>t.v;case">=":return s>=t.v;case"=":return s===t.v;default:return s!==t.v}}const G=new Map;function z(t){let s=G.get(t);return s===void 0&&(s=new he(t==="m/d/yyyy"?y.shortDate:t==="m/d/yyyy h:mm"?`${y.shortDate} h:mm`:t),G.set(t,s)),s}function ye(t,s){return z(t).number(s)}function xe(t){return z("#,##0").number(t)}export{be as BUILTIN_FORMATS,D as FILL_MARK,Fe as dateSerial,xe as formatCount,I as formatGeneral,ye as formatNumber,z as getFormatter,ue as partsToSerial,fe as serialToParts,ge as setNumberLocale};
1
+ import{calendarParts as K,dbnumDigits as Z,dbnumPlaces as Q,eraAt as B,erasOf as G,hijriLocale as V,localeData as v,mapDigits as ee,numeralDigits as te,parseLocaleBracket as se}from"../calcx/culture/format-locale.js";const y={decimal:".",group:",",months:["January","February","March","April","May","June","July","August","September","October","November","December"],days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],monthsWithDay:null,shortMonths:null,shortDays:null,shortDate:"m/d/yyyy",longDate:"dddd, mmmm d, yyyy",longTime:"h:mm:ss AM/PM",amDesignator:"AM",pmDesignator:"PM",tag:null,eras:null,numerals:null};function ye(e){Object.assign(y,e),R.clear()}const C="",xe="[$-F800]dddd, mmmm d, yyyy",Me={0:"General",1:"0",2:"0.00",3:"#,##0",4:"#,##0.00",9:"0%",10:"0.00%",11:"0.00E+00",12:"# ?/?",13:"# ??/??",14:"m/d/yyyy",15:"d-mmm-yy",16:"d-mmm",17:"mmm-yy",18:"h:mm AM/PM",19:"h:mm:ss AM/PM",20:"h:mm",21:"h:mm:ss",22:"m/d/yyyy h:mm",37:"#,##0_);(#,##0)",38:"#,##0_);[Red](#,##0)",39:"#,##0.00_);(#,##0.00)",40:"#,##0.00_);[Red](#,##0.00)",41:'_(* #,##0_);_(* \\(#,##0\\);_(* "-"_);_(@_)',42:'_($* #,##0_);_($* \\(#,##0\\);_($* "-"_);_(@_)',43:'_(* #,##0.00_);_(* \\(#,##0.00\\);_(* "-"??_);_(@_)',44:'_($* #,##0.00_);_($* \\(#,##0.00\\);_($* "-"??_);_(@_)',45:"mm:ss",46:"[h]:mm:ss",47:"mm:ss.0",48:"##0.0E+0",49:"@"},U={black:"#000000",blue:"#0000FF",cyan:"#00FFFF",green:"#00FF00",magenta:"#FF00FF",red:"#FF0000",white:"#FFFFFF",yellow:"#FFFF00"},ne=["000000","FFFFFF","FF0000","00FF00","0000FF","FFFF00","FF00FF","00FFFF","800000","008000","000080","808000","800080","008080","C0C0C0","808080","9999FF","993366","FFFFCC","CCFFFF","660066","FF8080","0066CC","CCCCFF","000080","FF00FF","FFFF00","00FFFF","800080","800000","008080","0000FF","00CCFF","CCFFFF","CCFFCC","FFFF99","99CCFF","FF99CC","CC99FF","FFCC99","3366FF","33CCCC","99CC00","FFCC00","FF9900","FF6600","666699","969696","003366","339966","003300","333300","993300","993366","333399","333333"];function W(e){const s=[[]];let n=s[0];const o=t=>{const i=n[n.length-1];i&&i.k==="lit"?i.s+=t:n.push({k:"lit",s:t})};for(let t=0;t<e.length;t++){const i=e[t];if((i==="G"||i==="g")&&e.slice(t,t+7).toLowerCase()==="general"){o("General"),t+=6;continue}switch(i){case";":n=[],s.push(n);break;case'"':{const a=e.indexOf('"',t+1);o(a<0?e.slice(t+1):e.slice(t+1,a)),t=a<0?e.length:a;break}case"\\":t+1<e.length&&o(e[++t]);break;case"_":t++,o(" ");break;case"*":n.push({k:"fill",c:e[++t]??" "});break;case"[":{const a=e.indexOf("]",t);if(a<0){o(i);break}const l=e.slice(t+1,a);t=a;const f=l.toLowerCase();if(/^dbnum[1-4]$/.test(f)){n.push({k:"dbnum",n:Number(f[5])});break}if(/^h+$/.test(f)||/^m+$/.test(f)||/^s+$/.test(f))n.push({k:"elapsed",c:f[0],n:f.length});else if(U[f])n.push({k:"color",v:U[f]});else if(/^color\s*\d+$/.test(f)){const d=ne[Number(f.replace(/\D/g,""))-1];d&&n.push({k:"color",v:`#${d}`})}else if(/^(<=|>=|<>|<|>|=)\s*-?[\d.]+/.test(l)){const d=/^(<=|>=|<>|<|>|=)\s*(-?[\d.]+(?:[eE][+-]?\d+)?)/.exec(l);n.push({k:"cond",op:d[1],v:Number(d[2])})}else if(l[0]==="$"){const d=se(l);d?.currency&&o(d.currency),d&&(d.tag||d.calendar||d.numerals||d.system||d.gannen)&&n.push({k:"loc",info:d})}break}case"@":n.push({k:"text"});break;case"#":case"0":case"?":n.push({k:"d",c:i});break;case".":n.push({k:"point"});break;case",":n.push({k:"comma"});break;case"%":n.push({k:"pct"});break;case"/":n.push({k:"slash"});break;case"E":case"e":{const a=e[t+1];if((a==="+"||a==="-")&&/[0#?]/.test(e[t+2]??""))n.push({k:"exp",plus:a==="+",upper:i==="E"}),t++;else if(i==="e"){let l=1;for(;e[t+l]==="e";)l++;n.push({k:"date",c:"e",n:l}),t+=l-1}else o(i);break}case"y":case"Y":case"m":case"M":case"d":case"D":case"h":case"H":case"s":case"S":{const a=i.toLowerCase();let l=1;for(;e[t+l]?.toLowerCase()===a;)l++;n.push({k:"date",c:a,n:l}),t+=l-1;break}case"g":case"G":case"b":case"B":{const a=i.toLowerCase();if(a==="b"&&e[t+1]==="2"){n.push({k:"hijri"}),t++;break}let l=1;for(;e[t+l]?.toLowerCase()===a;)l++;n.push({k:"date",c:a,n:l}),t+=l-1;break}case"A":case"a":{const a=e.slice(t,t+5).toUpperCase();if(/^AAA/.test(a)){let l=3;for(;e[t+l]?.toLowerCase()==="a";)l++;n.push({k:"date",c:"a",n:l}),t+=l-1}else a==="AM/PM"?(n.push({k:"ampm",long:!0}),t+=4):a.startsWith("A/P")?(n.push({k:"ampm",long:!1,lower:i==="a"}),t+=2):o(i);break}default:o(i)}}return s}function ie(e){const s={tokens:e,color:null,cond:null,isDate:!1,isText:!1,isNum:!1,isGeneral:!1},n=[];for(const t of e)t.k==="color"?s.color=t.v:t.k==="cond"?s.cond=t:t.k==="loc"?s.loc=t.info:t.k==="dbnum"?s.dbnum=t.n:t.k==="hijri"?s.hijri=!0:n.push(t);if(s.loc?.system){n.length=0;for(const t of W(s.loc.system==="date"?y.longDate:y.longTime)[0])n.push(t)}s.tokens=n;const o=n.filter(t=>t.k==="lit").map(t=>t.s).join("");if(s.isDate=n.some(t=>t.k==="date"||t.k==="elapsed"||t.k==="ampm"),s.isText=n.some(t=>t.k==="text"),s.isNum=!s.isDate&&n.some(t=>t.k==="d"),/general/i.test(o)){s.isGeneral=!0;for(const t of n)t.k==="lit"&&(t.s=t.s.replace(/general/i,""))}return s.isDate?ae(s):s.isNum&&re(s),s.digits=le(s),s}const oe=e=>e.op==="<"&&e.v<=0||e.op==="<="&&e.v<0||e.op==="="&&e.v<0;function Y(e,s){const n=E(s);return e.dbnum?Q(n,e.dbnum,e.loc?.tag??y.tag)??n:n}function le(e){if(e.dbnum){const s=Z(e.dbnum,e.loc?.tag??y.tag);if(s)return s}return e.loc?.numerals>1?te(e.loc.numerals):y.numerals??null}function ae(e){if(e.tokens.some(t=>t.k==="date"&&t.c==="g")&&G(e.loc,y)){let t=!1;for(const i of e.tokens)i.k==="date"&&i.c==="g"?t=!0:t&&i.k==="date"&&i.c==="y"&&(i.c="e",i.n=2)}const s=e.tokens;let n=null;for(let t=0;t<s.length;t++){const i=s[t];if(i.k==="date"&&i.c==="m"&&i.n<=2){let a=null;for(let l=t+1;l<s.length;l++)if(s[l].k!=="lit"){a=s[l];break}(n==="h"||a&&a.k==="date"&&a.c==="s"||a&&a.k==="elapsed"&&a.c==="s")&&(i.c="n"),n=null}else i.k==="date"&&i.c==="h"||i.k==="elapsed"&&i.c==="h"?n="h":(i.k==="date"||i.k==="elapsed")&&(n=null)}e.hasDay=s.some(t=>t.k==="date"&&t.c==="d"&&t.n<=2),e.hour12=s.some(t=>t.k==="ampm"),e.subDigits=0;const o=[];for(let t=0;t<s.length;t++){const i=s[t];if(i.k==="point"&&s[t+1]?.k==="d"){let a=0;for(;s[t+1+a]?.k==="d";)a++;o.push({k:"sub",n:a}),e.subDigits=Math.max(e.subDigits,a),t+=a}else i.k==="point"?o.push({k:"lit",s:"."}):i.k==="slash"?o.push({k:"lit",s:"/"}):i.k==="comma"?o.push({k:"lit",s:","}):i.k==="d"?o.push({k:"lit",s:i.c}):i.k==="pct"?o.push({k:"lit",s:"%"}):o.push(i)}e.tokens=o}function re(e){const s=e.tokens;let n=-1,o=-1,t=-1,i=-1,a=-1;s.forEach((c,k)=>{c.k==="d"?(o<0&&(o=k),n=k):c.k==="point"&&t<0?t=k:c.k==="exp"&&i<0?i=k:c.k==="slash"&&a<0&&o>=0&&(a=k)});let l=0,f=0,d=!1;const b=[];for(let c=0;c<s.length;c++){const k=s[c];if(k.k==="pct")l++,b.push({k:"lit",s:"%"});else if(k.k==="comma"){let m=!1;for(let h=c+1;h<s.length;h++){if(s[h].k==="d"){m=!0;break}if(s[h].k!=="comma")break}m&&c>o&&(t<0||c<t)?d=!0:c>o&&c<=n+8&&!m?f++:b.push({k:"lit",s:","})}else k.k==="slash"&&c!==a?b.push({k:"lit",s:"/"}):k.k==="point"&&c!==t?b.push({k:"lit",s:"."}):b.push(k)}e.tokens=b,e.pct=l,e.scale=f,e.grouping=d,e.fraction=a>=0,e.scientific=i>=0;let u="int";const p={int:0,dec:0,exp:0,num:0,den:0};let g="";for(const c of b)c.k==="point"?u="dec":c.k==="exp"?u="exp":c.k==="slash"?u="den":c.k==="d"?(c.part=u,p[u]++,u==="den"&&(g+=c.c)):c.k==="lit"&&u==="den"&&/^\d+$/.test(c.s)&&(g+=c.s);if(e.fraction){let k=b.findIndex(h=>h.k==="slash")-1,m=!1;for(;k>=0;){const h=b[k];if(h.k==="d")h.part="num",p.num++,p.int--,m=!0;else if(m)break;k--}e.fixedDen=/^\d+$/.test(g)?Number(g):0,e.denDigits=p.den}e.counts=p}function I(e,s){if(Number.isInteger(e)&&e<1e15)return{int:e===0?"":String(e),dec:"0".repeat(s)};const n=e.toExponential(14),o=n.indexOf("e"),t=n[0]+n.slice(2,o),a=Number(n.slice(o+1))+1+s;let l,f=!1;if(a<0?l="":a>=t.length?l=t+"0".repeat(a-t.length):(f=t.charCodeAt(a)>=53,l=t.slice(0,a)),f){let g=l.length-1;const c=l.split("");for(;g>=0;)if(c[g]==="9")c[g]="0",g--;else{c[g]=String.fromCharCode(c[g].charCodeAt(0)+1);break}l=c.join(""),g<0&&(l="1"+l)}const b=l.length-s;let u,p;return b<=0?(u="",p="0".repeat(-b)+l):(u=l.slice(0,b),p=l.slice(b)),u=u.replace(/^0+/,""),{int:u,dec:p}}function ce(e){if(e.length<=3)return e;let s="",n=e.length%3;for(n>0&&(s=e.slice(0,n));n<e.length;n+=3)s+=(s?y.group:"")+e.slice(n,n+3);return s}function fe(e,s){if(s.fixedDen>0)return[Math.round(e*s.fixedDen),s.fixedDen];const n=10**s.denDigits-1;let[o,t,i,a]=[0,1,1,0],l=e;for(let f=0;f<32;f++){const d=Math.floor(l),b=d*i+o,u=d*a+t;if(u>n)break;[o,t,i,a]=[i,a,b,u];const p=l-d;if(p<1e-10)break;l=1/p}return[i,a||1]}function ue(e,s){let n=s;e.pct&&(n*=100**e.pct),e.scale&&(n/=1e3**e.scale);const{counts:o}=e;let t="",i="",a="",l=!1,f="",d="";if(e.fraction){const r=o.int>0?Math.floor(n):0,[F,M]=fe(n-r,e);let S=r,_=F;_===M&&o.int>0&&(S+=1,_=0),t=S===0?_===0&&o.int>0?"0":"":String(S),f=_===0&&o.int>0?"":String(_),d=_===0&&o.int>0?"":String(M)}else if(e.scientific){let r=n===0?0:Math.floor(Math.log10(n));const F=Math.max(o.int,1);F>1&&(r=Math.floor(r/F)*F);let M=I(n/10**r,o.dec);M.int.length>F&&(r+=F>1?F:1,M=I(n/10**r,o.dec)),t=n===0&&F>1?"0".repeat(F):M.int,i=M.dec,l=r<0,a=String(Math.abs(r))}else{const r=I(n,o.dec);t=r.int,i=r.dec}let b=i.length,u="";const p=e.tokens,g=[];for(const r of p)r.k==="d"&&r.part==="int"&&g.push(r);const c=new Map;let k=t.length-1;for(let r=g.length-1;r>=0;r--){const F=g[r];if(k>=0){let M=t[k--];r===0&&k>=0&&(M=t.slice(0,k+1)+M,k=-1),c.set(F,M)}else c.set(F,F.c==="0"?"0":F.c==="?"?" ":"")}if(e.grouping&&g.length){let r="";for(const S of g)r+=c.get(S);const F=/^ */.exec(r)[0],M=ce(r.slice(F.length));c.forEach((S,_)=>c.set(_,"")),c.set(g[0],F+M)}const m=[];for(const r of p)r.k==="d"&&r.part==="dec"&&m.push(r);for(let r=m.length-1;r>=0&&!(m[r].c==="0"||i[r]!=="0");r--)b=r;let h=0,D=0;const x=p.filter(r=>r.k==="d"&&r.part==="exp").length,L=a.padStart(x,"0");let O=0;const P=p.filter(r=>r.k==="d"&&r.part==="num").length,T=f.padStart(P," ");let A=0;const N=p.filter(r=>r.k==="d"&&r.part==="den").length,j=d.padEnd(N," "),H=e.fraction&&f==="";for(const r of p)switch(r.k){case"lit":u+=r.s===""?"":r.s;break;case"fill":u+=C+r.c+C;break;case"point":g.length===0&&!e.fraction&&!e.scientific&&t!=="0"&&!u.includes(y.decimal)&&(u+=t),u+=y.decimal;break;case"exp":u+=(r.upper?"E":"e")+(l?"-":r.plus?"+":"");break;case"slash":u+=H?" ":"/";break;case"d":switch(r.part){case"int":u+=c.get(r);break;case"dec":{const F=h++;F<b?u+=i[F]:r.c==="?"?u+=" ":r.c==="0"&&(u+="0");break}case"exp":D===0&&(u+=L),D++;break;case"num":{O===0?u+=T.slice(0,T.length-P+1):u+=T[T.length-P+O],O++;break}case"den":{A<N&&(u+=j[A]),A++,A===N&&j.length>N&&(u+=j.slice(N));break}default:}break;default:}return u}function he(e){let s=Math.floor(e),n=e-s,o=Math.round(n*864e5);o>=864e5&&(s+=1,o-=864e5);let t,i,a;if(s===0)t=1900,i=1,a=0;else if(s===60)t=1900,i=2,a=29;else{const f=(s<60?s+1:s)-25569+719468,d=Math.floor(f/146097),b=f-d*146097,u=Math.floor((b-Math.floor(b/1460)+Math.floor(b/36524)-Math.floor(b/146096))/365),p=b-(365*u+Math.floor(u/4)-Math.floor(u/100)),g=Math.floor((5*p+2)/153);a=p-Math.floor((153*g+2)/5)+1,i=g<10?g+3:g-9,t=u+d*400+(i<=2?1:0)}const l=s===0?6:(s-1)%7;return{year:t,month:i,day:a,weekday:l,ms:o,serial:e}}function De(e,s){const n=e.trim().split(/[^0-9]+/).filter(Boolean).map(Number);if(n.length!==3)return null;const o=f=>n[s.indexOf(f)];let t=o("Y");const i=o("M"),a=o("D");if(t<100&&(t+=t<30?2e3:1900),!(i>=1&&i<=12&&a>=1&&a<=new Date(Date.UTC(t,i,0)).getUTCDate()))return null;const l=me(t,i,a);return Number.isFinite(l)?l:null}function me(e,s,n){const o=s<=2?e-1:e,t=s<=2?s+9:s-3,i=Math.floor(o/400),a=o-i*400,l=Math.floor((153*t+2)/5)+n-1,f=a*365+Math.floor(a/4)-Math.floor(a/100)+l,d=i*146097+f-719468+25569;return d<61?d-1:d}const w=e=>e<10?"0"+e:String(e);function q(e,s){const n=86400*10**(e.subDigits??0);s=Math.round(s*n)/n;const o=he(s),t=o.ms;let i=Math.floor(t/1e3);const a=t-i*1e3;let l="";e.subDigits>0?(l=(a/1e3).toFixed(e.subDigits).slice(2),l.length===0&&(l="")):a>=500&&(i+=1);let f=Math.floor(i/3600);const d=Math.floor(i%3600/60),b=i%60;f>=24&&(f=23);const u=e.hour12?f%12===0?12:f%12:f,{loc:p}=e,g=e.hijri&&!p?.tag,c=p?.tag?v(p.tag):g?V():y,k=p?.calendar||g?K(g?"hijri":p.calendar,s,o,p?.tag??null)??o:o;let m="";for(const h of e.tokens)switch(h.k){case"lit":m+=h.s;break;case"fill":m+=C+h.c+C;break;case"date":switch(h.c){case"y":m+=h.n<=2?w(k.year%100):String(k.year);break;case"e":{const D=G(p,y);if(D===null){m+=String(k.year);break}const x=o.year-B(D,s).firstYear+1;x===1&&p?.gannen?m+="元":m+=h.n===1?x:w(x);break}case"g":{const D=G(p,y);if(D===null)break;const x=B(D,s);m+=h.n===1?x.symbol:h.n===2?x.abbreviation:x.name;break}case"b":{const D=p?.tag?o.year:o.year+543;m+=h.n<=2?w(D%100):String(D);break}case"a":m+=h.n===3?c.shortDays?.[o.weekday]??c.days[o.weekday].slice(0,3):c.days[o.weekday];break;case"m":h.n===1?m+=k.month:h.n===2?m+=w(k.month):k.monthName?m+=h.n===5?k.monthName[0]:k.monthName:h.n===3?m+=c.shortMonths?.[k.month-1]??c.months[k.month-1].slice(0,3):h.n===5?m+=c.months[k.month-1][0]:m+=(e.hasDay?c.monthsWithDay??c.months:c.months)[k.month-1];break;case"d":h.n===1?m+=k.day:h.n===2?m+=w(k.day):h.n===3?m+=c.shortDays?.[o.weekday]??c.days[o.weekday].slice(0,3):m+=c.days[o.weekday];break;case"h":m+=h.n===1?u:w(u);break;case"n":m+=h.n===1?d:w(d);break;case"s":m+=h.n===1?b:w(b);break;default:}break;case"elapsed":{const x=Math.floor(s)*86400+i,L=h.c==="h"?Math.floor(x/3600):h.c==="m"?Math.floor(x/60):x;m+=String(L).padStart(h.n,"0");break}case"ampm":h.long?m+=f<12?p?.tag?c.am:y.amDesignator:p?.tag?c.pm:y.pmDesignator:m+=h.lower?f<12?"a":"p":f<12?"A":"P";break;case"sub":m+=y.decimal+l.slice(0,h.n);break;default:}return m}function de(e){return e.replace(/e([+-])(\d+)$/i,(s,n,o)=>`E${n}${o.padStart(2,"0")}`)}function $(e,s){const[n,o]=e.toExponential(s).split("e");return de(Number(n)+"e"+o).replace(".",y.decimal)}function J(e,s){const n=Number(e.toPrecision(15)).toFixed(s);return n.includes(".")?n.replace(/\.?0+$/,"").replace(".",y.decimal):n}function E(e,s=11){const n=Math.abs(e);if(Number.isInteger(e)&&n<1e11||!Number.isFinite(e))return String(e);const o=Math.max(s-6,0);if(n>=1e11||n<1e-9)return $(e,o);const t=Math.floor(Math.log10(n)),i=Math.max(0,s-(n<1?2:t+1));return t<0&&i+t<o&&Number(e.toFixed(i))!==e?$(e,o):J(e,i)}function Ce(e,s){const n=E(e);if(s(n))return n;const o=Math.abs(e);if(o===0)return n;const t=Math.floor(Math.log10(o)),i=!n.includes("E");for(let a=11;a>=1;a--){if(i&&a>t){const l=Math.min(20,a-1-t),f=J(e,l);if(s(f))return f}if(!i||t<0||a<=t){const l=$(e,a-1);if(s(l))return l}}return $(e,0)}class ke{constructor(s){this.code=s;const n=W(s);this.sections=n.slice(0,4).map(ie);const o=this.sections[0];if(this.general=this.sections.length===1&&o.isGeneral&&!o.dbnum&&o.tokens.every(t=>t.k==="lit"&&t.s===""),this.isDate=this.sections.some(t=>t.isDate),this.isText=this.sections.length===1&&o.isText,this.sections.some(t=>t.digits)){const t=this.number;this.number=i=>{const a=this.sections[this._pick(i)],l=t.call(this,i);return a.digits?ee(l,a.digits):l}}}_pick(s){const n=this.sections;if(n[0].cond!==null||n[1]&&n[1].cond!==null){for(let i=0;i<Math.min(n.length,3);i++){const a=n[i].cond;if(a===null){if(i===n.length-1||i===2||n[i].isText&&i===3)return i;continue}if(be(a,s))return i}return Math.min(n.length-1,2)}const t=n.length>1&&n.length<4&&n[n.length-1].isText?n.length-1:n.length;return t===1?0:t===2?s<0?1:0:s>0?0:s<0?1:2}number(s){if(this.general)return E(s);const n=this._pick(s),o=this.sections[n],t=this.sections[0].cond!==null||this.sections[1]&&this.sections[1].cond!==null,i=s<0&&(o.cond!==null?!oe(o.cond):n===0||t);if(o.isDate){const l=s<0&&n>0?-s:s;return z(o,l)?"#".repeat(8):l<0?`-${q(o,-l)}`:q(o,l)}if(o.isNum){const l=ue(o,Math.abs(s));return i&&ge(l)?"-"+l:l}if(o.isGeneral){let l="";for(const f of o.tokens)f.k==="lit"?l+=f.s===""?Y(o,Math.abs(s)):f.s.replace("",Y(o,Math.abs(s))):f.k==="fill"&&(l+=C+f.c+C);return s<0&&n===0?"-"+l:l}let a=i?"-":"";for(const l of o.tokens)l.k==="lit"?a+=l.s:l.k==="fill"?a+=C+l.c+C:l.k==="pct"?a+="%":l.k==="text"&&(a+=E(Math.abs(s)));return a}dateOverflow(s){if(this.general)return!1;const n=this._pick(s),o=this.sections[n];return!!o.isDate&&z(o,s<0&&n>0?-s:s)}text(s){const n=this.sections,o=n.length===4?n[3]:n.length===1&&n[0].isText?n[0]:null;if(o===null)return s;let t="";for(const i of o.tokens)i.k==="lit"?t+=i.s:i.k==="text"?t+=s:i.k==="fill"&&(t+=C+i.c+C);return t}color(s){return this.sections[this._pick(s)].color}}const pe=2958466;function z(e,s){return e.tokens.some(o=>o.k==="date"||o.k==="ampm")?s<0||s>=pe:!1}function ge(e){for(let s=0;s<e.length;s++){const n=e.charCodeAt(s);if(n>=49&&n<=57)return!0}return!1}function be(e,s){switch(e.op){case"<":return s<e.v;case"<=":return s<=e.v;case">":return s>e.v;case">=":return s>=e.v;case"=":return s===e.v;default:return s!==e.v}}const R=new Map;function X(e){let s=R.get(e);return s===void 0&&(s=new ke(e==="m/d/yyyy"?y.shortDate:e==="m/d/yyyy h:mm"?`${y.shortDate} h:mm`:e),R.set(e,s)),s}function _e(e,s){return X(e).number(s)}function we(e){return X("#,##0").number(e)}export{Me as BUILTIN_FORMATS,C as FILL_MARK,xe as SYSTEM_LONG_DATE,De as dateSerial,de as excelExponent,Ce as fitGeneral,we as formatCount,E as formatGeneral,_e as formatNumber,X as getFormatter,me as partsToSerial,he as serialToParts,ye as setNumberLocale};
@@ -0,0 +1 @@
1
+ import{parseInput as v}from"./input.js";import{NUM as F}from"./sheet.js";const u=r=>r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),M=new Set([" "," "," "]),O=r=>r.replace(/\B(?=(\d{3})+(?!\d))/g,",");function q(r){const{decimal:$,order:f}=r,a=r.group===$?"":r.group,c=M.has(a)?"[\\s\\u00a0\\u202f]":a?u(a):null,d=u($),l=`(?:\\d+(?:${d}\\d*)?|${d}\\d+)`,i=c?`(?:\\d{1,3}(?:${c}\\d{3})+(?:${d}\\d*)?|${l})`:l,S=c?new RegExp(c):null,g=c?new RegExp(c,"g"):null,B=new RegExp(`^([+-]?)(${i})(%?)$`),I=new RegExp(`^([+-]?${l})[eE]([+-]?\\d+)$`),y=r.currency?.symbol??"$",b=u(y),A=new RegExp(`^(${i})\\s?${b}$`),C=new RegExp(`^${b}\\s?(${i})$`),E=r.dateSep??"/",w=`(?:/|-|${u(E)})`,x=E==="."?"\\.?":"",D=new RegExp(`^(\\d{4})(${w})(\\d{1,2})\\2(\\d{1,2})${x}$`),G=new RegExp(`^(\\d{1,2})(${w})(\\d{1,2})(?:\\2(\\d{1,4}))?${x}$`),R=n=>Object.fromEntries(n.map((t,p)=>[t,[1,3,4][p]])),j=R(f),k=R(f.filter(n=>n!=="year")),h=n=>(g?n.replace(g,""):n).replace($,".");return n=>{if(n===""||n[0]==="'"||n[0]==="=")return n;const t=n.trim(),p=o=>v(o).kind===F?o:n;let e=B.exec(t);if(e){const o=h(e[2]);if(S?.test(e[2])){const[m,s]=o.split(".");return`${e[1]}${O(m)}${s===void 0?"":`.${s}`}${e[3]}`}return`${e[1]}${o}${e[3]}`}if(e=I.exec(t),e)return`${e[1].replace($,".")}e${e[2]}`;if(e=A.exec(t)??C.exec(t),e)return`${h(e[1])} ${y}`;if(e=D.exec(t),e)return p(`${e[1]}-${e[3]}-${e[4]}`);if(e=G.exec(t),e){const{month:o,day:m,year:s}=e[4]===void 0?k:j,N=`${Number(e[o])}/${Number(e[m])}`;return p(s?`${N}/${e[s]}`:N)}return n}}export{q as regionInput};
@@ -0,0 +1 @@
1
+ import{EMPTY as c,KIND_MASK as a,STR as d}from"./sheet.js";const l=t=>Math.ceil(t*1.33);function x(t,o,i,f){for(let n=0;n<t.columns.length;n++){if(t.columns[n]===void 0)continue;const e=t.kind(i,n)&a;e!==c&&(t.merges.length&&t.mergeAt(i,n)||f(n,e,o.styles.list[t.style(i,n)]??o.styles.list[0]))}}function g(t,o,i,f,n,e){let r=l(e.fontSize)+(e.ext?.pad?e.ext.pad[0]+e.ext.pad[2]:0);const u=n===d&&o.strings.rich!==null?o.strings.rich.get(t.value(i,f)):void 0;if(u)for(const s of u)s.sz&&(r=Math.max(r,l(s.sz)));return r}function m(t,o,i){let f=0;return x(t,o,i,(n,e,r)=>{f=Math.max(f,g(t,o,i,n,e,r))}),f}function p(t,o,i,f=m){const n=t.rows;return n.isHidden(i)||t.filteredRows?.has(i)||n.isCustom(i)&&!n.isAuto(i)?n.sizeOf(i):Math.max(n.defaultSize,f(t,o,i))}function h(t,o,i,f,n=2e3){if(!(f-i>n))for(let e=i;e<=f;e++){const r=p(t,o,e);r!==t.rows.sizeOf(e)&&t.rows.setSize(e,r,!0)}}export{p as autoRowHeight,g as cellLineHeight,x as eachFittedCell,m as fontRowHeight,h as growRowsToFonts,l as lineHeight};
package/core/sheet.js CHANGED
@@ -1 +1 @@
1
- import{Axis as A}from"./axis.js";import{MAX_COLS as v,MAX_ROWS as R}from"./address.js";import{FormulaStore as S}from"./formula-store.js";const u=10,M=1<<u,m=M-1,G=0,U=1,P=2,H=3,V=4,Y=8,j=16,W=32,X=64,L=7,Z=15,b=["#NULL!","#DIV/0!","#VALUE!","#REF!","#NAME?","#NUM!","#N/A","#GETTING_DATA","#SPILL!","#CALC!","#BUSY!","#CONNECT!","#BLOCKED!","#FIELD!","#UNKNOWN!"];class O{constructor(){this.kinds=new Uint8Array(M),this.values=new Float64Array(M),this.styles=null}}let K=1;class q{constructor(t){this.uid=K++,this.name=t,this.rowExtras=null,this.colExtras=null,this.valueMeta=null,this.columns=[],this.maxRow=-1,this.maxCol=-1,this.rows=new A(R,16),this.cols=new A(v,65),this._formulas=new S,this.spills=null,this.rowStyles=null,this.colStyles=null,this.merges=[],this.cellTypes=null,this.sparkRules=null,this.cellDropdowns=null,this.cellTags=null,this.cellPicOpts=null,this.cellButtons=null,this.cellStates=null,this.changes=null,this.autoMerges=null,this.background=null,this.outlineColumn=null,this.split=null,this.frozenTrailingRows=0,this.frozenTrailingCols=0,this.axisSizing=null,this.hideZeros=!1,this.mergeIndex=null,this.frozenRows=0,this.frozenCols=0,this.showGridlines=!0,this.zoom=1,this.sourceId=null,this.revision=0}_chunk(t,s){const e=this.columns[s];return e===void 0?void 0:e[t>>>u]}_ensure(t,s){let e=this.columns[s];e===void 0&&(e=[],this.columns[s]=e);const n=t>>>u;let i=e[n];return i===void 0&&(i=new O,e[n]=i),i}kind(t,s){const e=this._chunk(t,s);return e===void 0?0:e.kinds[t&m]}value(t,s){const e=this._chunk(t,s);return e===void 0?0:e.values[t&m]}style(t,s){const e=this._chunk(t,s);if(e!==void 0&&e.styles!==null){const n=e.styles[t&m];if(n!==0)return n}if(this.rowStyles!==null){const n=this.rowStyles.get(t);if(n!==void 0)return n}if(this.colStyles!==null){const n=this.colStyles.get(s);if(n!==void 0)return n}return 0}cellStyle(t,s){const e=this._chunk(t,s);return e===void 0||e.styles===null?0:e.styles[t&m]}get formulas(){return this._formulas}set formulas(t){this._formulas=t instanceof S?t:S.from(t)}formula(t,s){return this._formulas.at(t,s)}setCell(t,s,e,n){const i=this._ensure(t,s),l=t&m;i.kinds[l]=e,i.values[l]=n,t>this.maxRow&&(this.maxRow=t),s>this.maxCol&&(this.maxCol=s)}setStyle(t,s,e){const n=this._ensure(t,s);if(n.styles===null){if(e===0)return;n.styles=new Uint16Array(M)}n.styles[t&m]=e,t>this.maxRow&&(this.maxRow=t),s>this.maxCol&&(this.maxCol=s)}setFormula(t,s,e){e?this._formulas.put(t,s,e):this._formulas.remove(t,s)}applyBatch(t){const{rows:s,cols:e,kinds:n,values:i,styles:l,count:o}=t;let h=-1,f=-1,d=null,k=this.maxRow,g=this.maxCol;for(let r=0;r<o;r++){const a=s[r],c=e[r],y=a>>>u;(c!==h||y!==f)&&(d=this._ensure(a,c),h=c,f=y);const p=a&m;d.kinds[p]=n[r],d.values[p]=i[r];const _=l[r];_!==0&&(d.styles===null&&(d.styles=new Uint16Array(M)),d.styles[p]=_),c>g&&(g=c)}if(o>0&&s[o-1]>k&&(k=s[o-1]),this.maxRow=k,this.maxCol=g,t.formulas){const r=this._formulas;for(const[a,c]of t.formulas)r.set(a,c)}if(t.valueMeta){this.valueMeta===null&&(this.valueMeta=new Map);for(const[r,a]of t.valueMeta)this.valueMeta.set(r,a)}this.revision++}addMerge(t){this.merges.push(t),this.mergeIndex=null}removeMergesIn(t){const s=this.merges.length;this.merges=this.merges.filter(e=>!D(e,t)),this.merges.length!==s&&(this.mergeIndex=null)}_buildMergeIndex(){const t=new Map;for(const s of this.merges)for(let e=s.r0>>>u;e<=s.r1>>>u;e++){const n=t.get(e);n?n.push(s):t.set(e,[s])}this.mergeIndex=t}mergeAt(t,s){if(this.merges.length===0)return null;this.mergeIndex===null&&this._buildMergeIndex();const e=this.mergeIndex.get(t>>>u);if(e===void 0)return null;for(let n=0;n<e.length;n++){const i=e[n];if(t>=i.r0&&t<=i.r1&&s>=i.c0&&s<=i.c1)return i}return null}mergesInRows(t,s){if(this.merges.length===0)return[];this.mergeIndex===null&&this._buildMergeIndex();const e=new Set;for(let n=t>>>u;n<=s>>>u;n++){const i=this.mergeIndex.get(n);if(i)for(const l of i)l.r1>=t&&l.r0<=s&&e.add(l)}return[...e]}jumpRow(t,s,e){const n=this.columns[s],i=e>0?R-1:0;if(t===i)return t;const l=h=>{const f=n===void 0?void 0:n[h>>>u];return f!==void 0&&(f.kinds[h&m]&15)!==0};let o=t+e;if(l(t)&&l(o)){for(;o!==i&&l(o+e);)o+=e;return o}for(;o>=0&&o<R;){const h=n===void 0?void 0:n[o>>>u];if(h===void 0){const f=e>0?(o>>>u)+1<<u:(o>>>u<<u)-1;if(f<0||f>=R)return i;o=f;continue}if((h.kinds[o&m]&15)!==0)return o;o+=e}return i}jumpCol(t,s,e){const n=e>0?v-1:0;if(s===n)return s;const i=h=>(this.kind(t,h)&15)!==0;let l=s+e;if(i(s)&&i(l)){for(;l!==n&&i(l+e);)l+=e;return l}const o=e>0?this.maxCol:0;for(;l!==n;){if(i(l))return l;if(e>0&&l>o)return n;l+=e}return n}aggregate(t){let s=0,e=0,n=0,i=1/0,l=-1/0;for(const{r0:o,c0:h,r1:f,c1:d}of t){const k=Math.min(d,this.maxCol),g=Math.min(f,this.maxRow);for(let r=h;r<=k;r++){const a=this.columns[r];if(a!==void 0)for(let c=o>>>u;c<=g>>>u;c++){const y=a[c];if(y===void 0)continue;const p=c<<u,_=Math.max(o-p,0),N=Math.min(g-p,m),{kinds:T,values:E}=y;for(let I=_;I<=N;I++){const w=T[I]&L;if(w!==0&&(n++,w===U)){const C=E[I];s+=C,e++,C<i&&(i=C),C>l&&(l=C)}}}}}return{sum:s,numeric:e,filled:n,min:i,max:l}}}function D(x,t){return x.r0<=t.r1&&x.r1>=t.r0&&x.c0<=t.c1&&x.c1>=t.c0}export{H as BOOL,u as CHUNK_BITS,m as CHUNK_MASK,M as CHUNK_SIZE,Z as CONTENT_MASK,W as DIRTY,G as EMPTY,V as ERR,b as ERRORS,X as EVALUATING,Y as FORMULA,L as KIND_MASK,U as NUM,j as SPILL,P as STR,q as Sheet,D as rangesTouch};
1
+ import{Axis as A}from"./axis.js";import{MAX_COLS as R,MAX_ROWS as N}from"./address.js";import{FormulaStore as S}from"./formula-store.js";const u=10,M=1<<u,g=M-1,H=0,O=1,V=2,Y=3,j=4,W=8,X=16,Z=32,b=64,K=7,v=15,q=["#NULL!","#DIV/0!","#VALUE!","#REF!","#NAME?","#NUM!","#N/A","#GETTING_DATA","#SPILL!","#CALC!","#BUSY!","#CONNECT!","#BLOCKED!","#FIELD!","#UNKNOWN!"];class z{constructor(){this.kinds=new Uint8Array(M),this.values=new Float64Array(M),this.styles=null}}function E(a,t,e,s,n,l){const o=(h,m)=>{if(!s||h<0||h>=e||s.sizeOf(h)>0)return h;const x=s.offsetOf(h)-(m<0?1:0);return x<0?-1:x>=s.total?e:s.indexAt(x)},r=o(t>0?e-1:0,-t);if((r-a)*t<=0)return a;let i=o(a+t,t);if(n(a)&&n(i)){for(;i!==r;){const h=o(i+t,t);if(!n(h))break;i=h}return i}for(;(r-i)*t>=0;){if(n(i))return i;i=o(l(i),t)}return r}let D=1;class J{constructor(t){this.uid=D++,this.name=t,this.rowExtras=null,this.colExtras=null,this.valueMeta=null,this.columns=[],this.maxRow=-1,this.maxCol=-1,this.rows=new A(N,16),this.cols=new A(R,65),this._formulas=new S,this.spills=null,this.rowStyles=null,this.colStyles=null,this.merges=[],this.cellTypes=null,this.sparkRules=null,this.cellDropdowns=null,this.cellTags=null,this.cellPicOpts=null,this.cellButtons=null,this.cellStates=null,this.changes=null,this.autoMerges=null,this.background=null,this.outlineColumn=null,this.split=null,this.frozenTrailingRows=0,this.frozenTrailingCols=0,this.axisSizing=null,this.hideZeros=!1,this.mergeIndex=null,this.frozenRows=0,this.frozenCols=0,this.showGridlines=!0,this.zoom=1,this.sourceId=null,this.revision=0}_chunk(t,e){const s=this.columns[e];return s===void 0?void 0:s[t>>>u]}_ensure(t,e){let s=this.columns[e];s===void 0&&(s=[],this.columns[e]=s);const n=t>>>u;let l=s[n];return l===void 0&&(l=new z,s[n]=l),l}kind(t,e){const s=this._chunk(t,e);return s===void 0?0:s.kinds[t&g]}value(t,e){const s=this._chunk(t,e);return s===void 0?0:s.values[t&g]}style(t,e){const s=this._chunk(t,e);if(s!==void 0&&s.styles!==null){const n=s.styles[t&g];if(n!==0)return n}if(this.rowStyles!==null){const n=this.rowStyles.get(t);if(n!==void 0)return n}if(this.colStyles!==null){const n=this.colStyles.get(e);if(n!==void 0)return n}return 0}cellStyle(t,e){const s=this._chunk(t,e);return s===void 0||s.styles===null?0:s.styles[t&g]}get formulas(){return this._formulas}set formulas(t){this._formulas=t instanceof S?t:S.from(t)}formula(t,e){return this._formulas.at(t,e)}setCell(t,e,s,n){const l=this._ensure(t,e),o=t&g;l.kinds[o]=s,l.values[o]=n,t>this.maxRow&&(this.maxRow=t),e>this.maxCol&&(this.maxCol=e)}setStyle(t,e,s){const n=this._ensure(t,e);if(n.styles===null){if(s===0)return;n.styles=new Uint16Array(M)}n.styles[t&g]=s,t>this.maxRow&&(this.maxRow=t),e>this.maxCol&&(this.maxCol=e)}setFormula(t,e,s){s?this._formulas.put(t,e,s):this._formulas.remove(t,e)}applyBatch(t){const{rows:e,cols:s,kinds:n,values:l,styles:o,count:r}=t;let i=-1,h=-1,m=null,x=this.maxRow,p=this.maxCol;for(let c=0;c<r;c++){const d=e[c],f=s[c],y=d>>>u;(f!==i||y!==h)&&(m=this._ensure(d,f),i=f,h=y);const k=d&g;m.kinds[k]=n[c],m.values[k]=l[c];const _=o[c];_!==0&&(m.styles===null&&(m.styles=new Uint16Array(M)),m.styles[k]=_),f>p&&(p=f)}if(r>0&&e[r-1]>x&&(x=e[r-1]),this.maxRow=x,this.maxCol=p,t.formulas){const c=this._formulas;for(const[d,f]of t.formulas)c.set(d,f)}if(t.valueMeta){this.valueMeta===null&&(this.valueMeta=new Map);for(const[c,d]of t.valueMeta)this.valueMeta.set(c,d)}this.revision++}addMerge(t){this.merges.push(t),this.mergeIndex=null}removeMergesIn(t){const e=this.merges.length;this.merges=this.merges.filter(s=>!B(s,t)),this.merges.length!==e&&(this.mergeIndex=null)}_buildMergeIndex(){const t=new Map;for(const e of this.merges)for(let s=e.r0>>>u;s<=e.r1>>>u;s++){const n=t.get(s);n?n.push(e):t.set(s,[e])}this.mergeIndex=t}mergeAt(t,e){if(this.merges.length===0)return null;this.mergeIndex===null&&this._buildMergeIndex();const s=this.mergeIndex.get(t>>>u);if(s===void 0)return null;for(let n=0;n<s.length;n++){const l=s[n];if(t>=l.r0&&t<=l.r1&&e>=l.c0&&e<=l.c1)return l}return null}mergesInRows(t,e){if(this.merges.length===0)return[];this.mergeIndex===null&&this._buildMergeIndex();const s=new Set;for(let n=t>>>u;n<=e>>>u;n++){const l=this.mergeIndex.get(n);if(l)for(const o of l)o.r1>=t&&o.r0<=e&&s.add(o)}return[...s]}jumpRow(t,e,s,n=!1){const l=this.columns[e],o=i=>{const h=l?.[i>>>u];return h!==void 0&&(h.kinds[i&g]&v)!==0},r=i=>l?.[i>>>u]!==void 0?i+s:s>0?(i>>>u)+1<<u:(i>>>u<<u)-1;return E(t,s,n?this.rows.count:N,n&&this.rows,o,r)}jumpCol(t,e,s,n=!1){const l=r=>(this.kind(t,r)&v)!==0,o=r=>s>0&&r>=this.maxCol?R:r+s;return E(e,s,n?this.cols.count:R,n&&this.cols,l,o)}aggregate(t){let e=0,s=0,n=0,l=1/0,o=-1/0;for(const{r0:r,c0:i,r1:h,c1:m}of t){const x=Math.min(m,this.maxCol),p=Math.min(h,this.maxRow);for(let c=i;c<=x;c++){const d=this.columns[c];if(d!==void 0)for(let f=r>>>u;f<=p>>>u;f++){const y=d[f];if(y===void 0)continue;const k=f<<u,_=Math.max(r-k,0),T=Math.min(p-k,g),{kinds:U,values:L}=y;for(let I=_;I<=T;I++){const w=U[I]&K;if(w!==0&&(n++,w===O)){const C=L[I];e+=C,s++,C<l&&(l=C),C>o&&(o=C)}}}}}return{sum:e,numeric:s,filled:n,min:l,max:o}}}function B(a,t){return a.r0<=t.r1&&a.r1>=t.r0&&a.c0<=t.c1&&a.c1>=t.c0}export{Y as BOOL,u as CHUNK_BITS,g as CHUNK_MASK,M as CHUNK_SIZE,v as CONTENT_MASK,Z as DIRTY,H as EMPTY,j as ERR,q as ERRORS,b as EVALUATING,W as FORMULA,K as KIND_MASK,O as NUM,X as SPILL,V as STR,J as Sheet,B as rangesTouch};
@@ -0,0 +1 @@
1
+ import{CHUNK_BITS as v,CHUNK_MASK as A,CHUNK_SIZE as q,FORMULA as ln,STR as sn}from"./sheet.js";import{FormulaStore as D}from"./formula-store.js";import{cellRef as B,MAX_COLS as w,MAX_ROWS as k,parseCellRef as L,parseRangeRef as X,rangeRef as H}from"./address.js";import{translateFormula as on}from"./formula-refs.js";import{adjustFormula as T,adjustRange as O}from"../calc/adjust.js";import{clonePageSetup as en,shiftPageSetup as an}from"../print/page-setup.js";import{shiftSparse as cn}from"./sparse.js";import{shiftRuleFormulas as un}from"../cells/cf-file.js";import{noteStructure as fn}from"./changes.js";import{shiftHeaderColors as mn}from"../cells/header-colors.js";function V(t){return{kinds:new Uint8Array(q),values:new Float64Array(q),styles:t.styles===null?null:new Uint16Array(q)}}function W(t,n,r){if(!t||n===0)return;const s=/outlineLevel="\d+"/.exec(t.get(n-1)??"");if(s)for(let o=n;o<n+r;o++)t.set(o,s[0])}function Z(t,n,r,s,o,c){let l=s,e=r,u=c;for(;l>0;){const a=u>>>v,i=u&A,m=Math.min(l,q-i);let y=o[a];y===void 0&&(y=V(t),o[a]=y);const d=e-n;y.kinds.set(t.kinds.subarray(d,d+m),i),y.values.set(t.values.subarray(d,d+m),i),t.styles!==null&&(y.styles===null&&(y.styles=new Uint16Array(q)),y.styles.set(t.styles.subarray(d,d+m),i)),l-=m,e+=m,u+=m}}function G(t,n,r){const s=n>>>v,o=t.slice(0,s),c=t[s];c!==void 0&&(n&A)>0&&Z(c,s<<v,s<<v,n&A,o,s<<v);const l=r<0?n-r:n;for(let e=s;e<t.length;e++){const u=t[e];if(u===void 0)continue;const a=e<<v,i=Math.max(a,l),m=a+q;if(i>=m)continue;const y=i+r;if(y>=k)continue;const d=Math.min(m-i,k-y);Z(u,a,i,d,o,y)}return o}function dn(t,n,r){const s=[];for(let o=0;o<t.length;o++){const c=t[o];c!==void 0&&(o<n?s[o]=c:r>0?o+r<w&&(s[o+r]=c):o>=n-r&&(s[o+r]=c))}return s}function gn(t,n,r,s){const o=r-1,c=Math.min(s,(n?k:w)-r);if(c<=0)return;if(n){const u=c*t.columns.filter(Boolean).length<=4194304;for(let i=0;u&&i<t.columns.length;i++){if(t.columns[i]===void 0)continue;const m=t.cellStyle(o,i);if(m!==0)for(let y=0;y<c;y++)t.setStyle(r+y,i,m)}const a=t.rowStyles?.get(o);if(a!==void 0)for(let i=0;i<c;i++)t.rowStyles.set(r+i,a);return}const l=t.columns[o];if(l!==void 0&&l.some(u=>u?.styles)){for(let u=0;u<c;u++){const a=[];l.forEach((i,m)=>{if(i===void 0||i.styles===null)return;const y=V(i);y.styles.set(i.styles),a[m]=y}),t.columns[r+u]=a}r+c-1>t.maxCol&&(t.maxCol=r+c-1)}const e=t.colStyles?.get(o);if(e!==void 0)for(let u=0;u<c;u++)t.colStyles.set(r+u,e)}function N(t,n,r){if(t===null)return null;const s=new Map;for(const[o,c]of t)o<n?s.set(o,c):(r>0||o>=n-r)&&s.set(o+r,c);return s}const U=(t,n,r)=>r.axis==="row"?t<r.at?[t,n]:r.n>0?t+r.n<k?[t+r.n,n]:null:t>=r.at-r.n?[t+r.n,n]:null:n<r.at?[t,n]:r.n>0?n+r.n<w?[t,n+r.n]:null:n>=r.at-r.n?[t,n+r.n]:null;function J(t,n,r){if(t.f!==void 0)return t.f;if(t.sf!==void 0){const s=Math.floor(t.base/w);return on(t.sf,n-s,r-(t.base-s*w))}return null}const P=(t,n)=>(t.f??t.sf)?.toUpperCase().includes(n)===!0;function Q(t,n){for(const r of t.descriptors())if(P(r,n))return!0;return!1}function _(t,n){const r=X(t),s=r&&O(r,n);return s?H(s):t}function Y(t,n,r,s,o){if(t.t==="dataTable"){if(!s)return t;const l={...t,ref:_(t.ref,r),tpl:void 0};return t.r1&&(l.r1=_(t.r1,r)),t.r2&&(l.r2=_(t.r2,r)),l}const c={f:T(n,r,s,o)};return t.typed&&(c.typed=!0),t.ca&&(c.ca=t.ca),t.cm&&(c.cm=t.cm),t.t==="array"&&(c.t="array",c.ref=s&&t.ref?_(t.ref,r):t.ref),c}function Un(t,n,r){t.collab?.structure(n,r);const{at:s,n:o}=r,c=r.axis==="row",l=$(t,n),e=fn(n,r);if(c){const d=[];n.columns.forEach((p,R)=>{d[R]=G(p,s,o)}),n.columns=d,n.rows=n.rows.shifted(s,o),n.rowStyles=N(n.rowStyles,s,o),n.rowExtras=N(n.rowExtras,s,o),o>0&&W(n.rowExtras,s,o),n.maxRow=n.maxRow>=s?Math.max(s-1,n.maxRow+o):n.maxRow,n.maxRow>=k&&(n.maxRow=k-1),n.frozenRows>s&&(n.frozenRows=Math.max(s,n.frozenRows+o))}else n.columns=dn(n.columns,s,o),n.cols=n.cols.shifted(s,o),n.colStyles=N(n.colStyles,s,o),n.colExtras=N(n.colExtras,s,o),o>0&&W(n.colExtras,s,o),n.colsChanged=!0,n.maxCol=n.maxCol>=s?Math.max(s-1,n.maxCol+o):n.maxCol,n.maxCol>=w&&(n.maxCol=w-1),n.frozenCols>s&&(n.frozenCols=Math.max(s,n.frozenCols+o));if(o>0&&s>0&&!r.noFormat&&gn(n,c,s,o),n.merges=n.merges.map(d=>O(d,r)).filter(d=>d!==null&&(d.r0!==d.r1||d.c0!==d.c1)),n.mergeIndex=null,n.spills){const d=new Map;for(const[p,R]of n.spills){const f=Math.floor(p/w),x=U(f,p-f*w,r),g=O(R,r);x&&g&&d.set(x[0]*w+x[1],{...R,...g})}n.spills=d}if(n.valueMeta){const d=new Map;for(const[p,R]of n.valueMeta){const f=Math.floor(p/w),x=U(f,p-f*w,r);x&&d.set(x[0]*w+x[1],R)}n.valueMeta=d}const u=n.name.toUpperCase();for(const d of t.sheets){const p=d===n;if(!p&&!Q(d.formulas,u))continue;const R=new D;d.formulas.forEachCell((f,x,g)=>{const C=p?U(f,x,r):[f,x];if(C===null)return;if(!p&&!P(g,u)){R.put(C[0],C[1],g);return}const S=J(g,f,x);if(S===null||!p&&!S.toUpperCase().includes(u)){R.put(C[0],C[1],g);return}const M=Y(g,S,r,p,n.name);if(M.t==="array"&&M.ref&&p){const z=d.spills?.get(C[0]*w+C[1]);z&&(M.ref=H(z))}R.put(C[0],C[1],M)}),d.formulas=R}const a=tn(t,n,r),i=t.engine;if(i){for(const d of i.names.values())d.text=T(d.text,r,!1,n.name);i.names.size>0&&(t.namesChanged=!0),i.nameTemplates.clear(),i.rebuild()}n.structureOps=[...n.structureOps??[],r],n.pageSetup&&(an(n.pageSetup,r),n.pageSetupChanged=!0),n.viewChanged=!0,n.revision++,t.collab?.structureDone(n);const m=(d,p)=>()=>m(d(),p()),y=m(a,b(t,n,l));return e===null?y:m(y,e)}const E=(t,n)=>{if(n.span){const[r,s]=n.span;if(!(n.axis==="row"?t.c0>=r&&t.c1<=s:t.r0>=r&&t.r1<=s))return t}return O(t,n)};function j(t,n){return t.split(/\s+/).map(X).filter(Boolean).map(r=>E(r,n)).filter(Boolean).map(H).join(" ")}function F(t,n,r){if(r.span){const[s,o]=r.span,c=r.axis==="row"?n:t;if(c<s||c>o)return[t,n]}return U(t,n,r)}function h(t,n){if(!t)return t;const r=E(t,n);if(r===null)return null;let{columns:s}=t;if(n.axis==="col"&&s?.size>0){s=new Map;for(const[c,l]of t.columns){const e=F(t.r0,c,n);e&&s.set(e[1],l)}}let{buttons:o}=t;if(n.axis==="col"&&o?.size>0){o=new Map;for(const[c,l]of t.buttons){const e=F(t.r0,c,n);e&&o.set(e[1],l)}}return{...t,...r,columns:s,buttons:o,sortState:nn(t.sortState,n)}}function nn(t,n){if(!t)return t??null;const r=E(t,n);if(r===null)return null;const s=[];for(const o of t.conditions){const c=E(o,n);c!==null&&s.push({...o,...c})}return s.length>0?{...t,...r,conditions:s}:null}const I=t=>({sheets:t.sheets.map(n=>[n,{cf:n.cf,cellStates:n.cellStates,headerColors:n.headerColors,validations:n.validations,hyperlinks:n.hyperlinks,ignoredErrors:n.ignoredErrors,comments:n.comments,sparklines:n.sparklines,sparkRules:n.sparkRules,autoFilter:n.autoFilter,advancedFilter:n.advancedFilter,outlineColumn:n.outlineColumn,outlineVersion:(n.outlineVersion??0)+1,sortState:n.sortState,filteredRows:n.filteredRows,scenarios:n.scenarios,threads:n.threads}]),tables:t.engine?[...t.engine.tables.values()].map(n=>[n,{r0:n.r0,c0:n.c0,r1:n.r1,c1:n.c1,columns:n.columns,filter:n.filter,totals:n.totals,dirty:n.dirty,columnsChanged:n.columnsChanged}]):[]}),rn=t=>{for(const[n,r]of t.sheets)Object.assign(n,r),n.cfEval?.invalidate();for(const[n,r]of t.tables)Object.assign(n,r)};function tn(t,n,r){const s=I(t),o=n.name;for(const l of t.sheets){const e=l===n,u=a=>typeof a=="string"&&a!==""?T(a,r,e,o):a;if(l.cf?.length>0&&(l.cf=l.cf.map(({_ranges:a,...i})=>({...i,sqref:e?j(i.sqref,r):i.sqref,rules:i.rules.map(m=>un(m,u))})).filter(a=>a.sqref!==""),l.cfChanged=!0,l.cfEval?.invalidate()),e&&l.headerColors&&!r.span&&(l.headerColors=mn(l.headerColors,r)),e&&l.cellStates?.length>0){const a=l.cellStates.map(i=>({...i,ranges:i.ranges.map(m=>E(m,r)).filter(Boolean)})).filter(i=>i.ranges.length>0);l.cellStates=a.length>0?a:null}l.validations?.length>0&&(l.validations=l.validations.map(({_ranges:a,...i})=>({...i,sqref:e?j(i.sqref,r):i.sqref,formula1:u(i.formula1),formula2:u(i.formula2)})).filter(a=>a.sqref!==""),l.validationsChanged=!0),e&&l.ignoredErrors?.length>0&&(l.ignoredErrors=l.ignoredErrors.map(a=>({...a,sqref:j(a.sqref,r)})).filter(a=>a.sqref!==""),l.ignoredErrorsChanged=!0),l.hyperlinks?.length>0&&(l.hyperlinks=l.hyperlinks.map(({_ranges:a,...i})=>{const m=e?j(i.ref,r):i.ref;return{...i,ref:m,...i.location?{location:u(i.location)}:{}}}).filter(a=>a.ref!==""),l.hyperlinksChanged=!0),l.sparkRules?.length>0&&(l.sparkRules=l.sparkRules.map(a=>({...a,sqref:e?j(a.sqref,r):a.sqref,formula:u(a.formula)})).filter(a=>a.sqref!=="")),l.sparklines?.length>0&&(l.sparklines=l.sparklines.map(a=>({...a,items:a.items.map(i=>{const m=L(i.cell),y=e&&m?F(m.row,m.col,r):m?[m.row,m.col]:null;return y?{...i,cell:B(y[0],y[1]),source:u(i.source)}:null}).filter(Boolean)})).filter(a=>a.items.length>0),l.sparklinesChanged=!0)}if(n.comments?.size>0){const l=new Map;for(const[e,u]of n.comments){const a=Math.floor(e/w),i=F(a,e-a*w,r);i&&l.set(i[0]*w+i[1],{...u,ref:B(i[0],i[1])})}n.comments=l,n.commentsChanged=!0}if(n.scenarios?.items.length>0&&(n.scenarios={...n.scenarios,items:n.scenarios.items.map(l=>({...l,cells:l.cells.map(e=>{const u=L(e.r),a=u&&F(u.row,u.col,r);return a?{...e,r:B(a[0],a[1])}:null}).filter(Boolean)}))},n.scenariosChanged=!0),n.threads?.length>0&&(n.threads=n.threads.map(l=>{const e=L(l.ref),u=e&&F(e.row,e.col,r);return u?{...l,ref:B(u[0],u[1])}:null}).filter(Boolean),n.threadsChanged=!0),n.outlineColumn){const l=n.outlineColumn;if(r.axis==="col"&&!r.span){const e=U(0,l.col,r);n.outlineColumn=e?{...l,col:e[1]}:null}else if(r.axis==="row"&&!r.span&&l.checks){const e={};for(const u of Object.keys(l.checks)){const a=U(Number(u),0,r);a&&(e[a[0]]=l.checks[u])}n.outlineColumn={...l,checks:e}}}if(n.outlineVersion=(n.outlineVersion??0)+1,n.sortState&&(n.sortState=nn(n.sortState,r),n.filterChanged=!0),n.autoFilter&&(n.autoFilter=h(n.autoFilter,r),n.filterChanged=!0),n.advancedFilter){const l=n.advancedFilter,e=l.listSheet===n?E(l,r):l;n.advancedFilter=e===null?null:{...l,r0:e.r0,c0:e.c0,r1:e.r1,c1:e.c1,criteria:l.criteria&&l.criteriaSheet===n?E(l.criteria,r):l.criteria,extract:l.extract?E(l.extract,r):null},n.filterChanged=!0}if(n.filteredRows&&r.axis==="row"&&!r.span){const l=new Set;for(const e of n.filteredRows){const u=U(e,0,r);u&&l.add(u[0])}n.filteredRows=l}if(t.engine)for(const l of[...t.engine.tables.values()]){if(l.sheet!==n)continue;const e=E(l,r);if(e===null)continue;const u=!r.span||r.span[0]<=l.r0&&r.span[1]>=l.r1;r.axis==="col"&&u&&e.c1-e.c0!==l.c1-l.c0&&Cn(t,l,r),Object.assign(l,{r0:e.r0,c0:e.c0,r1:e.r1,c1:e.c1}),l.filter=h(l.filter,r),l.dirty=!0}const c=l=>()=>{const e=I(t);return rn(l),c(e)};return c(s)}function On(t){const n=r=>()=>{const s=I(t);return rn(r),n(s)};return n(I(t))}function xn(t,n){return b(t,n,$(t,n))}function yn(t,n,r,s){if(t===void 0&&n===void 0)return;const o=t===void 0?[]:t.slice();for(let c=r>>>v;c<=s>>>v;c++){const l=t?.[c],e=n?.[c];if(l===void 0&&e===void 0)continue;const u=c<<v,a=Math.max(r,u)-u,i=Math.min(s,u+A)-u+1,m=l===void 0?V({styles:null}):{kinds:l.kinds.slice(),values:l.values.slice(),styles:l.styles?l.styles.slice():null};e===void 0?(m.kinds.fill(0,a,i),m.values.fill(0,a,i),m.styles!==null&&m.styles.fill(0,a,i)):(m.kinds.set(e.kinds.subarray(a,i),a),m.values.set(e.values.subarray(a,i),a),e.styles!==null?(m.styles===null&&(m.styles=new Uint16Array(q)),m.styles.set(e.styles.subarray(a,i),a)):m.styles!==null&&m.styles.fill(0,a,i)),o[c]=m}return o}function wn(t){if(t===void 0)return-1;for(let n=t.length-1;n>=0;n--){const r=t[n];if(r!==void 0){for(let s=A;s>=0;s--)if(r.kinds[s]!==0||r.styles!==null&&r.styles[s]!==0)return(n<<v)+s}}return-1}function An(t,n,r){t.collab?.structure(n,r);const{at:s,n:o}=r,[c,l]=r.span,e=r.axis==="row",u=xn(t,n),a=n.columns.slice();if(e)for(let f=c;f<=Math.min(l,n.columns.length-1);f++)n.columns[f]!==void 0&&(a[f]=G(n.columns[f],s,o),o>0&&(n.maxRow=Math.min(k-1,Math.max(n.maxRow,wn(a[f])))));else{const f=n.columns.length-1,x=o>0?Math.min(f+o,w-1):f;for(let g=s;g<=x;g++){const C=g-o,S=o>0&&g<s+o?void 0:n.columns[C],M=yn(n.columns[g],S,c,l);M!==void 0&&(a[g]=M)}o>0&&(n.maxCol=Math.min(w-1,Math.max(n.maxCol,Math.min(n.maxCol+o,x))))}if(n.columns=a,o>0&&s>0&&!r.noFormat){const f=Math.min(o,(e?k:w)-s),x=Math.min(l,e?n.maxCol:n.maxRow);if(f>0&&f*(x-c+1)<=1<<22)for(let g=c;g<=x;g++){const C=e?n.cellStyle(s-1,g):n.cellStyle(g,s-1);if(C!==0)for(let S=0;S<f;S++)e?n.setStyle(s+S,g,C):n.setStyle(g,s+S,C)}}const i=f=>e?f.c0>=c&&f.c1<=l:f.r0>=c&&f.r1<=l;if(n.merges=n.merges.map(f=>i(f)?O(f,r):f).filter(f=>f!==null&&(f.r0!==f.r1||f.c0!==f.c1)),n.mergeIndex=null,n.spills){const f=new Map;for(const[x,g]of n.spills){const C=Math.floor(x/w),S=F(C,x-C*w,r),M=i(g)?O(g,r):g;S&&M&&f.set(S[0]*w+S[1],{...g,...M})}n.spills=f}if(n.valueMeta){const f=new Map;for(const[x,g]of n.valueMeta){const C=Math.floor(x/w),S=F(C,x-C*w,r);S&&f.set(S[0]*w+S[1],g)}n.valueMeta=f}const m=n.name.toUpperCase();for(const f of t.sheets){const x=f===n;if(!x&&!Q(f.formulas,m))continue;const g=new D;f.formulas.forEachCell((C,S,M)=>{const z=x?F(C,S,r):[C,S];if(z===null)return;const K=x||P(M,m)?J(M,C,S):null;K===null||!x&&!K.toUpperCase().includes(m)?g.put(z[0],z[1],M):g.put(z[0],z[1],Y(M,K,r,x,n.name))}),f.formulas=g}const y=tn(t,n,r),d=cn(n,r),p=t.engine;if(p){for(const f of p.names.values())f.text=T(f.text,r,!1,n.name);p.names.size>0&&(t.namesChanged=!0),p.nameTemplates.clear(),p.rebuild()}n.viewChanged=!0,n.revision++,t.collab?.structureDone(n);const R=(f,x,g)=>()=>R(f(n),x(n),g(n));return R(d,y,u)}function Cn(t,n,r){const s=r.at-n.c0,o=n.columns.slice(),c=n.totals?.slice()??null;if(r.n>0){const l=new Set(o.map(u=>u.toLowerCase())),e=[];for(let u=0,a=1;u<r.n;u++){for(;l.has(`column${a}`);)a++;l.add(`column${a}`),e.push(`Column${a}`)}o.splice(s,0,...e),c?.splice(s,0,...e.map(()=>null)),n.headerRows>0&&e.forEach((u,a)=>n.sheet.setCell(n.r0,r.at+a,sn,t.strings.add(u)))}else{const l=Math.max(s,0),e=Math.min(-r.n-Math.max(0,n.c0-r.at),o.length-l);o.splice(l,e),c?.splice(l,e)}n.columns=o,n.totals=c,n.columnsChanged=!0}function $(t,n){const r=t.engine;return{columns:n.columns,rows:n.rows,cols:n.cols,rowStyles:n.rowStyles,colStyles:n.colStyles,rowExtras:n.rowExtras,colExtras:n.colExtras,valueMeta:n.valueMeta,merges:n.merges,spills:n.spills,maxRow:n.maxRow,maxCol:n.maxCol,frozenRows:n.frozenRows,frozenCols:n.frozenCols,colsChanged:n.colsChanged,structureOps:n.structureOps,pageSetup:n.pageSetup?en(n.pageSetup):null,formulas:t.sheets.map(s=>[s,s.formulas]),names:r?[...r.names].map(([s,o])=>[s,{...o}]):null}}function b(t,n,r){return()=>{const s=$(t,n);n.columns=r.columns,n.rows=r.rows,n.cols=r.cols,n.rowStyles=r.rowStyles,n.colStyles=r.colStyles,n.rowExtras=r.rowExtras,n.colExtras=r.colExtras,n.valueMeta=r.valueMeta,n.merges=r.merges,n.mergeIndex=null,n.spills=r.spills,n.maxRow=r.maxRow,n.maxCol=r.maxCol,n.frozenRows=r.frozenRows,n.frozenCols=r.frozenCols,n.colsChanged=r.colsChanged,n.structureOps=r.structureOps,r.pageSetup&&(n.pageSetup=r.pageSetup);for(const[c,l]of r.formulas)c.formulas=l;const o=t.engine;return o&&(o.names=new Map(r.names),o.nameTemplates.clear(),o.rebuild()),n.revision++,b(t,n,s)}}export{ln as FORMULA,I as captureFeatures,On as featuresUndo,rn as restoreFeatures,An as shiftBand,tn as shiftFeatures,Un as shiftStructure,xn as structureUndo};
package/core/styles.js CHANGED
@@ -1 +1 @@
1
- const u='"Aptos Narrow","Calibri","Carlito","Arial Narrow","Helvetica Neue",Arial,sans-serif',h="Aptos Narrow",g=12;function d(){return{fontName:h,fontSize:12,bold:!1,italic:!1,underline:0,strike:!1,color:null,fill:null,borders:null,numFmt:"General",hAlign:null,vAlign:null,wrap:!1,indent:0,rotation:0,shrink:!1}}const x=["pattern","gradient","quotePrefix","vertAlign","namedStyle","unlocked","hiddenFormula","readingOrder","justifyLastLine"];function c(r){return JSON.stringify(r,(e,i)=>e[0]==="_"?void 0:i)}class A{constructor(){this.list=[d()],this.index=null,this.sourceCount=0,this.deriveCache=new Map,this.hasButtons=!1,this.hasExt=!1}get(e){return this.list[e]??this.list[0]}load(e){this.list=e.length?e:[d()],this.sourceCount=e.length,this.index=null,this.hasExt=!1,this.hasButtons=!1,this.deriveCache.clear()}intern(e){if(this.index===null){this.index=new Map;for(let n=this.list.length-1;n>=0;n--)this.index.set(c(this.list[n]),n)}const i=c(e);let l=this.index.get(i);if(l===void 0){if(l=this.list.length,l>=65535)throw new Error("Too many cell formats");this.list.push(e),this.index.set(i,l)}return l}derive(e,i,l=JSON.stringify(i)){let n=this.deriveCache.get(l);n===void 0&&(n=new Map,this.deriveCache.set(l,n));let o=n.get(e);if(o===void 0){const f=this.get(e),s={...f,...i};for(const t of Object.keys(s))t[0]==="_"&&delete s[t];if(i.ext!==void 0){const t={...f.ext??{},...i.ext??{}};for(const a of Object.keys(t))t[a]===null&&delete t[a];Object.keys(t).length>0?(s.ext=t,t.buttons!==void 0&&(this.hasButtons=!0),this.hasExt=!0):delete s.ext}for(const t of x)(s[t]===null||s[t]===void 0||s[t]===!1)&&delete s[t];o=this.intern(s),n.set(e,o)}return o}}function b(r,e){const i=r.fontName===h?u:`"${r.fontName}",${u}`;return`${r.italic?"italic ":""}${r.bold?"bold ":""}${e}px ${i}`}export{h as DEFAULT_FONT,g as DEFAULT_SIZE,u as FONT_FALLBACK,A as StyleTable,b as cssFont,d as defaultStyle};
1
+ const d='"Aptos Narrow","Calibri","Carlito","Arial Narrow","Helvetica Neue",Arial,sans-serif',u="Aptos Narrow",m=12;function c(){return{fontName:u,fontSize:12,bold:!1,italic:!1,underline:0,strike:!1,color:null,fill:null,borders:null,numFmt:"General",hAlign:null,vAlign:null,wrap:!1,indent:0,rotation:0,shrink:!1}}const g=["pattern","gradient","quotePrefix","vertAlign","namedStyle","unlocked","hiddenFormula","readingOrder","justifyLastLine"];function x(f){return JSON.stringify(f,(t,i)=>t[0]==="_"?void 0:i)}class b{constructor(){this.list=[c()],this.index=null,this.sourceCount=0,this.deriveCache=new Map,this.hasButtons=!1,this.hasExt=!1}get(t){return this.list[t]??this.list[0]}explicitDefault(){if(this.list[this.defaultCellId]!==this.list[0]){if(this.list.length>=65535)throw new Error("Too many cell formats");this.defaultCellId=this.list.length,this.list.push(this.list[0])}return this.defaultCellId}load(t){this.list=t.length?t:[c()],this.sourceCount=t.length,this.index=null,this.hasExt=!1,this.hasButtons=!1,this.deriveCache.clear()}intern(t){if(this.index===null){this.index=new Map;for(let n=this.list.length-1;n>=0;n--)this.index.set(x(this.list[n]),n)}const i=x(t);let l=this.index.get(i);if(l===void 0){if(l=this.list.length,l>=65535)throw new Error("Too many cell formats");this.list.push(t),this.index.set(i,l)}return l}derive(t,i,l=JSON.stringify(i)){let n=this.deriveCache.get(l);n===void 0&&(n=new Map,this.deriveCache.set(l,n));let h=n.get(t);if(h===void 0){const a=this.get(t),s={...a,...i};for(const e of Object.keys(s))e[0]==="_"&&delete s[e];if(i.ext!==void 0){const e={...a.ext??{},...i.ext??{}};for(const r of Object.keys(e))e[r]===null&&delete e[r];Object.keys(e).length>0?(s.ext=e,e.buttons!==void 0&&(this.hasButtons=!0),this.hasExt=!0):delete s.ext}if(a.themeRefs||i.themeRefs){const e={...a.themeRefs};for(const[r,o]of Object.entries(i.themeRefs??{}))o!==void 0&&(e[r]=o);for(const[r,o]of[["fill","fill"],["color","color"],["fontName","font"],["pattern","patternFg"]])r in i&&!i.themeRefs?.[o]&&delete e[o];Object.keys(e).length?s.themeRefs=e:delete s.themeRefs}for(const e of g)(s[e]===null||s[e]===void 0||s[e]===!1)&&delete s[e];h=this.intern(s),n.set(t,h)}return h}}function k(f,t){const i=f.fontName===u?d:`"${f.fontName}",${d}`;return`${f.italic?"italic ":""}${f.bold?"bold ":""}${t}px ${i}`}export{u as DEFAULT_FONT,m as DEFAULT_SIZE,d as FONT_FALLBACK,b as StyleTable,k as cssFont,c as defaultStyle};
package/core/tables.js CHANGED
@@ -1,2 +1,2 @@
1
- import{patchTableColumns as Y,totalsChildXml as N}from"../slicers/table-file.js";import{colName as q,MAX_COLS as G,rangeRef as F,sheetPrefix as J}from"./address.js";import{columnStem as L,freeColumnName as Q,newColumnName as V,nextName as k,tableStem as ee,totalLabel as te}from"./default-names.js";import{FORMULA as D,KIND_MASK as M,STR as T}from"./sheet.js";import{translateFormula as ne}from"./formula-refs.js";import{autoFilterXml as re,sortStateXml as se}from"../cells/filter-file.js";import{PRESET_DXFS as le,PRESET_STYLES as B}from"./table-presets.js";import{resolveColor as oe}from"../io/xlsx-styles.js";import{elementTableColors as ae}from"../slicers/custom-styles.js";import{undoGroup as ce}from"../cells/undo-group.js";import{escapeMarkup as v,xmlAttr as _}from"../io/xml-escape.js";const ie=["FFFFFF","000000","E8E8E8","0E2841","156082","E97132","196B24","0F9ED5","A02B93","4EA72E"],K=new Set;function Oe(t){K.add(t)}function ue(t,e){const n=B[t];if(!n)return null;const s=e&&e.length>=10?e:ie,r=l=>typeof l=="string"?`#${l}`:oe({a:{theme:String(l[0]),tint:l[1]?String(l[1]):void 0}},s),o={},a={};for(const[l,u,f]of n){const c=le[u],i={};c.bold&&(i.bold=!0),c.italic&&(i.italic=!0),c.c&&(i.color=r(c.c)),c.f&&(i.fill=r(c.f));for(const m of["l","r","t","b","v","h"])c[m]&&(i.borders??={},i.borders[m]={style:c[m][0],color:r(c[m][1])});o[l]=i,f&&(a[l]=f)}return{el:o,sizes:a}}const P=new WeakMap,fe={};function me(t,e){if(t&&B[t]&&t.startsWith("TableStyle")){const n=e&&e.length>=10?e:fe;let s=P.get(n);s||P.set(n,s=new Map);let r=s.get(t);if(!r){const{el:o,sizes:a}=ue(t,e),l=u=>a[u]??1;r=ae(o,[l("firstRowStripe"),l("secondRowStripe")],[l("firstColumnStripe"),l("secondColumnStripe")]),s.set(t,r)}return r}for(const n of K){const s=n(t,e);if(s)return s}return null}function Ue(t,e){const n=t.r0+t.headerRows,s=t.sheet?.rows;if(!s||s.hiddenSizes===null||s.hiddenSizes.size===0)return e-n;let r=t._bandRows;if(!r||r.version!==s.version||r.top!==n||r.r1!==t.r1||r.rows!==s){const l=[];for(const u of s.hiddenSizes.keys())u>=n&&u<=t.r1&&l.push(u);l.sort((u,f)=>u-f),r={version:s.version,top:n,r1:t.r1,rows:s,list:l},t._bandRows=r}let o=0,a=r.list.length;for(;o<a;){const l=o+a>>1;r.list[l]<e?o=l+1:a=l}return e-n-o}function de(t){const e=t.engine;return k(ee(),n=>e.tables.has(n.toUpperCase()))}function ze(t,e,n,s,r="TableStyleMedium2"){const o=t.engine,a=[];let l={...n};return t.transact("table",u=>{if(!s){l.r0>0&&!I(e,l.r0-1,l.c0,l.c1)&&(l={...l,r0:l.r0-1});for(let i=l.c0;i<=l.c1;i++)t.setInput(u,l.r0,i,`${L()}${i-l.c0+1}`)}const f=new Set;for(let i=l.c0;i<=l.c1;i++){let m=t.editText(e,l.r0,i).trim()||`${L()}${i-l.c0+1}`,d=m;for(let p=2;f.has(d.toLowerCase());p++)d=`${m}${p}`;f.add(d.toLowerCase()),a.push(d),(d!==m||(e.kind(l.r0,i)&M)!==T)&&(m=d,u.saveCell(l.r0,i),e.setCell(l.r0,i,T,t.strings.add(d)))}const c={name:de(t),sheet:e,...l,headerRows:1,totalsRows:0,columns:a,styleName:r,showRowStripes:!0,showColumnStripes:!1,showFirstColumn:!1,showLastColumn:!1,filter:{r0:l.r0,c0:l.c0,r1:l.r1,c1:l.c1,columns:new Map},isNew:!0,dirty:!0};return o.tables.set(c.name.toUpperCase(),c),()=>(o.tables.delete(c.name.toUpperCase()),o.rebuild(),()=>(o.tables.set(c.name.toUpperCase(),c),o.rebuild(),()=>null))}),o.tables.get(`${[...o.tables.keys()].pop()}`)}function I(t,e,n,s){for(let r=n;r<=s;r++)if(t.kind(e,r)!==0)return!0;return!1}function g(t,e){const n=[];if(t.engine)for(const s of t.engine.tables.values())s.sheet===e&&n.push(s);return n}function Xe(t,e,n,s){for(const r of g(t,e)){const o=n===r.r1+1-0&&r.totalsRows===0&&s>=r.c0&&s<=r.c1,a=s===r.c1+1&&n>=r.r0&&n<=r.r1;if(o)return r.r1=n,r.filter&&(r.filter.r1=n),r.dirty=!0,n-1>=r.r0+r.headerRows&&t.transact("table-row",l=>{for(let u=r.c0;u<=r.c1;u++)u===s||(e.kind(n-1,u)&D)===0||e.kind(n,u)!==0||t.setInput(l,n,u,`=${ne(t.editText(e,n-1,u).slice(1),1,0)}`)}),t.engine.rebuild(),r;if(a){r.c1=s;const l=t.editText(e,r.r0,s).trim()||V(new Set(r.columns.map(u=>u.toLowerCase())),r.columns);return r.columns.push(l),r.filter&&(r.filter.c1=s),e.kind(r.r0,s)===0&&e.setCell(r.r0,s,T,t.strings.add(l)),r.dirty=!0,r.columnsChanged=!0,t.engine.rebuild(),r}}return null}function Ne(t,e,n){for(const s of g(t,e)){if(n.r0!==s.r1+1||s.totalsRows!==0||n.c0<s.c0||n.c0>s.c1)continue;const r=n.r1;if(g(t,e).some(a=>a!==s&&a.r0<=r&&a.r1>=n.r0&&a.c0<=s.c1&&a.c1>=s.c0))return null;const o={r1:s.r1,filter:s.filter?{...s.filter}:null};return t.transact("table-expand",()=>{const a=f=>{s.r1=f.r1,s.filter=f.filter?{...f.filter}:null,s.dirty=!0,t.engine.rebuild()},l={r1:r,filter:s.filter?{...s.filter,r1:r}:null};a(l);const u=(f,c)=>()=>(a(f),u(c,f));return u(o,l)}),s}return null}function De(t,e,n,s,r,o){if(!r.startsWith("="))return!1;for(const a of g(t,e)){if(s<a.c0||s>a.c1||n<=a.r0||n>a.r1-a.totalsRows)continue;const l=a.r0+a.headerRows,u=a.r1-a.totalsRows;for(let f=l;f<=u;f++)if(f!==n&&e.kind(f,s)!==0)return!1;return u===l?!1:(t.transact("calculated-column",f=>{for(let c=l;c<=u;c++)c!==n&&t.setInput(f,c,s,`=${o(r.slice(1),c-n,0)}`)}),!0)}return!1}const Z=(t,e)=>{const n=t.totals?.[e];return n?n.label!==void 0?` totalsRowLabel="${_(n.label)}"`:` totalsRowFunction="${n.fn}"`:""},j=t=>`<tableStyleInfo name="${_(t.styleName??"TableStyleMedium2")}" showFirstColumn="${t.showFirstColumn?1:0}" showLastColumn="${t.showLastColumn?1:0}" showRowStripes="${t.showRowStripes===!1?0:1}" showColumnStripes="${t.showColumnStripes?1:0}"/>`,pe=t=>F({r0:t.r0,c0:t.c0,r1:t.r1-t.totalsRows,c1:t.c1}),H=t=>t.filter&&t.headerRows>0?re({...t.filter,r0:t.r0,c0:t.c0,r1:t.r1-t.totalsRows,c1:t.c1},!1)+se(t.filter.sortState):"",W=t=>`${t.headerRows===0?' headerRowCount="0"':""}${t.totalsRows?` totalsRowCount="${t.totalsRows}"`:' totalsRowShown="0"'}`;function Be(t,e){const n=t.columns.map((r,o)=>{const a=N(t,o),l=`<tableColumn id="${t.columnIds?.[o]??o+1}" name="${_(r)}"${Z(t,o)}`;return a?`${l}>${a}</tableColumn>`:`${l}/>`}).join(""),s=H(t);return`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
- <table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" id="${e}" name="${v(t.name)}" displayName="${v(t.name)}" ref="${F(t)}"${W(t)}>${s}<tableColumns count="${t.columns.length}">${n}</tableColumns>${j(t)}</table>`}function Ke(t){return{name:t.name,ref:F(t),filterRef:t.filter&&t.headerRows>0?pe(t):null,filterXml:H(t),counts:W(t),styleInfo:j(t),totals:t.totals?t.columns.map((e,n)=>Z(t,n)):null,totalsChild:t.totals?t.columns.map((e,n)=>N(t,n)):null}}function Pe(t,e){let n=t.replace(/<table\b[^>]*>/,s=>s.replace(/\bname="[^"]*"/,`name="${v(e.name)}"`).replace(/\bdisplayName="[^"]*"/,`displayName="${v(e.name)}"`).replace(/\bref="[^"]*"/,`ref="${e.ref}"`).replace(/\s(?:headerRowCount|totalsRowCount|totalsRowShown)="[^"]*"/g,"").replace(/\s*(\/?)>$/,`${e.counts}$1>`));return n=n.replace(/<autoFilter\b[^>]*\/>|<autoFilter\b[\s\S]*?<\/autoFilter>/,"").replace(/<sortState\b[^>]*\/>|<sortState\b[\s\S]*?<\/sortState>/,""),e.filterXml&&(n=n.replace("<tableColumns",`${e.filterXml}<tableColumns`)),/<tableStyleInfo\b/.test(n)?n=n.replace(/<tableStyleInfo\b[^>]*\/>/,e.styleInfo):n=n.replace("</tableColumns>",`</tableColumns>${e.styleInfo}`),e.totals&&(n=Y(n,e)),n}const he=/[[\]#']/g,E=t=>t.replace(he,"'$&"),we=/^[A-Za-z_\\][A-Za-z0-9_.\\]*$/;function O(t,e){const n=[];for(const s of t.sheets){const r=new Map;for(const[o,a]of s.formulas){const l=a.f??a.sf;if(typeof l!="string")continue;const u=a.f===void 0&&a.base!==void 0;let f=u?r.get(a):void 0;if(f===void 0){const c=e(l,s,Math.floor((u?a.base:o)/G));f=c===l?null:a.f!==void 0?{...a,f:c,tpl:void 0}:{...a,sf:c,tpl:void 0},u&&r.set(a,f)}f!==null&&n.push([s.formulas,o,a,f])}}for(const[s,r]of t.engine.names){const o=e(r.text,null,0);o!==r.text&&n.push([t.engine.names,s,r,{...r,text:o}])}for(const s of t.engine.tables.values())s.totals?.forEach((r,o)=>{if(r?.fn!=="custom"||!r.formula)return;const a=e(r.formula,s.sheet,s.r1);a!==r.formula&&n.push([{set:(l,u)=>{s.totals[l]=u}},o,r,{...r,formula:a}])});return t.formulasEdited=!0,n.some(s=>s[0]===t.engine.names)&&(t.namesChanged=!0),y(n,3),n}const y=(t,e)=>{for(const n of t)n[0].set(n[1],n[e])};function C(t,e,n,s,{rebuild:r=!1}={}){const o=t.engine;t.transact(n,a=>{const l={...e},u=s(a),f={...e};e.dirty=!0,r&&o.rebuild();const c=(i,m,d)=>()=>(Object.assign(e,i),e.dirty=!0,d?.(),r&&o.rebuild(),c(m,i,d));return c(l,f,u)})}function Ze(t,e,n,s){C(t,e,"table-option",()=>{e[n]=s})}function je(t,e,n){n&&e.headerRows===0||C(t,e,"table-filter",()=>{e.filter=n?{r0:e.r0,c0:e.c0,r1:e.r1-e.totalsRows,c1:e.c1,columns:new Map}:null})}function He(t,e,n){const s=t.engine;if(!we.test(n)||/^[A-Za-z]{1,3}[0-9]+$/.test(n)||/^[Rr][Cc]?$|^[Cc]$/.test(n))return"That isn't a valid table name.";const r=n.toUpperCase();if(r===e.name.toUpperCase()&&n===e.name)return null;if(s.tables.has(r)&&s.tables.get(r)!==e)return"Another table already uses that name.";if(s.names.has(r))return"A defined name already uses that name.";const o=e.name;return t.transact("table-name",()=>{const a=new RegExp(`(^|[^A-Za-z0-9_.\\\\])${o.replace(/[.\\]/g,"\\$&")}(?=\\[)`,"gi"),l=O(t,i=>i.replace(a,`$1${n}`)),u=(i,m,d)=>{s.tables.delete(i.toUpperCase()),e.name=m,s.tables.set(m.toUpperCase(),e),e.dirty=!0,y(l,d),s.rebuild()};u(o,n,3);const f=()=>(u(o,n,3),c),c=()=>(u(n,o,2),f);return c}),null}const be={average:101,countNums:102,count:103,max:104,min:105,stdDev:107,sum:109,var:110};function $e(t,e,n){if(!n)return null;if(n.label!==void 0)return n.label;if(n.fn==="custom")return n.formula?`=${n.formula}`:null;const s=be[n.fn];return s?`=SUBTOTAL(${s},${t.name}[${E(t.columns[e])}])`:null}function We(t,e,n){const{sheet:s}=e;if(n===e.totalsRows>0)return null;if(n){const o=e.r1+1;return I(s,o,e.c0,e.c1)?"Clear the row under the table to add a total row.":(C(t,e,"table-totals",a=>{const l=e.c1-e.c0,u=e.totals?.some(Boolean)?e.columns.map((c,i)=>e.totals[i]??null):null;if(u){e.totals=u,u.forEach((c,i)=>{const m=$e(e,i,c);m&&t.setInput(a,o,e.c0+i,m)}),e.totalsRows=1,e.r1=o,e.columnsChanged=e.source!==!0;return}e.totals=e.columns.map(()=>null);const f=te();if(e.totals[0]={label:f},t.setInput(a,o,e.c0,f),l>0){let c=!1;for(let m=e.r0+e.headerRows;m<=e.r1&&!c;m++)c=(s.kind(m,e.c1)&M)===1;const i=c?109:103;e.totals[l]={fn:c?"sum":"count"},t.setInput(a,o,e.c1,`=SUBTOTAL(${i},${e.name}[${E(e.columns[l])}])`)}e.totalsRows=1,e.r1=o,e.columnsChanged=e.source!==!0},{rebuild:!0}),null)}const r=e.r1;return C(t,e,"table-totals",o=>{for(let a=e.c0;a<=e.c1;a++)t.setInput(o,r,a,"");e.totalsRows=0,e.r1=r-1},{rebuild:!0}),null}function Ye(t,e,n){if(n===e.headerRows>0)return null;const{sheet:s}=e;return n?e.r0===0||I(s,e.r0-1,e.c0,e.c1)?"Clear the row above the table to show its headers.":(C(t,e,"table-header",r=>{e.r0-=1,e.headerRows=1,e.columns.forEach((o,a)=>t.setInput(r,e.r0,e.c0+a,o)),e.filter={r0:e.r0,c0:e.c0,r1:e.r1-e.totalsRows,c1:e.c1,columns:new Map}},{rebuild:!0}),null):(C(t,e,"table-header",r=>{for(let o=e.c0;o<=e.c1;o++)t.setInput(r,e.r0,o,"");e.r0+=1,e.headerRows=0,e.filter=null},{rebuild:!0}),null)}function qe(t,e,n){const{sheet:s}=e;if(n.r0!==e.r0)return"The header row must stay in the same row.";if(n.c0!==e.c0)return"The table must keep its first column.";if(n.r1<n.r0+e.headerRows+e.totalsRows)return"The table needs at least one data row.";for(const r of g(t,s))if(r!==e&&r.r0<=n.r1&&r.r1>=n.r0&&r.c0<=n.c1&&r.c1>=n.c0)return"A table can't overlap another table.";return C(t,e,"table-resize",()=>{const r=n.c1-n.c0+1,o=e.columns.slice(0,r);for(let a=o.length;a<r;a++){const l=e.c0+a;let u=e.headerRows?t.editText(s,e.r0,l).trim():"";u||(u=`${L()}${a+1}`),o.push(u)}e.columns=o,e.r1=n.r1,e.c1=n.c1,e.filter&&(e.filter={r0:e.r0,c0:e.c0,r1:e.r1-e.totalsRows,c1:e.c1,columns:new Map}),e.columnsChanged=!0},{rebuild:!0}),null}function Ce(t,e,n){const{sheet:s}=e,r=e.r1-e.r0+1,o=e.c1-e.c0+1,a=new Map,l=(f,c,i,m)=>{const d=(f-e.r0)*o+(c-e.c0);let p=a.get(d);p||a.set(d,p={}),p[i]=m};for(const f of n.areas(e,e.r0,e.r1,e.c0,e.c1)){const c=f.borders;for(let i=Math.max(f.r0,e.r0);i<=Math.min(f.r1,e.r1);i++)for(let m=Math.max(f.c0,e.c0);m<=Math.min(f.c1,e.c1);m++)c.t&&i===f.r0&&l(i,m,"t",c.t),c.b&&i===f.r1&&l(i,m,"b",c.b),c.l&&m===f.c0&&l(i,m,"l",c.l),c.r&&m===f.c1&&l(i,m,"r",c.r),c.h&&i<f.r1&&l(i,m,"b",c.h),c.v&&m<f.c1&&l(i,m,"r",c.v)}const u=new Map;t.transact("format",f=>{for(let c=0;c<r;c++){const i=e.r0+c;for(let m=0;m<o;m++){const d=e.c0+m,p=n.format(e,i,d),h=a.get(c*o+m),w=s.style(i,d),$=t.styles.get(w),b={};if(p.fill&&!$.fill&&(b.fill=p.fill),p.color&&!$.color&&(b.color=p.color),p.bold&&!$.bold&&(b.bold=!0),p.italic&&!$.italic&&(b.italic=!0),h){const R=$.borders??{},z={l:R.l??null,r:R.r??null,t:R.t??null,b:R.b??null};let X=!1;for(const S of["l","r","t","b"])h[S]&&!R[S]&&(z[S]={style:h[S].style,color:h[S].color},X=!0);X&&(b.borders=z)}const A=JSON.stringify(b);if(A==="{}")continue;const U=`${w}|${A}`;let x=u.get(U);x===void 0&&u.set(U,x=t.styles.derive(w,b,A)),x!==w&&(f.saveCell(i,d),s.setStyle(i,d,x))}}})}function Ge(t,e){ce(t,"Convert to Range",()=>ge(t,e))}function ge(t,e){const n=t.engine,{sheet:s}=e,r=me(e.styleName,t.theme),o=t.activeSheet;t.activeSheet=t.sheets.indexOf(s),r?.format&&Ce(t,e,r),t.activeSheet=o;const a=e.name.toUpperCase(),l=new RegExp(`(^|[^A-Za-z0-9_.\\\\])${e.name.replace(/[.\\]/g,"\\$&")}(\\[(?:[^\\[\\]']|'.|\\[(?:[^\\[\\]']|'.)*\\])*\\])`,"gi");t.transact("table-convert",()=>{const u=O(t,(m,d,p)=>m.replace(l,(h,w,$)=>{const b=n.tableSpecToA1(e,$,p);return b===null?h:`${w}${d===s?"":J(s.name)}${b}`})),f=e.source===!0&&e.part?{part:e.part,sheetIndex:t.sheets.indexOf(s)}:null,c=()=>{n.tables.delete(a),f&&(t.removedTables??=[]).push(f),y(u,3),n.rebuild()},i=()=>(n.tables.set(a,e),f&&(t.removedTables=t.removedTables.filter(m=>m!==f)),y(u,2),n.rebuild(),()=>(c(),i));return c(),i})}function Re(t,e,n,s){const r=t.toLowerCase(),o=`${e.toLowerCase()}[`;let a="",l=0;for(let u=r.indexOf(o);u>=0;u=r.indexOf(o,u+1)){const f=u+o.length-1;let c=0,i=f;for(;i<t.length;i++){const p=t[i];if(p==="'")i++;else if(p==="[")c++;else if(p==="]"&&--c===0)break}const m=t.slice(f,i+1),d=m.split(`[${n}]`).join(`[${s}]`);d!==m&&(a+=t.slice(l,f)+d,l=i+1)}return l===0?t:a+t.slice(l)}function Se(t,e,n,s){const r=e.columns[n],o=E(r),a=E(s),l=O(t,i=>i.includes("[")?Re(i,e.name,o,a):i),u=(i,m)=>{e.columns=e.columns.map((d,p)=>p===n?i:d),e.columnsChanged=!0,e.dirty=!0,y(l,m),t.engine?.rebuild()};u(s,3);const f=()=>(u(s,3),c),c=()=>(u(r,2),f);return c}function Je(t,e,n){let s=null;const{sheet:r}=e;for(const o of g(t,r))if(!(o.headerRows===0||o.dataSheet))for(let a=0;a<n.count;a++){if(n.rows[a]!==o.r0||n.cols[a]<o.c0||n.cols[a]>o.c1)continue;const l=n.cols[a],u=l-o.c0,f=r.kind(o.r0,l);let c=f===0?"":String(t.engine.read(r,o.r0,l)??"").trim();const i=new Set(o.columns.filter((h,w)=>w!==u).map(h=>h.toLowerCase()));if(c==="")c=Q(i);else if(i.has(c.toLowerCase())){const h=c;let w=2;for(;i.has(`${h}${w}`.toLowerCase());)w++;c=`${h}${w}`}if(((f&M)!==T||(f&D)!==0||t.strings.get(r.value(o.r0,l))!==c)&&(e.saveCell(o.r0,l),r.setFormula(o.r0,l,null),r.setCell(o.r0,l,T,t.strings.add(c))),o.columns[u]===c)continue;const m=Se(t,o,u,c),d=s,p=(h,w)=>w?()=>p(h(),w()):h;s=p(m,d)}return s}const Qe=[...Array.from({length:21},(t,e)=>`TableStyleLight${e+1}`),...Array.from({length:28},(t,e)=>`TableStyleMedium${e+1}`),...Array.from({length:11},(t,e)=>`TableStyleDark${e+1}`)];export{be as SUBTOTAL_CODE,Qe as TABLE_STYLE_NAMES,Ue as bandRow,q as colName,Ge as convertTableToRange,ze as createTable,De as fillCalculatedColumn,Ne as growTableOver,Xe as maybeExpandTable,de as nextTableName,Pe as patchTableXml,ue as presetElements,He as renameTable,Se as renameTableColumn,qe as resizeTable,Oe as setCustomTableResolver,je as setTableFilterButton,Ye as setTableHeaderRow,Ze as setTableOption,We as setTableTotals,E as structuredColumn,Je as syncTableHeaders,me as tableColors,Ke as tablePatch,Be as tableXml,g as tablesOf,$e as totalInput};
1
+ import{formulaEntryLimit as H}from"../calc/parser.js";import{patchTableColumns as q,totalsChildXml as B}from"../slicers/table-file.js";import{allUndo as J,cowTx as Q,rectsTouch as A}from"../cells/block.js";import{checkBandShift as V,MSG_OFF as k}from"../cells/shift-check.js";import{unmergeCells as ee}from"./merge.js";import{shiftBand as te}from"./structure.js";import{colName as re,MAX_COLS as ne,MAX_ROWS as se,rangeRef as L,sheetPrefix as le}from"./address.js";import{columnStem as M,freeColumnName as oe,newColumnName as ae,nextName as ce,tableStem as ie,totalLabel as ue}from"./default-names.js";import{FORMULA as D,KIND_MASK as _,STR as v}from"./sheet.js";import{translateFormula as fe}from"./formula-refs.js";import{autoFilterXml as me,sortStateXml as de}from"../cells/filter-file.js";import{PRESET_DXFS as pe,PRESET_STYLES as K}from"./table-presets.js";import{resolveColor as he}from"../io/xlsx-styles.js";import{elementTableColors as we}from"../slicers/custom-styles.js";import{undoGroup as be}from"../cells/undo-group.js";import{escapeMarkup as F,xmlAttr as I}from"../io/xml-escape.js";const Ce=["FFFFFF","000000","E8E8E8","0E2841","156082","E97132","196B24","0F9ED5","A02B93","4EA72E"],P=new Set;function qe(t){P.add(t)}function ge(t,e){const r=K[t];if(!r)return null;const s=e&&e.length>=10?e:Ce,n=c=>typeof c=="string"?`#${c}`:he({a:{theme:String(c[0]),tint:c[1]?String(c[1]):void 0}},s),a={},o={};for(const[c,l,i]of r){const u=pe[l],f={};u.bold&&(f.bold=!0),u.italic&&(f.italic=!0),u.c&&(f.color=n(u.c)),u.f&&(f.fill=n(u.f));for(const m of["l","r","t","b","v","h"])u[m]&&(f.borders??={},f.borders[m]={style:u[m][0],color:n(u[m][1])});a[c]=f,i&&(o[c]=i)}return{el:a,sizes:o}}const Z=new WeakMap,$e={};function Re(t,e){if(t&&K[t]&&t.startsWith("TableStyle")){const r=e&&e.length>=10?e:$e;let s=Z.get(r);s||Z.set(r,s=new Map);let n=s.get(t);if(!n){const{el:a,sizes:o}=ge(t,e),c=l=>o[l]??1;n=we(a,[c("firstRowStripe"),c("secondRowStripe")],[c("firstColumnStripe"),c("secondColumnStripe")]),s.set(t,n)}return n}for(const r of P){const s=r(t,e);if(s)return s}return null}function Je(t,e){const r=t.r0+t.headerRows,s=t.sheet?.rows;if(!s||s.hiddenSizes===null||s.hiddenSizes.size===0)return e-r;let n=t._bandRows;if(!n||n.version!==s.version||n.top!==r||n.r1!==t.r1||n.rows!==s){const c=[];for(const l of s.hiddenSizes.keys())l>=r&&l<=t.r1&&c.push(l);c.sort((l,i)=>l-i),n={version:s.version,top:r,r1:t.r1,rows:s,list:c},t._bandRows=n}let a=0,o=n.list.length;for(;a<o;){const c=a+o>>1;n.list[c]<e?a=c+1:o=c}return e-r-a}function Se(t){const e=t.engine;return ce(ie(),r=>e.tables.has(r.toUpperCase()))}function Qe(t,e,r,s,n="TableStyleMedium2",a=[]){const o=t.engine,c=[];let l={...r};const i={...l,r0:l.r0-1,r1:l.r0-1},u=S(t,e),f=!s&&l.r0>0&&!O(e,i.r0,l.c0,l.c1)&&!u.some(d=>A(d,i))&&!e.merges.some(d=>A(d,i))&&!a.some(d=>d.anchor?.sheet===e&&d.renderedRange&&A(d.renderedRange,i)),m=!s&&!f;let h=null;return t.transact("table",d=>{if(e.protection)return;if(m){const p=l.r1+1>=se?k:V(t,e,{...l,r1:l.r0},"down",1,a);if(p)throw new Te(p)}const w=[];if(e.merges.some(p=>A(p,l))&&w.push(ee(e,l)),m?(w.push(te(t,e,{axis:"row",at:l.r0,n:1,span:[l.c0,l.c1]})),d=Q(e),l.r1++):f&&l.r0--,!s)for(let p=l.c0;p<=l.c1;p++)t.setInput(d,l.r0,p,`${M()}${p-l.c0+1}`);const b=new Set;for(let p=l.c0;p<=l.c1;p++){let $=t.editText(e,l.r0,p).trim()||`${M()}${p-l.c0+1}`,g=$;for(let R=2;b.has(g.toLowerCase());R++)g=`${$}${R}`;b.add(g.toLowerCase()),c.push(g),(g!==$||(e.kind(l.r0,p)&_)!==v)&&($=g,d.saveCell(l.r0,p),e.setCell(l.r0,p,v,t.strings.add(g)))}h={name:Se(t),sheet:e,...l,headerRows:1,totalsRows:0,columns:c,styleName:n,showRowStripes:!0,showColumnStripes:!1,showFirstColumn:!1,showLastColumn:!1,filter:{r0:l.r0,c0:l.c0,r1:l.r1,c1:l.c1,columns:new Map},isNew:!0,dirty:!0},o.tables.set(h.name.toUpperCase(),h);const C=p=>()=>(p?o.tables.set(h.name.toUpperCase(),h):o.tables.delete(h.name.toUpperCase()),o.rebuild(),C(!p));return J([...w,C(!1)])}),h}class Te extends Error{}function O(t,e,r,s){for(let n=r;n<=s;n++)if(t.kind(e,n)!==0)return!0;return!1}function S(t,e){const r=[];if(t.engine)for(const s of t.engine.tables.values())s.sheet===e&&r.push(s);return r}function Ve(t,e,r,s){for(const n of S(t,e)){const a=r===n.r1+1-0&&n.totalsRows===0&&s>=n.c0&&s<=n.c1,o=s===n.c1+1&&r>=n.r0&&r<=n.r1;if(a)return n.r1=r,n.filter&&(n.filter.r1=r),n.dirty=!0,r-1>=n.r0+n.headerRows&&t.transact("table-row",c=>{for(let l=n.c0;l<=n.c1;l++)l===s||(e.kind(r-1,l)&D)===0||e.kind(r,l)!==0||t.setInput(c,r,l,`=${fe(t.editText(e,r-1,l).slice(1),1,0)}`,{overLimitAsText:!0})}),t.engine.rebuild(),n;if(o){n.c1=s;const c=t.editText(e,n.r0,s).trim()||ae(new Set(n.columns.map(l=>l.toLowerCase())),n.columns);return n.columns.push(c),n.filter&&(n.filter.c1=s),e.kind(n.r0,s)===0&&e.setCell(n.r0,s,v,t.strings.add(c)),n.dirty=!0,n.columnsChanged=!0,t.engine.rebuild(),n}}return null}function ke(t,e,r){for(const s of S(t,e)){if(r.r0!==s.r1+1||s.totalsRows!==0||r.c0<s.c0||r.c0>s.c1)continue;const n=r.r1;if(S(t,e).some(o=>o!==s&&o.r0<=n&&o.r1>=r.r0&&o.c0<=s.c1&&o.c1>=s.c0))return null;const a={r1:s.r1,filter:s.filter?{...s.filter}:null};return t.transact("table-expand",()=>{const o=i=>{s.r1=i.r1,s.filter=i.filter?{...i.filter}:null,s.dirty=!0,t.engine.rebuild()},c={r1:n,filter:s.filter?{...s.filter,r1:n}:null};o(c);const l=(i,u)=>()=>(o(i),l(u,i));return l(a,c)}),s}return null}function et(t,e,r,s,n,a){if(!n.startsWith("="))return!1;for(const o of S(t,e)){if(s<o.c0||s>o.c1||r<=o.r0||r>o.r1-o.totalsRows)continue;const c=o.r0+o.headerRows,l=o.r1-o.totalsRows;for(let i=c;i<=l;i++)if(i!==r&&e.kind(i,s)!==0)return!1;return l===c?!1:(t.transact("calculated-column",i=>{for(let u=c;u<=l;u++)u!==r&&t.setInput(i,u,s,`=${a(n.slice(1),u-r,0)}`,{overLimitAsText:!0})}),!0)}return!1}const j=(t,e)=>{const r=t.totals?.[e];return r?r.label!==void 0?` totalsRowLabel="${I(r.label)}"`:` totalsRowFunction="${r.fn}"`:""},W=t=>`<tableStyleInfo name="${I(t.styleName??"TableStyleMedium2")}" showFirstColumn="${t.showFirstColumn?1:0}" showLastColumn="${t.showLastColumn?1:0}" showRowStripes="${t.showRowStripes===!1?0:1}" showColumnStripes="${t.showColumnStripes?1:0}"/>`,xe=t=>L({r0:t.r0,c0:t.c0,r1:t.r1-t.totalsRows,c1:t.c1}),Y=t=>t.filter&&t.headerRows>0?me({...t.filter,r0:t.r0,c0:t.c0,r1:t.r1-t.totalsRows,c1:t.c1},!1)+de(t.filter.sortState):"",G=t=>`${t.headerRows===0?' headerRowCount="0"':""}${t.totalsRows?` totalsRowCount="${t.totalsRows}"`:' totalsRowShown="0"'}`;function tt(t,e){const r=t.columns.map((n,a)=>{const o=B(t,a),c=`<tableColumn id="${t.columnIds?.[a]??a+1}" name="${I(n)}"${j(t,a)}`;return o?`${c}>${o}</tableColumn>`:`${c}/>`}).join(""),s=Y(t);return`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" id="${e}" name="${F(t.name)}" displayName="${F(t.name)}" ref="${L(t)}"${G(t)}>${s}<tableColumns count="${t.columns.length}">${r}</tableColumns>${W(t)}</table>`}function rt(t){return{name:t.name,ref:L(t),filterRef:t.filter&&t.headerRows>0?xe(t):null,filterXml:Y(t),counts:G(t),styleInfo:W(t),totals:t.totals?t.columns.map((e,r)=>j(t,r)):null,totalsChild:t.totals?t.columns.map((e,r)=>B(t,r)):null}}function nt(t,e){let r=t.replace(/<table\b[^>]*>/,s=>s.replace(/\bname="[^"]*"/,`name="${F(e.name)}"`).replace(/\bdisplayName="[^"]*"/,`displayName="${F(e.name)}"`).replace(/\bref="[^"]*"/,`ref="${e.ref}"`).replace(/\s(?:headerRowCount|totalsRowCount|totalsRowShown)="[^"]*"/g,"").replace(/\s*(\/?)>$/,`${e.counts}$1>`));return r=r.replace(/<autoFilter\b[^>]*\/>|<autoFilter\b[\s\S]*?<\/autoFilter>/,"").replace(/<sortState\b[^>]*\/>|<sortState\b[\s\S]*?<\/sortState>/,""),e.filterXml&&(r=r.replace("<tableColumns",`${e.filterXml}<tableColumns`)),/<tableStyleInfo\b/.test(r)?r=r.replace(/<tableStyleInfo\b[^>]*\/>/,e.styleInfo):r=r.replace("</tableColumns>",`</tableColumns>${e.styleInfo}`),e.totals&&(r=q(r,e)),r}const ye=/[[\]#']/g,U=t=>t.replace(ye,"'$&"),ve=/^[A-Za-z_\\][A-Za-z0-9_.\\]*$/;function z(t,e){const r=[];for(const s of t.sheets){const n=new Map;for(const[a,o]of s.formulas){const c=o.f??o.sf;if(typeof c!="string")continue;const l=o.f===void 0&&o.base!==void 0;let i=l?n.get(o):void 0;if(i===void 0){const u=e(c,s,Math.floor((l?o.base:a)/ne));i=u===c?null:o.f!==void 0?{...o,f:u,tpl:void 0}:{...o,sf:u,tpl:void 0},l&&n.set(o,i)}i!==null&&r.push([s.formulas,a,o,i])}}for(const[s,n]of t.engine.names){const a=e(n.text,null,0,s);a!==n.text&&r.push([t.engine.names,s,n,{...n,text:a}])}for(const s of t.engine.tables.values())s.totals?.forEach((n,a)=>{if(n?.fn!=="custom"||!n.formula)return;const o=e(n.formula,s.sheet,s.r1);o!==n.formula&&r.push([{set:(c,l)=>{s.totals[c]=l,s.dirty=!0}},a,n,{...n,formula:o}])});return t.formulasEdited=!0,r.some(s=>s[0]===t.engine.names)&&(t.namesChanged=!0),E(r,3),r}const E=(t,e)=>{for(const r of t)r[0].set(r[1],r[e])};function T(t,e,r,s,{rebuild:n=!1}={}){const a=t.engine;t.transact(r,o=>{const c={...e},l=s(o),i={...e};e.dirty=!0,n&&a.rebuild();const u=(f,m,h)=>()=>(Object.assign(e,f),e.dirty=!0,h?.(),n&&a.rebuild(),u(m,f,h));return u(c,i,l)})}function st(t,e,r,s){T(t,e,"table-option",()=>{e[r]=s})}function lt(t,e,r){r&&e.headerRows===0||T(t,e,"table-filter",()=>{e.filter=r?{r0:e.r0,c0:e.c0,r1:e.r1-e.totalsRows,c1:e.c1,columns:new Map}:null})}function ot(t,e,r){const s=t.engine;if(!ve.test(r)||/^[A-Za-z]{1,3}[0-9]+$/.test(r)||/^[Rr][Cc]?$|^[Cc]$/.test(r))return"That isn't a valid table name.";const n=r.toUpperCase();if(n===e.name.toUpperCase()&&r===e.name)return null;if(s.tables.has(n)&&s.tables.get(n)!==e)return"Another table already uses that name.";if(s.names.has(n))return"A defined name already uses that name.";const a=e.name;return t.transact("table-name",()=>{const o=new RegExp(`(^|[^A-Za-z0-9_.\\\\])${a.replace(/[.\\]/g,"\\$&")}(?=\\[)`,"gi"),c=z(t,f=>f.replace(o,`$1${r}`)),l=(f,m,h)=>{s.tables.delete(f.toUpperCase()),e.name=m,s.tables.set(m.toUpperCase(),e),e.dirty=!0,E(c,h),s.rebuild()};l(a,r,3);const i=()=>(l(a,r,3),u),u=()=>(l(r,a,2),i);return u}),null}const Ee={average:101,countNums:102,count:103,max:104,min:105,stdDev:107,sum:109,var:110};function Ae(t,e,r){if(!r)return null;if(r.label!==void 0)return r.label;if(r.fn==="custom")return r.formula?`=${r.formula}`:null;const s=Ee[r.fn];return s?`=SUBTOTAL(${s},${t.name}[${U(t.columns[e])}])`:null}function at(t,e,r){const{sheet:s}=e;if(r===e.totalsRows>0)return null;if(r){const a=e.r1+1;if(O(s,a,e.c0,e.c1))return"Clear the row under the table to add a total row.";const o=e.columns.map((l,i)=>e.totals?.[i]??null);if(!o.some(Boolean)&&(o[0]={label:ue()},o.length>1)){let l=!1;for(let i=e.r0+e.headerRows;i<=e.r1&&!l;i++)l=(s.kind(i,e.c1)&_)===1;o[o.length-1]={fn:l?"sum":"count"}}const c=o.map((l,i)=>Ae(e,i,l));for(let l=0;l<c.length;l++){if(o[l]?.label!==void 0||!c[l])continue;const i=H(c[l].slice(1));if(i)return i}return T(t,e,"table-totals",l=>{e.totals=o,c.forEach((i,u)=>{o[u]?.label!==void 0?t.setRichInput(l,a,e.c0+u,i,null):i&&t.setInput(l,a,e.c0+u,i)}),e.totalsRows=1,e.r1=a,e.columnsChanged=e.source!==!0},{rebuild:!0}),null}const n=e.r1;return T(t,e,"table-totals",a=>{for(let o=e.c0;o<=e.c1;o++)t.setInput(a,n,o,"");e.totalsRows=0,e.r1=n-1},{rebuild:!0}),null}function ct(t,e,r){if(r===e.headerRows>0)return null;const{sheet:s}=e;return r?e.r0===0||O(s,e.r0-1,e.c0,e.c1)?"Clear the row above the table to show its headers.":(T(t,e,"table-header",n=>{e.r0-=1,e.headerRows=1,e.columns.forEach((a,o)=>t.setRichInput(n,e.r0,e.c0+o,a,null)),e.filter={r0:e.r0,c0:e.c0,r1:e.r1-e.totalsRows,c1:e.c1,columns:new Map}},{rebuild:!0}),null):(T(t,e,"table-header",n=>{for(let a=e.c0;a<=e.c1;a++)t.setInput(n,e.r0,a,"");e.r0+=1,e.headerRows=0,e.filter=null},{rebuild:!0}),null)}function it(t,e,r){const{sheet:s}=e;if(r.r0!==e.r0)return"The header row must stay in the same row.";if(r.c0!==e.c0)return"The table must keep its first column.";if(r.r1<r.r0+e.headerRows+e.totalsRows)return"The table needs at least one data row.";for(const n of S(t,s))if(n!==e&&n.r0<=r.r1&&n.r1>=r.r0&&n.c0<=r.c1&&n.c1>=r.c0)return"A table can't overlap another table.";return T(t,e,"table-resize",()=>{const n=r.c1-r.c0+1,a=e.columns.slice(0,n);for(let o=a.length;o<n;o++){const c=e.c0+o;let l=e.headerRows?t.editText(s,e.r0,c).trim():"";l||(l=`${M()}${o+1}`),a.push(l)}e.columns=a,e.r1=r.r1,e.c1=r.c1,e.filter&&(e.filter={r0:e.r0,c0:e.c0,r1:e.r1-e.totalsRows,c1:e.c1,columns:new Map}),e.columnsChanged=!0},{rebuild:!0}),null}function Fe(t,e,r){const{sheet:s}=e,n=e.r1-e.r0+1,a=e.c1-e.c0+1,o=new Map,c=(i,u,f,m)=>{const h=(i-e.r0)*a+(u-e.c0);let d=o.get(h);d||o.set(h,d={}),d[f]=m};for(const i of r.areas(e,e.r0,e.r1,e.c0,e.c1)){const u=i.borders;for(let f=Math.max(i.r0,e.r0);f<=Math.min(i.r1,e.r1);f++)for(let m=Math.max(i.c0,e.c0);m<=Math.min(i.c1,e.c1);m++)u.t&&f===i.r0&&c(f,m,"t",u.t),u.b&&f===i.r1&&c(f,m,"b",u.b),u.l&&m===i.c0&&c(f,m,"l",u.l),u.r&&m===i.c1&&c(f,m,"r",u.r),u.h&&f<i.r1&&c(f,m,"b",u.h),u.v&&m<i.c1&&c(f,m,"r",u.v)}const l=new Map;t.transact("format",i=>{for(let u=0;u<n;u++){const f=e.r0+u;for(let m=0;m<a;m++){const h=e.c0+m,d=r.format(e,f,h),w=o.get(u*a+m),b=s.style(f,h),C=t.styles.get(b),p={};if(d.fill&&!C.fill&&(p.fill=d.fill),d.color&&!C.color&&(p.color=d.color),d.bold&&!C.bold&&(p.bold=!0),d.italic&&!C.italic&&(p.italic=!0),w){const x=C.borders??{},X={l:x.l??null,r:x.r??null,t:x.t??null,b:x.b??null};let N=!1;for(const y of["l","r","t","b"])w[y]&&!x[y]&&(X[y]={style:w[y].style,color:w[y].color},N=!0);N&&(p.borders=X)}const $=JSON.stringify(p);if($==="{}")continue;const g=`${b}|${$}`;let R=l.get(g);R===void 0&&l.set(g,R=t.styles.derive(b,p,$)),R!==b&&(i.saveCell(f,h),s.setStyle(f,h,R))}}})}function ut(t,e){be(t,"Convert to Range",()=>Le(t,e))}function Le(t,e){const r=t.engine,{sheet:s}=e,n=Re(e.styleName,t.theme),a=t.activeSheet;t.activeSheet=t.sheets.indexOf(s),n?.format&&Fe(t,e,n),t.activeSheet=a;const o=e.name.toUpperCase(),c=new RegExp(`(^|[^A-Za-z0-9_.\\\\])${e.name.replace(/[.\\]/g,"\\$&")}(\\[(?:[^\\[\\]']|'.|\\[(?:[^\\[\\]']|'.)*\\])*\\])`,"gi");t.transact("table-convert",()=>{const l=z(t,(m,h,d)=>m.replace(c,(w,b,C)=>{const p=r.tableSpecToA1(e,C,d);return p===null?w:`${b}${h===s?"":le(s.name)}${p}`})),i=e.source===!0&&e.part?{part:e.part,sheetIndex:t.sheets.indexOf(s)}:null,u=()=>{r.tables.delete(o),i&&(t.removedTables??=[]).push(i),E(l,3),r.rebuild()},f=()=>(r.tables.set(o,e),i&&(t.removedTables=t.removedTables.filter(m=>m!==i)),E(l,2),r.rebuild(),()=>(u(),f));return u(),f})}function Me(t,e,r,s){const n=t.toLowerCase(),a=`${e.toLowerCase()}[`;let o="",c=0;for(let l=n.indexOf(a);l>=0;l=n.indexOf(a,l+1)){const i=l+a.length-1;let u=0,f=i;for(;f<t.length;f++){const d=t[f];if(d==="'")f++;else if(d==="[")u++;else if(d==="]"&&--u===0)break}const m=t.slice(i,f+1),h=m.split(`[${r}]`).join(`[${s}]`);h!==m&&(o+=t.slice(c,i)+h,c=f+1)}return c===0?t:o+t.slice(c)}function _e(t,e,r,s){const n=e.columns[r],a=U(n),o=U(s),c=z(t,f=>f.includes("[")?Me(f,e.name,a,o):f),l=(f,m)=>{e.columns=e.columns.map((h,d)=>d===r?f:h),e.columnsChanged=!0,e.dirty=!0,E(c,m),t.engine?.rebuild()};l(s,3);const i=()=>(l(s,3),u),u=()=>(l(n,2),i);return u}function ft(t,e,r){let s=null;const{sheet:n}=e;for(const a of S(t,n))if(!(a.headerRows===0||a.dataSheet))for(let o=0;o<r.count;o++){if(r.rows[o]!==a.r0||r.cols[o]<a.c0||r.cols[o]>a.c1)continue;const c=r.cols[o],l=c-a.c0,i=n.kind(a.r0,c);let u=i===0?"":String(t.engine.read(n,a.r0,c)??"").trim();const f=new Set(a.columns.filter((w,b)=>b!==l).map(w=>w.toLowerCase()));if(u==="")u=oe(f);else if(f.has(u.toLowerCase())){const w=u;let b=2;for(;f.has(`${w}${b}`.toLowerCase());)b++;u=`${w}${b}`}if(((i&_)!==v||(i&D)!==0||t.strings.get(n.value(a.r0,c))!==u)&&(e.saveCell(a.r0,c),n.setFormula(a.r0,c,null),n.setCell(a.r0,c,v,t.strings.add(u))),a.columns[l]===u)continue;const m=_e(t,a,l,u),h=s,d=(w,b)=>b?()=>d(w(),b()):w;s=d(m,h)}return s}const mt=[...Array.from({length:21},(t,e)=>`TableStyleLight${e+1}`),...Array.from({length:28},(t,e)=>`TableStyleMedium${e+1}`),...Array.from({length:11},(t,e)=>`TableStyleDark${e+1}`)];export{Ee as SUBTOTAL_CODE,mt as TABLE_STYLE_NAMES,Te as TableCreationError,Je as bandRow,re as colName,ut as convertTableToRange,Qe as createTable,et as fillCalculatedColumn,ke as growTableOver,Ve as maybeExpandTable,Se as nextTableName,nt as patchTableXml,ge as presetElements,ot as renameTable,_e as renameTableColumn,it as resizeTable,z as rewriteFormulas,qe as setCustomTableResolver,lt as setTableFilterButton,ct as setTableHeaderRow,st as setTableOption,at as setTableTotals,U as structuredColumn,ft as syncTableHeaders,Re as tableColors,rt as tablePatch,tt as tableXml,S as tablesOf,Ae as totalInput};
package/core/whatif.js CHANGED
@@ -1 +1 @@
1
- import{FORMULA as H,KIND_MASK as K,NUM as b,STR as A}from"./sheet.js";import{cellRef as I,MAX_COLS as W,parseRangeRef as F,rangeRef as X}from"./address.js";async function Z(t,n,r,e,a,{maxIter:o=200,tolerance:l=.001}={}){const u=t.engine,g=async d=>{t.transact("goal-seek",S=>{S.saveCell(e.row,e.col),n.setCell(e.row,e.col,b,d)}),await u.whenIdle();const x=u.read(n,r.row,r.col);return typeof x=="number"?x-a:NaN},s=()=>t.undoStack.length,c=s(),f=n.value(e.row,e.col);let p=f,i=await g(p),v=f===0?1:f*1.01,M=await g(v),E={x:p,f:i},L=null,h=null;for(let d=0;d<o&&((Math.abs(M)<Math.abs(E.f)||Number.isNaN(E.f))&&(E={x:v,f:M}),!(Math.abs(M)<=l));d++){i*M<0&&(L=i<0?p:v,h=i<0?v:p);let x=M===i?v*1.5+1:v-M*(v-p)/(M-i);if(L!==null&&(!Number.isFinite(x)||x<Math.min(L,h)||x>Math.max(L,h))&&(x=(L+h)/2),!Number.isFinite(x))break;p=v,i=M,v=x,M=await g(v)}for(;s()>c;)t.undoStack.pop();t.transact("goal-seek",d=>{d.saveCell(e.row,e.col),n.setCell(e.row,e.col,b,f)});const m=t.undoStack.pop();return t.transact("goal-seek",d=>{d.saveCell(e.row,e.col),n.setCell(e.row,e.col,b,E.x)}),await u.whenIdle(),{value:E.x,found:Math.abs(E.f)<=l,result:u.read(n,r.row,r.col)}}const P=t=>{if(t===void 0)return 0;const n=t.indexOf('outlineLevel="');return n<0?0:t.charCodeAt(n+14)-48},U=(t,n)=>t.rowExtras===null?0:P(t.rowExtras.get(n));function j(t,n,r){t.rowExtras||(t.rowExtras=new Map);const e=(t.rowExtras.get(n)??"").replace(/\s*outlineLevel="\d+"/,"").trim(),a=r>0?`${e} outlineLevel="${r}"`.trim():e;a?t.rowExtras.set(n,a):t.rowExtras.delete(n)}const _=(t,n)=>t.colExtras===null?0:P(t.colExtras.get(n));function z(t,n,r){t.colExtras||(t.colExtras=new Map);const e=(t.colExtras.get(n)??"").replace(/\s*outlineLevel="\d+"/,"").trim(),a=r>0?`${e} outlineLevel="${r}"`.trim():e;a?t.colExtras.set(n,a):t.colExtras.delete(n)}function C(t,n,r,e,a){for(let o=r;o<=e;o++)n==="row"?j(t,o,Math.max(0,Math.min(7,U(t,o)+a))):z(t,o,Math.max(0,Math.min(7,_(t,o)+a)));t.outlineVersion=(t.outlineVersion??0)+1,n==="col"&&(t.colsChanged=!0),t.revision++}function tt(t,n){let r=0;const e=n==="row"?t.rowExtras:t.colExtras;if(e)for(const a of e.values()){const o=/outlineLevel="(\d+)"/.exec(a);o&&(r=Math.max(r,Number(o[1])))}return r}function nt(t,n,r){return n==="row"?U(t,r):_(t,r)}function rt(t,n,r){const e=n==="row"?t.rowExtras:t.colExtras,a=n==="row"?t.rows:t.cols;if(e){for(const[o,l]of e){const u=/outlineLevel="(\d+)"/.exec(l);u&&a.setHidden(o,Number(u[1])>=r)}n==="col"&&(t.colsChanged=!0),t.revision++}}function et(t,n,r,e,a,o){const l=[],u={1:"Average",2:"Count",3:"Count",4:"Max",5:"Min",6:"Product",7:"StdDev",8:"StdDevp",9:"Total",10:"Var",11:"Varp"}[a],g=c=>(n.kind(c,e)&K)===A?t.strings.get(n.value(c,e)):String(n.value(c,e));let s=r.r0+1;for(let c=r.r0+1;c<=r.r1+1;c++)(c>r.r1||g(c)!==g(s))&&(l.push({afterRow:c-1,from:s,to:c-1,title:`${g(s)} ${u}`}),s=c);return{groups:l,functionNum:a,sumCols:o,groupCol:e,grandTitle:`Grand ${u}`}}function ot(t,n,r,e,a){const o={r0:r.r0+1,c0:r.c0+1,r1:r.r1,c1:r.c1},l=e&&a,u={t:"dataTable",ref:X(o),dt2D:l?"1":"0",dtr:e?"1":"0",r1:I((e??a).row,(e??a).col)};l&&(u.r2=I(a.row,a.col));const g=t.engine,s={sheet:n,row:o.r0,col:o.c0,info:u};t.transact("data-table",c=>{for(let i=o.r0;i<=o.r1;i++)for(let v=o.c0;v<=o.c1;v++)c.saveCell(i,v),n.setFormula(i,v,null),n.setCell(i,v,b,0);n.setFormula(o.r0,o.c0,u),n.setCell(o.r0,o.c0,H|b,0),t.formulasEdited=!0,g.dataTables.push(s),g.tablesDirty=!0;const f=()=>(g.dataTables=g.dataTables.filter(i=>i!==s),p),p=()=>(g.dataTables.push(s),g.tablesDirty=!0,f);return f})}const q=(t,n,r,e)=>{const a=n.kind(r,e)&K;return a===0?"":a===A?t.strings.get(n.value(r,e)):String(n.value(r,e))};function at(t,n,r,e){const a=[],o=new Set;for(const l of r.split(/[\s,;]+/)){const u=F(l.replace(/\$/g,""));if(u)for(let g=u.r0;g<=u.r1&&a.length<32;g++)for(let s=u.c0;s<=u.c1&&a.length<32;s++){const c=I(g,s);if(o.has(c))continue;o.add(c);const f=e?.cells.find(p=>p.r===c);a.push({r:c,val:f?f.val:q(t,n,g,s)})}}return a}function ct(t,n,r){t.transact("scenario",e=>{for(const{r:a,val:o}of r.cells){const l=F(a);l&&t.setInput(e,l.r0,l.c0,o)}})}function st(t,n,r,e){const a=t.engine,o=[["Scenario Summary","Current Values",...r.map(s=>s.name)]],l=[];for(const s of r)for(const c of s.cells)l.includes(c.r)||l.push(c.r);const u=s=>{const c=F(s);return{row:c.r0,col:c.c0}};o.push(["Changing Cells:"]);for(const s of l){const{row:c,col:f}=u(s);o.push([s,q(t,n,c,f),...r.map(p=>p.cells.find(i=>i.r===s)?.val??"")])}if(e.length>0){o.push(["Result Cells:"]);const s=r.map(c=>a.evaluateWith(n,c.cells.map(f=>({...u(f.r),text:f.val})),()=>e.map(({row:f,col:p})=>a.read(n,f,p))));e.forEach(({row:c,col:f},p)=>{o.push([I(c,f),String(a.read(n,c,f)??""),...s.map(i=>String(i[p]??""))])})}const g=t.addSheet();return t.transact("scenario-summary",s=>{o.forEach((c,f)=>c.forEach((p,i)=>{p!==""&&t.setInput(s,f,i,String(p))}))}),g}const w=t=>t.filter(n=>typeof n=="number"),R=t=>t.reduce((n,r)=>n+r,0),B=t=>R(t)/t.length,O=(t,n)=>{const r=B(t);return R(t.map(e=>(e-r)**2))/n},J={Sum:t=>R(w(t)),Count:t=>t.filter(n=>n!==null&&n!=="").length,Average:t=>w(t).length?B(w(t)):null,Max:t=>w(t).length?Math.max(...w(t)):0,Min:t=>w(t).length?Math.min(...w(t)):0,Product:t=>w(t).length?w(t).reduce((n,r)=>n*r,1):0,"Count Numbers":t=>w(t).length,StdDev:t=>w(t).length>1?Math.sqrt(O(w(t),w(t).length-1)):null,StdDevp:t=>w(t).length?Math.sqrt(O(w(t),w(t).length)):null,Var:t=>w(t).length>1?O(w(t),w(t).length-1):null,Varp:t=>w(t).length?O(w(t),w(t).length):null};function lt(t,n,r,e,a,{topRow:o=!1,leftCol:l=!1}={}){const u=t.engine,g=J[a],s=[],c=[],f=new Map,p=new Map,i=new Map,v=(h,m,d)=>{const x=String(d).toLowerCase();let S=h.get(x);return S===void 0&&(S=m.length,h.set(x,S),m.push(d)),S};for(const{sheet:h,range:m,read:d}of e){const x=d??((y,T)=>u.read(h,y,T)),S=m.r0+(o?1:0),N=m.c0+(l?1:0);for(let y=S;y<=m.r1;y++){const T=l?x(y,m.c0):y-S;if(l&&(T===null||T===""))continue;const k=v(f,s,T);for(let D=N;D<=m.c1;D++){const V=o?x(m.r0,D):D-N;if(o&&(V===null||V===""))continue;const G=v(p,c,V),$=k*16384+G;i.has($)||i.set($,[]),i.get($).push(x(y,D))}}}const M=r.row+(o?1:0),E=r.col+(l?1:0),L=(h,m,d,x)=>{h.saveCell(m,d),n.setFormula(m,d,null),typeof x=="number"?n.setCell(m,d,b,x):n.setCell(m,d,A,t.strings.add(String(x)))};return t.transact("consolidate",h=>{o&&c.forEach((m,d)=>L(h,r.row,E+d,m)),l&&s.forEach((m,d)=>L(h,M+d,r.col,m));for(const[m,d]of i){const x=Math.floor(m/16384),S=m-x*16384,N=g(d);N!==null&&L(h,M+x,E+S,N)}}),{rows:s.length,cols:c.length}}export{J as CONSOLIDATE_FUNCTIONS,W as MAX_COLS,C as changeOutline,lt as consolidate,ot as createDataTable,Z as goalSeek,tt as maxOutlineLevel,nt as outlineLevel,st as scenarioSummary,at as scenarioValues,rt as showOutlineLevel,ct as showScenario,et as subtotalPlan};
1
+ import{FORMULA as H,KIND_MASK as K,NUM as b,STR as $}from"./sheet.js";import{cellRef as I,MAX_COLS as W,parseRangeRef as F,rangeRef as X}from"./address.js";async function Z(t,n,r,e,a,{maxIter:o=200,tolerance:l=.001}={}){const u=t.engine,g=async d=>{t.transact("goal-seek",S=>{S.saveCell(e.row,e.col),n.setCell(e.row,e.col,b,d)}),await u.whenIdle();const x=u.read(n,r.row,r.col);return typeof x=="number"?x-a:NaN},s=()=>t.undoStack.length,c=s(),f=n.value(e.row,e.col);let p=f,i=await g(p),v=f===0?1:f*1.01,M=await g(v),E={x:p,f:i},L=null,h=null;for(let d=0;d<o&&((Math.abs(M)<Math.abs(E.f)||Number.isNaN(E.f))&&(E={x:v,f:M}),!(Math.abs(M)<=l));d++){i*M<0&&(L=i<0?p:v,h=i<0?v:p);let x=M===i?v*1.5+1:v-M*(v-p)/(M-i);if(L!==null&&(!Number.isFinite(x)||x<Math.min(L,h)||x>Math.max(L,h))&&(x=(L+h)/2),!Number.isFinite(x))break;p=v,i=M,v=x,M=await g(v)}for(;s()>c;)t.undoStack.pop();t.transact("goal-seek",d=>{d.saveCell(e.row,e.col),n.setCell(e.row,e.col,b,f)});const m=t.undoStack.pop();return t.transact("goal-seek",d=>{d.saveCell(e.row,e.col),n.setCell(e.row,e.col,b,E.x)}),await u.whenIdle(),{value:E.x,found:Math.abs(E.f)<=l,result:u.read(n,r.row,r.col)}}const P=t=>{if(t===void 0)return 0;const n=t.indexOf('outlineLevel="');return n<0?0:t.charCodeAt(n+14)-48},U=(t,n)=>t.rowExtras===null?0:P(t.rowExtras.get(n));function j(t,n,r){t.rowExtras||(t.rowExtras=new Map);const e=(t.rowExtras.get(n)??"").replace(/\s*outlineLevel="\d+"/,"").trim(),a=r>0?`${e} outlineLevel="${r}"`.trim():e;a?t.rowExtras.set(n,a):t.rowExtras.delete(n)}const _=(t,n)=>t.colExtras===null?0:P(t.colExtras.get(n));function z(t,n,r){t.colExtras||(t.colExtras=new Map);const e=(t.colExtras.get(n)??"").replace(/\s*outlineLevel="\d+"/,"").trim(),a=r>0?`${e} outlineLevel="${r}"`.trim():e;a?t.colExtras.set(n,a):t.colExtras.delete(n)}function C(t,n,r,e,a){for(let o=r;o<=e;o++)n==="row"?j(t,o,Math.max(0,Math.min(7,U(t,o)+a))):z(t,o,Math.max(0,Math.min(7,_(t,o)+a)));t.outlineVersion=(t.outlineVersion??0)+1,n==="col"&&(t.colsChanged=!0),t.revision++}function tt(t,n){let r=0;const e=n==="row"?t.rowExtras:t.colExtras;if(e)for(const a of e.values()){const o=/outlineLevel="(\d+)"/.exec(a);o&&(r=Math.max(r,Number(o[1])))}return r}function nt(t,n,r){return n==="row"?U(t,r):_(t,r)}function rt(t,n,r){const e=n==="row"?t.rowExtras:t.colExtras,a=n==="row"?t.rows:t.cols;if(e){for(const[o,l]of e){const u=/outlineLevel="(\d+)"/.exec(l);u&&a.setHidden(o,Number(u[1])>=r)}n==="col"&&(t.colsChanged=!0),t.revision++}}function et(t,n,r,e,a,o){const l=[],u={1:"Average",2:"Count",3:"Count",4:"Max",5:"Min",6:"Product",7:"StdDev",8:"StdDevp",9:"Total",10:"Var",11:"Varp"}[a],g=c=>(n.kind(c,e)&K)===$?t.strings.get(n.value(c,e)):String(n.value(c,e));let s=r.r0+1;for(let c=r.r0+1;c<=r.r1+1;c++)(c>r.r1||g(c)!==g(s))&&(l.push({afterRow:c-1,from:s,to:c-1,title:`${g(s)} ${u}`}),s=c);return{groups:l,functionNum:a,sumCols:o,groupCol:e,grandTitle:`Grand ${u}`}}function ot(t,n,r,e,a){const o={r0:r.r0+1,c0:r.c0+1,r1:r.r1,c1:r.c1},l=e&&a,u={t:"dataTable",ref:X(o),dt2D:l?"1":"0",dtr:e?"1":"0",r1:I((e??a).row,(e??a).col)};l&&(u.r2=I(a.row,a.col));const g=t.engine,s={sheet:n,row:o.r0,col:o.c0,info:u};t.transact("data-table",c=>{for(let i=o.r0;i<=o.r1;i++)for(let v=o.c0;v<=o.c1;v++)c.saveCell(i,v),n.setFormula(i,v,null),n.setCell(i,v,b,0);n.setFormula(o.r0,o.c0,u),n.setCell(o.r0,o.c0,H|b,0),t.formulasEdited=!0,g.dataTables.push(s),g.tablesDirty=!0;const f=()=>(g.dataTables=g.dataTables.filter(i=>i!==s),p),p=()=>(g.dataTables.push(s),g.tablesDirty=!0,f);return f})}const q=(t,n,r,e)=>{const a=n.kind(r,e)&K;return a===0?"":a===$?t.strings.get(n.value(r,e)):String(n.value(r,e))};function at(t,n,r,e){const a=[],o=new Set;for(const l of r.split(/[\s,;]+/)){const u=F(l.replace(/\$/g,""));if(u)for(let g=u.r0;g<=u.r1&&a.length<32;g++)for(let s=u.c0;s<=u.c1&&a.length<32;s++){const c=I(g,s);if(o.has(c))continue;o.add(c);const f=e?.cells.find(p=>p.r===c);a.push({r:c,val:f?f.val:q(t,n,g,s)})}}return a}function ct(t,n,r){t.transact("scenario",e=>{for(const{r:a,val:o}of r.cells){const l=F(a);l&&t.setInput(e,l.r0,l.c0,o,{overLimitAsText:!0})}})}function st(t,n,r,e){const a=t.engine,o=[["Scenario Summary","Current Values",...r.map(s=>s.name)]],l=[];for(const s of r)for(const c of s.cells)l.includes(c.r)||l.push(c.r);const u=s=>{const c=F(s);return{row:c.r0,col:c.c0}};o.push(["Changing Cells:"]);for(const s of l){const{row:c,col:f}=u(s);o.push([s,q(t,n,c,f),...r.map(p=>p.cells.find(i=>i.r===s)?.val??"")])}if(e.length>0){o.push(["Result Cells:"]);const s=r.map(c=>a.evaluateWith(n,c.cells.map(f=>({...u(f.r),text:f.val})),()=>e.map(({row:f,col:p})=>a.read(n,f,p))));e.forEach(({row:c,col:f},p)=>{o.push([I(c,f),String(a.read(n,c,f)??""),...s.map(i=>String(i[p]??""))])})}const g=t.addSheet();return t.transact("scenario-summary",s=>{o.forEach((c,f)=>c.forEach((p,i)=>{p!==""&&t.setInput(s,f,i,String(p),{overLimitAsText:!0})}))}),g}const w=t=>t.filter(n=>typeof n=="number"),R=t=>t.reduce((n,r)=>n+r,0),B=t=>R(t)/t.length,O=(t,n)=>{const r=B(t);return R(t.map(e=>(e-r)**2))/n},J={Sum:t=>R(w(t)),Count:t=>t.filter(n=>n!==null&&n!=="").length,Average:t=>w(t).length?B(w(t)):null,Max:t=>w(t).length?Math.max(...w(t)):0,Min:t=>w(t).length?Math.min(...w(t)):0,Product:t=>w(t).length?w(t).reduce((n,r)=>n*r,1):0,"Count Numbers":t=>w(t).length,StdDev:t=>w(t).length>1?Math.sqrt(O(w(t),w(t).length-1)):null,StdDevp:t=>w(t).length?Math.sqrt(O(w(t),w(t).length)):null,Var:t=>w(t).length>1?O(w(t),w(t).length-1):null,Varp:t=>w(t).length?O(w(t),w(t).length):null};function lt(t,n,r,e,a,{topRow:o=!1,leftCol:l=!1}={}){const u=t.engine,g=J[a],s=[],c=[],f=new Map,p=new Map,i=new Map,v=(h,m,d)=>{const x=String(d).toLowerCase();let S=h.get(x);return S===void 0&&(S=m.length,h.set(x,S),m.push(d)),S};for(const{sheet:h,range:m,read:d}of e){const x=d??((y,T)=>u.read(h,y,T)),S=m.r0+(o?1:0),N=m.c0+(l?1:0);for(let y=S;y<=m.r1;y++){const T=l?x(y,m.c0):y-S;if(l&&(T===null||T===""))continue;const k=v(f,s,T);for(let D=N;D<=m.c1;D++){const A=o?x(m.r0,D):D-N;if(o&&(A===null||A===""))continue;const G=v(p,c,A),V=k*16384+G;i.has(V)||i.set(V,[]),i.get(V).push(x(y,D))}}}const M=r.row+(o?1:0),E=r.col+(l?1:0),L=(h,m,d,x)=>{h.saveCell(m,d),n.setFormula(m,d,null),typeof x=="number"?n.setCell(m,d,b,x):n.setCell(m,d,$,t.strings.add(String(x)))};return t.transact("consolidate",h=>{o&&c.forEach((m,d)=>L(h,r.row,E+d,m)),l&&s.forEach((m,d)=>L(h,M+d,r.col,m));for(const[m,d]of i){const x=Math.floor(m/16384),S=m-x*16384,N=g(d);N!==null&&L(h,M+x,E+S,N)}}),{rows:s.length,cols:c.length}}export{J as CONSOLIDATE_FUNCTIONS,W as MAX_COLS,C as changeOutline,lt as consolidate,ot as createDataTable,Z as goalSeek,tt as maxOutlineLevel,nt as outlineLevel,st as scenarioSummary,at as scenarioValues,rt as showOutlineLevel,ct as showScenario,et as subtotalPlan};
package/core/workbook.js CHANGED
@@ -1 +1 @@
1
- import{expandBareTables as j}from"../slicers/structured.js";import{syncTableHeaders as J}from"./tables.js";import{BOOL as I,CHUNK_BITS as A,CHUNK_MASK as W,EMPTY as _,ERR as F,ERRORS as Z,FORMULA as v,DIRTY as N,EVALUATING as z,KIND_MASK as Y,NUM as P,Sheet as T,SPILL as X,STR as R}from"./sheet.js";import{cellRef as Q,MAX_COLS as w,MAX_ROWS as B,quoteSheetName as ee,rangeRef as te,renameSheetInFormula as D,sheetReferencePattern as q}from"./address.js";import{translateFormula as se}from"./formula-refs.js";import{sheetStem as G}from"./default-names.js";import{StringTable as ne}from"./strings.js";import{StyleTable as ie}from"./styles.js";import{parseInput as H}from"./input.js";import{noteChanges as V}from"./changes.js";import{overrideRuns as le}from"../cells/rich-runs.js";import{sheetRenameHooks as re}from"./sparse.js";import{shiftRuleFormulas as oe}from"../cells/cf-file.js";import{expandStructured as ae,toDisplayFormula as K,toFileFormula as he}from"../calc/parser.js";const ce=200,ue=new Set(["entry","clear","format","borders","fill"]);class fe{constructor(){this.ids=new Int32Array(1024),this.list=[]}set(t,e){if(t>=this.ids.length){let l=this.ids.length*2;for(;l<=t;)l*=2;const i=new Int32Array(l);i.set(this.ids),this.ids=i}const n=this.list.length;(n===0||this.list[n-1]!==e)&&this.list.push(e),this.ids[t]=this.list.length}get(t){const e=t<this.ids.length?this.ids[t]:0;return e===0?void 0:this.list[e-1]}}class b{constructor(){this.capacity=1024,this.count=0,this.rows=new Uint32Array(this.capacity),this.cols=new Uint16Array(this.capacity),this.kinds=new Uint8Array(this.capacity),this.styles=new Uint16Array(this.capacity),this.values=new Float64Array(this.capacity),this.formulas=null,this.vms=null}_grow(){const t=this.capacity*2,e=(n,l)=>{const i=new n(t);return i.set(l),i};this.rows=e(Uint32Array,this.rows),this.cols=e(Uint16Array,this.cols),this.kinds=e(Uint8Array,this.kinds),this.styles=e(Uint16Array,this.styles),this.values=e(Float64Array,this.values),this.capacity=t}push(t,e,n){this.count===this.capacity&&this._grow();const l=this.count++,i=t.kind(e,n)&~(N|z);if(this.rows[l]=e,this.cols[l]=n,this.kinds[l]=i,this.styles[l]=t.cellStyle(e,n),this.values[l]=t.value(e,n),i&v&&(this.formulas===null&&(this.formulas=new fe),this.formulas.set(l,t.formula(e,n))),t.valueMeta!==null){const s=t.valueMeta.get(e*w+n);s!==void 0&&(this.vms??=new Map).set(l,s)}}}class be{constructor(){this.sheets=[new T(`${G()}1`)],this.activeSheet=0,this.strings=new ne,this.styles=new ie,this.undoStack=[],this.redoStack=[],this.listeners=new Set,this.source=null,this.formulasEdited=!1,this.edits=0,this._dirty=!1,this.engine=null,this.selectedSheets=null}get sheet(){return this.sheets[this.activeSheet]}get dirty(){return this._dirty}set dirty(t){t&&this.edits++,this._dirty=!!t}onChange(t){this.listeners.add(t)}_emit(t){this.dirty=!0;for(const e of this.listeners)e(t)}addSheet(t){const e=G();let n=1,l=t??`${e}${n}`;const i=o=>this.sheets.some(r=>r.name.toLowerCase()===o.toLowerCase());for(;i(l);)l=`${e}${++n}`;const s=new T(l);return this.sheets.push(s),this.activeSheet=this.sheets.length-1,this.engine?.sheetsChanged(),this._emit("sheets"),s}deleteSheet(t){if(this.sheets.filter(n=>!n.hidden).length<=1)return!1;this._shrink3D(t),this._refErrorsFor(t);const[e]=this.sheets.splice(t,1);if(this.selectedSheets?.delete(e),this._forgetSheet(e,t),this.engine)for(const[n,l]of this.engine.tables)l.sheet===e&&this.engine.tables.delete(n);for(this._reindexNames(n=>n===t?-1:n>t?n-1:n),this.activeSheet>=this.sheets.length&&(this.activeSheet=this.sheets.length-1);this.sheets[this.activeSheet].hidden;)this.activeSheet=(this.activeSheet+1)%this.sheets.length;return this.structureEdited=!0,this.engine?.sheetsChanged(),this.engine?.rebuild(),this._emit("sheets"),!0}_forgetSheet(t,e){const n=i=>i.sheet===t||i.sheet===void 0&&i.sheetIndex===e||(i.extra?.parts??[]).some(n),l=i=>{i.sheetIndex>e&&i.sheetIndex--;for(const s of i.extra?.parts??[])l(s)};for(const i of[this.undoStack,this.redoStack]){const s=i.filter(o=>!n(o));s.forEach(l),i.splice(0,i.length,...s)}}moveSheet(t,e){if(t===e)return;const n=this.sheet,[l]=this.sheets.splice(t,1);this.sheets.splice(e,0,l),this.activeSheet=this.sheets.indexOf(n);const i=o=>o===t?e:t<e?o>t&&o<=e?o-1:o:o>=e&&o<t?o+1:o,s=o=>{o.sheetIndex=i(o.sheetIndex);for(const r of o.extra?.parts??[])s(r)};for(const o of this.undoStack)s(o);for(const o of this.redoStack)s(o);this.structureEdited=!0,this._reindexNames(i),this.engine?.orderChanged(),this._emit("sheets")}_copyScopedNames(t,e,n){const{engine:l}=this;if(!l||l.names.size===0)return;const i=`${t}!`;let s=!1;for(const[o,r]of[...l.names])o.startsWith(i)&&(l.names.set(`${t+1}!${o.slice(i.length)}`,{...r,text:D(r.text,e.name,n.name)}),s=!0);s&&(this.namesChanged=!0)}_reindexNames(t){const{engine:e}=this;if(!e||e.names.size===0)return;const n=[];let l=!1;for(const[i,s]of e.names){const o=i.indexOf("!"),r=o<0?0:t(Number(i.slice(0,o)));o>=0&&r!==Number(i.slice(0,o))&&(l=!0),o<0?n.push([i,s]):r>=0&&n.push([`${r}!${i.slice(o+1)}`,s])}if(l){e.names.clear();for(const[i,s]of n)e.names.set(i,s);e.nameTemplates.clear(),this.namesChanged=!0,e.rebuild()}}duplicateSheet(t){const e=this.sheets[t];let l=`${e.name} (2)`,i=2;for(;this.sheets.some(a=>a.name.toLowerCase()===l.toLowerCase());)l=`${e.name} (${++i})`;const s=new T(l);e.columns.forEach((a,h)=>{const u=[];a.forEach((c,g)=>{u[g]={kinds:c.kinds.slice(),values:c.values.slice(),styles:c.styles?c.styles.slice():null}}),s.columns[h]=u}),s.maxRow=e.maxRow,s.maxCol=e.maxCol,s.rows=e.rows.shifted(0,0),s.cols=e.cols.shifted(0,0);const o=new Map;e.formulas.forEachCell((a,h,u)=>{let c=o.get(u);c===void 0&&(c={...u,tpl:void 0},o.set(u,c)),s.setFormula(a,h,c)}),s.merges=e.merges.map(a=>({...a})),s.rowStyles=e.rowStyles?new Map(e.rowStyles):null,s.colStyles=e.colStyles?new Map(e.colStyles):null,s.frozenRows=e.frozenRows,s.frozenCols=e.frozenCols,s.showGridlines=e.showGridlines,s.cellTypes=e.cellTypes?new Map(e.cellTypes):null,s.sparkRules=e.sparkRules,s.cellDropdowns=e.cellDropdowns?new Map(e.cellDropdowns):null,s.cellTags=e.cellTags?new Map(e.cellTags):null,s.cellButtons=e.cellButtons?new Map(e.cellButtons):null,s.cellStates=e.cellStates?e.cellStates.slice():null,s.autoMerges=e.autoMerges?e.autoMerges.map(a=>({...a})):null,s.background=e.background,s.outlineColumn=e.outlineColumn?{...e.outlineColumn}:null,s.frozenTrailingRows=e.frozenTrailingRows,s.frozenTrailingCols=e.frozenTrailingCols,s.hideZeros=e.hideZeros,s.hideHeadings=e.hideHeadings,s.isChartSheet=e.isChartSheet,s.hideOutline=e.hideOutline,s.gridColorId=e.gridColorId,s.areaOffset=e.areaOffset,s.selectionPolicy=e.selectionPolicy,s.selectionUnit=e.selectionUnit,s.gridlineAxes=e.gridlineAxes,s.gridColor=e.gridColor,s.tabStyles=e.tabStyles;const r=x;return s.cf=r(e.cf),s.validations=r(e.validations),s.comments=r(e.comments),s.threads=r(e.threads),s.hyperlinks=e.hyperlinks?e.hyperlinks.map(a=>({...r({...a,_ranges:void 0})})):e.hyperlinks,s.autoFilter=r(e.autoFilter),s.filteredRows=e.filteredRows?new Set(e.filteredRows):e.filteredRows,s.pageSetup=r(e.pageSetup),s.tabColor=e.tabColor,s.protection=r(e.protection),s.rtl=e.rtl,s.zoom=e.zoom,s.valueMeta=e.valueMeta?new Map(e.valueMeta):null,s.colsChanged=!0,s.viewChanged=!0,this.sheets.splice(t+1,0,s),this.activeSheet=t+1,this._reindexNames(a=>a>t?a+1:a),this._copyScopedNames(t,e,s),this.structureEdited=!0,this.engine?.sheetsChanged(),this.engine?.rebuild(),this._emit("sheets"),s}sheetNameProblem(t,e){const n=e.trim().slice(0,31);return!n||/[\\/?*[\]:]/.test(n)||/^'|'$/.test(n)?"rules":n.toLowerCase()==="history"?"reserved":this.sheets.some((l,i)=>i!==t&&l.name.toLowerCase()===n.toLowerCase())?"taken":null}renameSheet(t,e){const n=e.trim().slice(0,31);if(this.sheetNameProblem(t,e)!==null)return!1;const l=this.sheets[t].name;this.sheets[t].name=n,this._renameInFormulas(l,n);for(const i of re)i(this,l,n);return this.engine?.sheetsChanged(),this._emit("sheets"),!0}_shrink3D(t){const e=this.sheets[t].name.toLowerCase(),n=r=>this.sheets.findIndex(a=>a.name.toLowerCase()===r.toLowerCase()),l=r=>r[0]==="'"?r.slice(1,-1).replace(/''/g,"'"):r,i=/((?:'(?:[^']|'')+'|[A-Za-z_\u00C0-\uFFFF][\w.\u00C0-\uFFFF]*)):((?:'(?:[^']|'')+'|[A-Za-z_\u00C0-\uFFFF][\w.\u00C0-\uFFFF]*))!/g,s=r=>r.replace(i,(a,h,u,c)=>{if(r[c-1]==="]")return a;const g=l(h),p=l(u);if(g.toLowerCase()!==e&&p.toLowerCase()!==e)return a;let f=n(g),S=n(p);if(f<0||S<0||(f>S&&([f,S]=[S,f]),f===t&&f++,S===t&&S--,f>S))return a;const d=y=>ee(this.sheets[y].name);return f===S?`${d(f)}!`:`${d(f)}:${d(S)}!`}),o=r=>r.includes(":")&&r.includes("!")?r.split(/("(?:[^"]|"")*")/).map((a,h)=>h%2?a:s(a)).join(""):r;for(const r of this.sheets){const a=new Map;r.formulas.forEachCell((h,u,c)=>{let g=a.get(c);if(g===void 0){const p=c.f??c.sf,f=typeof p=="string"?o(p):p;g=f===p?null:c.f!==void 0?{...c,f,tpl:void 0}:{...c,sf:f,tpl:void 0},a.set(c,g)}g!==null&&r.setFormula(h,u,g)})}for(const r of this.engine?.names.values()??[])typeof r.text=="string"&&(r.text=o(r.text));this.engine?.templates.clear()}_refErrorsFor(t){const e=q(this.sheets[t].name),n=s=>s.split(/("(?:[^"]|"")*")/).map((o,r)=>r%2?o:o.replace(e,"#REF!")).join(""),l=s=>typeof s=="string"&&s!==""?n(s):s,i=this.sheets[t];for(const s of this.sheets){if(s===i)continue;s.cf?.length>0&&(s.cf=s.cf.map(r=>({...r,rules:r.rules.map(a=>oe(a,l))})),s.cfChanged=!0,s.cfEval?.invalidate()),s.validations?.length>0&&(s.validations=s.validations.map(r=>({...r,formula1:l(r.formula1),formula2:l(r.formula2)})),s.validationsChanged=!0);const o=new Map;s.formulas.forEachCell((r,a,h)=>{let u=o.get(h);if(u===void 0){const c=h.f??h.sf,g=typeof c=="string"?n(c):c;u=g===c?null:h.f!==void 0?{...h,f:g,tpl:void 0}:{...h,sf:g,tpl:void 0},o.set(h,u)}u!==null&&(s.setFormula(r,a,u),this.formulasEdited=!0)})}for(const s of this.engine?.names.values()??[])typeof s.text=="string"&&(s.text=n(s.text));this.engine?.templates.clear()}_renameInFormulas(t,e){const n=q(t);let l=!1;for(const i of this.sheets){const s=new Map;i.formulas.forEachCell((o,r,a)=>{let h=s.get(a);if(h===void 0){const u=a.f??a.sf;if(u===void 0||!n.test(u))n.lastIndex=0,h=null;else{n.lastIndex=0;const c=D(u,t,e);h=a.f!==void 0?{...a,f:c,tpl:void 0}:{...a,sf:c,tpl:void 0}}s.set(a,h)}h!==null&&(i.setFormula(o,r,h),l=!0)})}l&&(this.formulasEdited=!0,this.engine?.templates.clear(),this.engine?.rebuild())}editText(t,e,n){const l=t.kind(e,n);if(this.engine?.dataTables.length){const o=this.engine.dataTableAt(t,e,n);if(o){const{info:r}=o,a=r.dt2D==="1",h=r.dtr==="1";return`{=TABLE(${a||h?r.r1:""},${a?r.r2:h?"":r.r1})}`}}if(l&X&&this.engine){const o=this.engine.spillMaster(t,e,n);if(o)return this.editText(t,o.row,o.col)}if(l&v){const o=t.formula(e,n);if(o&&o.f!==void 0){const r="="+K(o.f);return o.t==="array"&&!o.cm?`{${r}}`:r}if(o&&o.sf!==void 0){const r=Math.floor(o.base/w),a=o.base-r*w;return"="+K(se(o.sf,e-r,n-a))}}const i=l&Y;if(i===_)return"";const s=t.value(e,n);if(i===F&&t.valueMeta!==null&&!(l&v)){const o=this.richImages?.get(t.valueMeta.get(e*w+n));if(o!==void 0)return o.alt??"";const r=this.richValues?.indexOfVm(t.valueMeta.get(e*w+n))??-1,a=r>=0?this.richValues.view(r):null;if(a!==null)return a.text}return i===R?this.styles.get(t.style(e,n)).quotePrefix?`'${this.strings.get(s)}`:this.strings.get(s):i===I?s?"TRUE":"FALSE":i===F?Z[s]??"#N/A":String(s)}transact(t,e){if(this.guard&&!this.guard(t))return;const n=this.selectedSheets,l=n!==null&&n.size>1&&ue.has(t)?this._runGroup(t,e,n):this._runOn(this.sheet,t,e);l!==null&&this._pushUndo(l)}transactOn(t,e,n){const l=this._runOn(t,e,n);l!==null&&this._pushUndo(l)}_runOn(t,e,n){const l=new b,i={sheet:t,log:l,saveCell:(h,u)=>l.push(t,h,u)},s=n(i)??null;e!=="entry"&&this.engine?.presentationChanged(),t.changes!==null&&l.count>0&&V(this,t,l),l.vms!==null&&de(t,l);const o=l.count>0&&this.engine?.tables.size>0?J(this,i,l):null,r=(h,u)=>c=>r(h(c),u(c)),a=s&&o?r(s,o):s??o;return l.count===0&&a===null?null:(t.revision++,this.engine?.cellsChanged(t,l.rows,l.cols,l.count),{sheetIndex:t===this.sheet?this.activeSheet:this.sheets.indexOf(t),sheet:t,log:l,label:e,extra:a})}_runGroup(t,e,n){const l=[];for(const i of this.sheets){if(!n.has(i)||i.protection&&i!==this.sheet)continue;const s=this._runOn(i,t,e);s!==null&&l.push(s)}return l.length===0?null:{sheetIndex:this.activeSheet,sheet:this.sheet,log:new b,label:t,extra:O(this,l)}}runSlice(t,e,n){const l=this._runOn(t,e,n);return l!==null&&this._emit("cells"),l}pushFolded(t,e,n){t.length!==0&&this._pushUndo({sheetIndex:this.sheets.indexOf(n),sheet:n,log:new b,label:e,extra:O(this,t)})}rollbackSlices(t){for(let e=t.length-1;e>=0;e--)this._restore(t[e]);this._emit("cells")}_pushUndo(t){this.allowUndo===!1?this.undoStack.length=0:(this.undoStack.push(t),this.undoStack.length>(this.historyLimit??ce)&&this.undoStack.shift()),this.redoStack.length=0,this._emit("cells")}_restore(t){const e=t.sheet??this.sheets[t.sheetIndex],{log:n}=t,l=new b;for(let o=n.count-1;o>=0;o--){const r=n.rows[o],a=n.cols[o];l.push(e,r,a),e.setCell(r,a,n.kinds[o],n.values[o]),e.setStyle(r,a,n.styles[o]),e.setFormula(r,a,n.kinds[o]&v?n.formulas.get(o):null)}if(n.vms!==null){e.valueMeta=new Map(e.valueMeta??[]);for(const[o,r]of n.vms)e.valueMeta.set(n.rows[o]*w+n.cols[o],r)}let i=null;t.extra&&(i=t.extra(e)),this.engine?.presentationChanged(),e.changes!==null&&n.count>0&&V(this,e,l),e.revision++,this.engine?.cellsChanged(e,n.rows,n.cols,n.count);const s=this.sheets.indexOf(e);return s>=0&&(this.activeSheet=s),{sheetIndex:s>=0?s:t.sheetIndex,sheet:e,log:l,label:t.label,extra:i}}undo(){const t=this.undoStack.pop();return t?(this.redoStack.push(this._restore(t)),this._emit("cells"),!0):!1}redo(){const t=this.redoStack.pop();return t?(this.undoStack.push(this._restore(t)),this._emit("cells"),!0):!1}setInput(t,e,n,l){const{sheet:i}=t;this.recorder?.push(`sheet.setValue(${JSON.stringify(Q(e,n))}, ${JSON.stringify(l)});`),t.saveCell(e,n),i.setFormula(e,n,null);const s=i.cellStyle(e,n),r=l!==""&&this.styles.get(i.style(e,n)).numFmt==="@"?{kind:R,text:l[0]==="'"?l.slice(1):l}:H(l),a=l[0]==="'"&&l.length>1;switch((a||s!==0&&this.styles.get(s).quotePrefix)&&i.setStyle(e,n,this.styles.derive(s,{quotePrefix:a})),r.kind){case _:i.setCell(e,n,_,0);break;case P:i.setCell(e,n,P,r.value),r.format&&(i.cellStyle(e,n)===0||this.styles.get(i.cellStyle(e,n)).numFmt==="General")&&i.setStyle(e,n,this.styles.derive(i.cellStyle(e,n),{numFmt:r.format}));break;case I:i.setCell(e,n,I,r.value);break;case F:i.setCell(e,n,F,r.value);break;case v:i.setCell(e,n,v|_,0),i.setFormula(e,n,{f:he(this._expandTableRefs(j(r.formula,this.engine?.tables),i,e,n),this.engine?.names??null),typed:this.dynamicArrays!==!1}),this.formulasEdited=!0;break;default:i.setCell(e,n,R,this.strings.add(r.text))}}setRichInput(t,e,n,l,i){const{sheet:s}=t;t.saveCell(e,n),s.setFormula(e,n,null),s.setCell(e,n,R,this.strings.addRich(l,i))}_expandTableRefs(t,e,n,l){const i=this.engine;return!i||i.tables.size===0?t:ae(t,()=>{for(const s of i.tables.values())if(s.sheet===e&&n>=s.r0&&n<=s.r1&&l>=s.c0&&l<=s.c1)return s;return null})}clearRange(t,e="contents",{visibleOnly:n=!1}={}){this.sheet.dataRanges?.cleared(t)||this.transact("clear",l=>{const{sheet:i}=l,s=[];if(e!=="contents")for(const{r0:r,c0:a,r1:h,c1:u}of t){const c=r===0&&h===B-1,g=a===0&&u===w-1,p=(f,S,d,y)=>{for(const[C,k]of[...S])C<d||C>y||(s.push([f,C,k]),S.delete(C))};c&&i.colStyles!==null&&p("col",i.colStyles,a,u),g&&i.rowStyles!==null&&p("row",i.rowStyles,r,h)}const o=n&&i.filteredRows?.size>0?i.filteredRows:null;for(const{r0:r,c0:a,r1:h,c1:u}of t){const c=Math.min(u,i.maxCol),g=Math.min(h,i.maxRow);for(let p=a;p<=c;p++)if(i.columns[p]!==void 0)for(let f=r;f<=g;f++){if(i._chunk(f,p)===void 0){f|=1023;continue}if(o!==null&&o.has(f))continue;const d=i.kind(f,p)!==0,y=i.cellStyle(f,p)!==0;if(e==="formats"){if(!y)continue;l.saveCell(f,p),i.setStyle(f,p,0);continue}!d&&!(e==="all"&&y)||(l.saveCell(f,p),i.setCell(f,p,_,0),i.setFormula(f,p,null),e==="all"&&i.setStyle(f,p,0))}}return s.length>0?E([],s):void 0})}applyStyle(t,e){const n=JSON.stringify(e);if(this.recorder)for(const l of t)this.recorder.push(`sheet.setFormat(${JSON.stringify(te(l))}, ${n});`);this.transact("format",l=>{const{sheet:i}=l;this.strings.rich!==null&&le(this,l,t,e);const s=[],o=new Set,r=[];for(const{r0:a,c0:h,r1:u,c1:c}of t){const g=a===0&&u===B-1,p=h===0&&c===w-1;if(g){i.colStyles===null&&(i.colStyles=new Map);for(let d=h;d<=c;d++){const y=i.colStyles.get(d);r.push(["col",d,y]),i.colStyles.set(d,this.styles.derive(y??0,e,n))}if(p&&i.rowStyles!==null)for(const[d,y]of i.rowStyles)r.push(["row",d,y]),i.rowStyles.set(d,this.styles.derive(y,e,n))}else if(p){i.rowStyles===null&&(i.rowStyles=new Map);for(let d=a;d<=u;d++){const y=i.rowStyles.get(d);r.push(["row",d,y]),i.rowStyles.set(d,this.styles.derive(y??0,e,n))}}const f=g?i.maxRow:u,S=p?i.maxCol:c;for(let d=h;d<=S;d++)for(let y=a>>>A;y<=f>>>A;y++){const C=y<<A,k=Math.max(a,C),L=Math.min(f,C+W);if(k>L)continue;const $=i._ensure(k,d),U=d*2048+y;o.has(U)||(o.add(U),s.push(d,y,$.styles===null?null:$.styles.slice()));for(let M=k;M<=L;M++)i.setStyle(M,d,this.styles.derive(i.style(M,d),e,n))}}return E(s,r)})}}function O(m,t){const e=()=>{const n=[];for(let l=t.length-1;l>=0;l--)n.push(m._restore(t[l]));return O(m,n)};return e.parts=t,e}function de(m,t){let e=null;for(const n of t.vms.keys()){const l=t.rows[n],i=t.cols[n];(m.kind(l,i)&~(N|z))===t.kinds[n]&&m.value(l,i)===t.values[n]||(e??=new Map(m.valueMeta),e.delete(l*w+i))}e!==null&&(m.valueMeta=e.size>0?e:null)}function x(m){if(m===null||typeof m!="object")return m;if(Array.isArray(m))return m.map(x);if(m instanceof Map)return new Map([...m].map(([e,n])=>[e,x(n)]));if(m instanceof Set)return new Set([...m].map(x));if(ArrayBuffer.isView(m))return m.slice();const t={};for(const[e,n]of Object.entries(m))!e.startsWith("_")&&typeof n!="function"&&(t[e]=x(n));return t}function E(m,t){return e=>{const n=[];for(let i=0;i<m.length;i+=3){const s=m[i],o=m[i+1],r=e.columns[s]?.[o];r!==void 0&&(n.push(s,o,r.styles),r.styles=m[i+2])}const l=[];for(const[i,s,o]of t){const r=i==="col"?"colStyles":"rowStyles",a=e[r]??=new Map;l.push([i,s,a.get(s)]),o===void 0?a.delete(s):a.set(s,o)}return E(n,l)}}export{b as CellLog,ue as GROUP_LABELS,be as Workbook,O as foldEntries,H as parseInput,E as swapStyles};
1
+ import{expandBareTables as ue}from"../slicers/structured.js";import{syncTableHeaders as fe,tablesOf as de}from"./tables.js";import{BOOL as D,CHUNK_BITS as z,CHUNK_MASK as K,EMPTY as T,ERR as U,ERRORS as me,FORMULA as F,DIRTY as W,EVALUATING as J,KIND_MASK as Z,NUM as P,Sheet as B,SPILL as pe,STR as A}from"./sheet.js";import{cellRef as ge,isValidName as ye,MAX_COLS as b,MAX_ROWS as Y,quoteSheetName as X,rangeRef as Se,renameSheetInFormula as Q,sheetReferencePattern as ee}from"./address.js";import{translateFormula as we}from"./formula-refs.js";import{sheetStem as te}from"./default-names.js";import{StringTable as Ce}from"./strings.js";import{formatAxis as se,formatSheetColumns as ve}from"./axis-styles.js";import{autoRowHeight as ke}from"./row-fit.js";import{StyleTable as xe}from"./styles.js";import{cellEntryText as ne,parseInput as ie}from"./input.js";import{noteChanges as oe}from"./changes.js";import{overrideRuns as _e}from"../cells/rich-runs.js";import{sheetRenameHooks as be}from"./sparse.js";import{shiftRuleFormulas as re}from"../cells/cf-file.js";import{expandStructured as Re,formulaEntryLimit as Me,FormulaSyntaxError as L,tokenize as Fe,toDisplayFormula as le,toFileFormula as Ae}from"../calc/parser.js";const Ie=200,Ee=new Set(["entry","clear","format","borders","fill"]);class Te{constructor(){this.ids=new Int32Array(1024),this.list=[]}set(t,e){if(t>=this.ids.length){let i=this.ids.length*2;for(;i<=t;)i*=2;const r=new Int32Array(i);r.set(this.ids),this.ids=r}const n=this.list.length;(n===0||this.list[n-1]!==e)&&this.list.push(e),this.ids[t]=this.list.length}get(t){const e=t<this.ids.length?this.ids[t]:0;return e===0?void 0:this.list[e-1]}}class ${constructor(){this.capacity=1024,this.count=0,this.rows=new Uint32Array(this.capacity),this.cols=new Uint16Array(this.capacity),this.kinds=new Uint8Array(this.capacity),this.styles=new Uint16Array(this.capacity),this.values=new Float64Array(this.capacity),this.formulas=null,this.vms=null}_grow(){const t=this.capacity*2,e=(n,i)=>{const r=new n(t);return r.set(i),r};this.rows=e(Uint32Array,this.rows),this.cols=e(Uint16Array,this.cols),this.kinds=e(Uint8Array,this.kinds),this.styles=e(Uint16Array,this.styles),this.values=e(Float64Array,this.values),this.capacity=t}push(t,e,n){this.count===this.capacity&&this._grow();const i=this.count++,r=t.kind(e,n)&~(W|J);if(this.rows[i]=e,this.cols[i]=n,this.kinds[i]=r,this.styles[i]=t.cellStyle(e,n),this.values[i]=t.value(e,n),r&F&&(this.formulas===null&&(this.formulas=new Te),this.formulas.set(i,t.formula(e,n))),t.valueMeta!==null){const s=t.valueMeta.get(e*b+n);s!==void 0&&(this.vms??=new Map).set(i,s)}}}class Ye{constructor(){this.sheets=[new B(`${te()}1`)],this.activeSheet=0,this.strings=new Ce,this.styles=new xe,this.undoStack=[],this.redoStack=[],this.listeners=new Set,this.source=null,this.formulasEdited=!1,this.edits=0,this._dirty=!1,this.engine=null,this.selectedSheets=null}get sheet(){return this.sheets[this.activeSheet]}get dirty(){return this._dirty}set dirty(t){t&&this.edits++,this._dirty=!!t}onChange(t){this.listeners.add(t)}_emit(t){this.dirty=!0;for(const e of this.listeners)e(t)}addSheet(t){const e=te();let n=1,i=t??`${e}${n}`;const r=o=>this.sheets.some(l=>l.name.toLowerCase()===o.toLowerCase());for(;r(i);)i=`${e}${++n}`;const s=new B(i);return this.sheets.push(s),this.activeSheet=this.sheets.length-1,this.engine?.sheetsChanged(),this._emit("sheets"),s}deleteSheet(t){return this.sheets.filter(e=>!e.hidden).length<=1?!1:(this._shrink3D(t),this._refErrorsFor(t),this._removeSheet(t))}_removeSheet(t){if(this.sheets.length<=1)return!1;const[e]=this.sheets.splice(t,1);if(this.selectedSheets?.delete(e),this._forgetSheet(e,t),this.engine)for(const[i,r]of this.engine.tables)r.sheet===e&&this.engine.tables.delete(i);this._reindexNames(i=>i===t?-1:i>t?i-1:i),this.activeSheet>=this.sheets.length&&(this.activeSheet=this.sheets.length-1);const n=this.activeSheet;for(;this.sheets[this.activeSheet].hidden&&(this.activeSheet=(this.activeSheet+1)%this.sheets.length,this.activeSheet!==n););return this.structureEdited=!0,this.engine?.sheetsChanged(),this.engine?.rebuild(),this._emit("sheets"),!0}_forgetSheet(t,e){const n=r=>r.sheet===t||r.sheet===void 0&&r.sheetIndex===e||(r.extra?.parts??[]).some(n),i=r=>{r.sheetIndex>e&&r.sheetIndex--;for(const s of r.extra?.parts??[])i(s)};for(const r of[this.undoStack,this.redoStack]){const s=r.filter(o=>!n(o));s.forEach(i),r.splice(0,r.length,...s)}}moveSheet(t,e){if(t===e)return;const n=this.sheet,[i]=this.sheets.splice(t,1);this.sheets.splice(e,0,i),this.activeSheet=this.sheets.indexOf(n);const r=o=>o===t?e:t<e?o>t&&o<=e?o-1:o:o>=e&&o<t?o+1:o,s=o=>{o.sheetIndex=r(o.sheetIndex);for(const l of o.extra?.parts??[])s(l)};for(const o of this.undoStack)s(o);for(const o of this.redoStack)s(o);this.structureEdited=!0,this._reindexNames(r),this.engine?.orderChanged(),this._emit("sheets")}_copyTables(t,e){const n=new Map,i=new Set;for(const r of de(this,t)){const{sheet:s,part:o,id:l,source:a,sheetIndex:c,...h}=r;let u=this.engine.tables.size+n.size+2,p,f;do{const d=String(u++),g=r.name.slice(0,255-d.length);p=g+d,ye(p)||(p=g.slice(0,254-d.length)+"_"+d),f=p.toUpperCase()}while(i.has(f)||this.engine.tables.has(f)||this.engine.names.has(f));i.add(f),n.set(r.name.toUpperCase(),{...I(h),name:p,sheet:e,isNew:!0,dirty:!0})}return n}_copyScopedNames(t,e,n,i){const{engine:r}=this;if(!r||r.names.size===0)return;const s=`${t}!`;let o=!1;for(const[l,a]of[...r.names])l.startsWith(s)&&(r.names.set(`${t+1}!${l.slice(s.length)}`,{...a,text:i(Q(a.text,e.name,n.name))}),o=!0);o&&(this.namesChanged=!0)}_reindexNames(t){const{engine:e}=this;if(!e||e.names.size===0)return;const n=[];let i=!1;for(const[r,s]of e.names){const o=r.indexOf("!"),l=o<0?0:t(Number(r.slice(0,o)));o>=0&&l!==Number(r.slice(0,o))&&(i=!0),o<0?n.push([r,s]):l>=0&&n.push([`${l}!${r.slice(o+1)}`,s])}if(i){e.names.clear();for(const[r,s]of n)e.names.set(r,s);e.nameTemplates.clear(),this.namesChanged=!0,e.rebuild()}}duplicateSheet(t){const e=this.sheets[t];let i=`${e.name} (2)`,r=2;for(;this.sheets.some(h=>h.name.toLowerCase()===i.toLowerCase());)i=`${e.name} (${++r})`;const s=new B(i),o=this._copyTables(e,s),l=h=>ze(h,o,e.name,s.name);for(const h of o.values())for(const u of h.totals??[])u?.formula&&(u.formula=l(u.formula));e.columns.forEach((h,u)=>{const p=[];h.forEach((f,d)=>{p[d]={kinds:f.kinds.slice(),values:f.values.slice(),styles:f.styles?f.styles.slice():null}}),s.columns[u]=p}),s.maxRow=e.maxRow,s.maxCol=e.maxCol,s.rows=e.rows.shifted(0,0),s.cols=e.cols.shifted(0,0);const a=new Map;e.formulas.forEachCell((h,u,p)=>{let f=a.get(p);f===void 0&&(f={...p,tpl:void 0},p.f!==void 0&&(f.f=l(p.f)),p.sf!==void 0&&(f.sf=l(p.sf)),a.set(p,f)),s.setFormula(h,u,f)}),s.merges=e.merges.map(h=>({...h})),s.rowStyles=e.rowStyles?new Map(e.rowStyles):null,s.colStyles=e.colStyles?new Map(e.colStyles):null,s.frozenRows=e.frozenRows,s.frozenCols=e.frozenCols,s.showGridlines=e.showGridlines,s.cellTypes=e.cellTypes?new Map(e.cellTypes):null,s.sparkRules=e.sparkRules,s.cellDropdowns=e.cellDropdowns?new Map(e.cellDropdowns):null,s.cellTags=e.cellTags?new Map(e.cellTags):null,s.cellButtons=e.cellButtons?new Map(e.cellButtons):null,s.cellStates=e.cellStates?e.cellStates.slice():null,s.autoMerges=e.autoMerges?e.autoMerges.map(h=>({...h})):null,s.background=e.background,s.outlineColumn=e.outlineColumn?{...e.outlineColumn}:null,s.frozenTrailingRows=e.frozenTrailingRows,s.frozenTrailingCols=e.frozenTrailingCols,s.hideZeros=e.hideZeros,s.hideHeadings=e.hideHeadings,s.isChartSheet=e.isChartSheet,s.hideOutline=e.hideOutline,s.gridColorId=e.gridColorId,s.areaOffset=e.areaOffset,s.selectionPolicy=e.selectionPolicy,s.selectionUnit=e.selectionUnit,s.gridlineAxes=e.gridlineAxes,s.gridColor=e.gridColor,s.tabStyles=e.tabStyles;const c=I;if(s.cf=c(e.cf),s.validations=c(e.validations),o.size){for(const h of s.cf??[])h.rules=h.rules.map(u=>re(u,l));for(const h of s.validations??[])h.formula1&&(h.formula1=l(h.formula1)),h.formula2&&(h.formula2=l(h.formula2))}s.comments=c(e.comments),s.threads=c(e.threads),s.hyperlinks=e.hyperlinks?e.hyperlinks.map(h=>({...c({...h,_ranges:void 0})})):e.hyperlinks,s.autoFilter=c(e.autoFilter),s.filteredRows=e.filteredRows?new Set(e.filteredRows):e.filteredRows,s.pageSetup=c(e.pageSetup),s.tabColor=e.tabColor,s.protection=c(e.protection),s.rtl=e.rtl,s.zoom=e.zoom,s.valueMeta=e.valueMeta?new Map(e.valueMeta):null,s.colsChanged=!0,s.viewChanged=!0,this.sheets.splice(t+1,0,s);for(const h of o.values())this.engine.tables.set(h.name.toUpperCase(),h);return this.activeSheet=t+1,this._reindexNames(h=>h>t?h+1:h),this._copyScopedNames(t,e,s,l),this.structureEdited=!0,this.engine?.sheetsChanged(),this.engine?.rebuild(),this._emit("sheets"),s}sheetNameProblem(t,e){const n=e.trim().slice(0,31);return!n||/[\\/?*[\]:]/.test(n)||/^'|'$/.test(n)?"rules":n.toLowerCase()==="history"?"reserved":this.sheets.some((i,r)=>r!==t&&i.name.toLowerCase()===n.toLowerCase())?"taken":null}renameSheet(t,e){const n=e.trim().slice(0,31);if(this.sheetNameProblem(t,e)!==null)return!1;const i=this.sheets[t].name;this.sheets[t].name=n,this.engine?.sheetsChanged(),this._renameInFormulas(i,n);for(const r of be)r(this,i,n);return this._emit("sheets"),!0}_shrink3D(t){const e=this.sheets[t].name.toLowerCase(),n=l=>this.sheets.findIndex(a=>a.name.toLowerCase()===l.toLowerCase()),i=l=>l[0]==="'"?l.slice(1,-1).replace(/''/g,"'"):l,r=/((?:'(?:[^']|'')+'|[A-Za-z_\u00C0-\uFFFF][\w.\u00C0-\uFFFF]*)):((?:'(?:[^']|'')+'|[A-Za-z_\u00C0-\uFFFF][\w.\u00C0-\uFFFF]*))!/g,s=l=>l.replace(r,(a,c,h,u)=>{if(l[u-1]==="]")return a;const p=i(c),f=i(h);if(p.toLowerCase()!==e&&f.toLowerCase()!==e)return a;let d=n(p),g=n(f);if(d<0||g<0||(d>g&&([d,g]=[g,d]),d===t&&d++,g===t&&g--,d>g))return a;const y=w=>X(this.sheets[w].name);return d===g?`${y(d)}!`:`${y(d)}:${y(g)}!`}),o=l=>l.includes(":")&&l.includes("!")?l.split(/("(?:[^"]|"")*")/).map((a,c)=>c%2?a:s(a)).join(""):l;for(const l of this.sheets){const a=new Map;l.formulas.forEachCell((c,h,u)=>{let p=a.get(u);if(p===void 0){const f=u.f??u.sf,d=typeof f=="string"?o(f):f;p=d===f?null:u.f!==void 0?{...u,f:d,tpl:void 0}:{...u,sf:d,tpl:void 0},a.set(u,p)}p!==null&&l.setFormula(c,h,p)})}for(const l of this.engine?.names.values()??[])typeof l.text=="string"&&(l.text=o(l.text));this.engine?.templates.clear()}_refErrorsFor(t){const e=ee(this.sheets[t].name),n=s=>s.split(/("(?:[^"]|"")*")/).map((o,l)=>l%2?o:o.replace(e,"#REF!")).join(""),i=s=>typeof s=="string"&&s!==""?n(s):s,r=this.sheets[t];for(const s of this.sheets){if(s===r)continue;s.cf?.length>0&&(s.cf=s.cf.map(l=>({...l,rules:l.rules.map(a=>re(a,i))})),s.cfChanged=!0,s.cfEval?.invalidate()),s.validations?.length>0&&(s.validations=s.validations.map(l=>({...l,formula1:i(l.formula1),formula2:i(l.formula2)})),s.validationsChanged=!0);const o=new Map;s.formulas.forEachCell((l,a,c)=>{let h=o.get(c);if(h===void 0){const u=c.f??c.sf,p=typeof u=="string"?n(u):u;h=p===u?null:c.f!==void 0?{...c,f:p,tpl:void 0}:{...c,sf:p,tpl:void 0},o.set(c,h)}h!==null&&(s.setFormula(l,a,h),this.formulasEdited=!0)})}for(const s of this.engine?.names.values()??[])typeof s.text=="string"&&(s.text=n(s.text));this.engine?.templates.clear()}_renameInFormulas(t,e){const n=ee(t);let i=!1;for(const r of this.sheets){const s=new Map;r.formulas.forEachCell((o,l,a)=>{let c=s.get(a);if(c===void 0){const h=a.f??a.sf;if(h===void 0||!n.test(h))n.lastIndex=0,c=null;else{n.lastIndex=0;const u=Q(h,t,e);c=a.f!==void 0?{...a,f:u,tpl:void 0}:{...a,sf:u,tpl:void 0}}s.set(a,c)}c!==null&&(r.setFormula(o,l,c),i=!0)})}i&&(this.formulasEdited=!0,this.engine?.templates.clear(),this.engine?.rebuild())}editText(t,e,n){const i=t.kind(e,n);if(this.engine?.dataTables.length){const o=this.engine.dataTableAt(t,e,n);if(o){const{info:l}=o,a=l.dt2D==="1",c=l.dtr==="1";return`{=TABLE(${a||c?l.r1:""},${a?l.r2:c?"":l.r1})}`}}if(i&pe&&this.engine){const o=this.engine.spillMaster(t,e,n);if(o)return this.editText(t,o.row,o.col)}if(i&F){const o=t.formula(e,n),l=o&&!o.typed&&o.t!=="array"&&o.tpl?.rangeResult?"=@":"=";if(o&&o.f!==void 0){const a=l+le(o.f);return o.t==="array"&&!o.cm?`{${a}}`:a}if(o&&o.sf!==void 0){const a=Math.floor(o.base/b),c=o.base-a*b;return l+le(we(o.sf,e-a,n-c))}}const r=i&Z;if(r===T)return"";const s=t.value(e,n);if(r===U&&t.valueMeta!==null&&!(i&F)){const o=this.richImages?.get(t.valueMeta.get(e*b+n));if(o!==void 0)return o.alt??"";const l=this.richValues?.indexOfVm(t.valueMeta.get(e*b+n))??-1,a=l>=0?this.richValues.view(l):null;if(a!==null)return a.text}return r===A?this.styles.get(t.style(e,n)).quotePrefix?`'${this.strings.get(s)}`:this.strings.get(s):r===D?s?"TRUE":"FALSE":r===U?me[s]??"#N/A":String(s)}transact(t,e,n=null){if(this.guard&&!this.guard(t))return;const i=this.selectedSheets;!n&&i?.size>1&&Ee.has(t)&&(n=this.sheets.filter(s=>i.has(s)&&(!s.protection||s===this.sheet)));const r=n?this._runGroup(t,e,n):this._runOn(this.sheet,t,e);r!==null&&this._pushUndo(r)}transactOn(t,e,n){const i=this._runOn(t,e,n);i!==null&&this._pushUndo(i)}_runOn(t,e,n){const i=new $,r={sheet:t,log:i,saveCell:(f,d)=>i.push(t,f,d)},{maxRow:s,maxCol:o}=t,l=this.recorder?.length,a=this.formulasEdited;let c;try{c=n(r)??null}catch(f){throw f instanceof L&&(ae(t,i),t.maxRow=s,t.maxCol=o,this.formulasEdited=a,l!==void 0&&(this.recorder.length=l),this.engine?.cellsChanged(t,i.rows,i.cols,i.count)),f}e!=="entry"&&this.engine?.presentationChanged(),t.changes!==null&&i.count>0&&oe(this,t,i),i.vms!==null&&Oe(t,i);const h=i.count>0&&this.engine?.tables.size>0?fe(this,r,i):null,u=(f,d)=>g=>u(f(g),d(g)),p=c&&h?u(c,h):c??h;return i.count===0&&p===null?null:(t.revision++,this.engine?.cellsChanged(t,i.rows,i.cols,i.count),{sheetIndex:t===this.sheet?this.activeSheet:this.sheets.indexOf(t),sheet:t,log:i,label:e,extra:p})}_runGroup(t,e,n){const i=[],r=this.activeSheet,s=this.recorder?.length,o=this.formulasEdited,l=[];try{for(const a of n){const{maxRow:c,maxCol:h}=a,u=this._runOn(a,t,e);u!==null&&(i.push(u),l.push([a,c,h]))}}catch(a){if(a instanceof L){this.rollbackSlices(i);for(const[c,h,u]of l)c.maxRow=h,c.maxCol=u;this.activeSheet=r,this.formulasEdited=o,s!==void 0&&(this.recorder.length=s)}throw a}return i.length===0?null:{sheetIndex:this.activeSheet,sheet:this.sheet,log:new $,label:t,extra:H(this,i)}}runSlice(t,e,n){const i=this._runOn(t,e,n);return i!==null&&this._emit("cells"),i}pushFolded(t,e,n){t.length!==0&&this._pushUndo({sheetIndex:this.sheets.indexOf(n),sheet:n,log:new $,label:e,extra:H(this,t)})}rollbackSlices(t){for(let e=t.length-1;e>=0;e--)this._restore(t[e]);this._emit("cells")}_pushUndo(t){this.allowUndo===!1?this.undoStack.length=0:(this.undoStack.push(t),this.undoStack.length>(this.historyLimit??Ie)&&this.undoStack.shift()),this.redoStack.length=0,this._emit("cells")}_restore(t){const e=t.sheet??this.sheets[t.sheetIndex],{log:n}=t,i=new $;if(ae(e,n,i),n.vms!==null){e.valueMeta=new Map(e.valueMeta??[]);for(const[o,l]of n.vms)e.valueMeta.set(n.rows[o]*b+n.cols[o],l)}let r=null;t.extra&&(r=t.extra(e)),this.engine?.presentationChanged(),e.changes!==null&&n.count>0&&oe(this,e,i),e.revision++,this.engine?.cellsChanged(e,n.rows,n.cols,n.count);const s=this.sheets.indexOf(e);return s>=0&&(this.activeSheet=s),{sheetIndex:s>=0?s:t.sheetIndex,sheet:e,log:i,label:t.label,extra:r}}undo(){const t=this.undoStack.pop();return t?(this.redoStack.push(this._restore(t)),this._emit("cells"),!0):!1}redo(){const t=this.redoStack.pop();return t?(this.undoStack.push(this._restore(t)),this._emit("cells"),!0):!1}setInput(t,e,n,i,{overLimitAsText:r=!1}={}){const{sheet:s}=t,o=s.style(e,n),l=i!==""&&this.styles.get(o).numFmt==="@";let a=l?{kind:A,text:i[0]==="'"?i.slice(1):i}:ie(i);if(a.kind===F){const h=Me(a.formula);if(h){if(!r)throw new L(h);a={kind:A,text:i}}}if(this.recorder){const h=a.kind===A&&i.startsWith("=")&&!l?`'${i}`:i;this.recorder.push(`sheet.setValue(${JSON.stringify(ge(e,n))}, ${JSON.stringify(h)});`)}t.saveCell(e,n),s.setFormula(e,n,null);const c=i[0]==="'"&&i.length>1;switch((c||this.styles.get(o).quotePrefix)&&s.setStyle(e,n,this.styles.derive(o,{quotePrefix:c})),a.kind){case T:s.setCell(e,n,T,0);break;case P:s.setCell(e,n,P,a.value),a.format&&this.styles.get(o).numFmt==="General"&&s.setStyle(e,n,this.styles.derive(s.style(e,n),{numFmt:a.format}));break;case D:s.setCell(e,n,D,a.value);break;case U:s.setCell(e,n,U,a.value);break;case F:s.setCell(e,n,F|T,0),s.setFormula(e,n,{f:Ae(this._expandTableRefs(ue(a.formula,this.engine?.tables),s,e,n),this.engine?.names??null),typed:this.dynamicArrays!==!1}),this.formulasEdited=!0;break;default:s.setCell(e,n,A,this.strings.add(ne(a.text)))}}setRichInput(t,e,n,i,r){const{sheet:s}=t;t.saveCell(e,n),s.setFormula(e,n,null);const o=ne(i);s.setCell(e,n,A,this.strings.addRich(o,o===i?r:r?.filter(l=>l.at<o.length)))}_expandTableRefs(t,e,n,i){const r=this.engine;return!r||r.tables.size===0?t:Re(t,()=>{for(const s of r.tables.values())if(s.sheet===e&&n>=s.r0&&n<=s.r1&&i>=s.c0&&i<=s.c1)return s;return null})}clearRange(t,e="contents",{visibleOnly:n=!1}={}){this.sheet.dataRanges?.cleared(t)||this.transact("clear",i=>{const{sheet:r}=i,s=[];if(e!=="contents")for(const{r0:l,c0:a,r1:c,c1:h}of t){const u=l===0&&c===Y-1,p=a===0&&h===b-1,f=(d,g,y,w)=>{for(const[C,x]of[...g])C<y||C>w||(s.push([d,C,x]),g.delete(C))};u&&r.colStyles!==null&&f("col",r.colStyles,a,h),p&&r.rowStyles!==null&&f("row",r.rowStyles,l,c)}const o=n&&r.filteredRows?.size>0?r.filteredRows:null;for(const{r0:l,c0:a,r1:c,c1:h}of t){const u=Math.min(h,r.maxCol),p=Math.min(c,r.maxRow);for(let f=a;f<=u;f++)if(r.columns[f]!==void 0)for(let d=l;d<=p;d++){if(r._chunk(d,f)===void 0){d|=1023;continue}if(o!==null&&o.has(d))continue;const y=r.kind(d,f)!==0,w=r.cellStyle(d,f)!==0;if(e==="formats"){if(!w)continue;i.saveCell(d,f),r.setStyle(d,f,0);continue}!y&&!(e==="all"&&w)||(i.saveCell(d,f),r.setCell(d,f,T,0),r.setFormula(d,f,null),e==="all"&&r.setStyle(d,f,0))}}return s.length>0?V([],s):void 0})}applyStyle(t,e,{measureRow:n,numberFit:i}={}){const r=JSON.stringify(e);if(this.recorder)for(const s of t)this.recorder.push(`sheet.setFormat(${JSON.stringify(Se(s))}, ${r});`);this.transact("format",s=>{const{sheet:o}=s;this.strings.rich!==null&&_e(this,s,t,e);const l=[],a=new Set,c=[],h=[],u=(g,y,w)=>{const C=g*2048+y;a.has(C)||(a.add(C),l.push(g,y,w.styles===null?null:w.styles.slice()))},p=(g,y,w)=>{w===0&&(o.rowStyles?.has(g)||o.colStyles?.has(y))&&(w=this.styles.explicitDefault()),o.setStyle(g,y,w)};for(const{r0:g,c0:y,r1:w,c1:C}of t){const x=g===0&&w===Y-1,v=y===0&&C===b-1,k=x||v,R=k?Math.min(w,o.maxRow):w,he=k?Math.min(C,o.maxCol):C;for(let S=y;S<=he&&g<=R;S++){const _=o.columns[S];if(!(k&&!_))for(let M=g>>>z;M<=R>>>z;M++){const q=M<<z,G=Math.max(g,q),ce=Math.min(R,q+K),N=k?_[M]:o._ensure(G,S);if(!(k&&!N?.styles)){u(S,M,N);for(let E=G;E<=ce;E++)(!k||N.styles[E&K])&&p(E,S,this.styles.derive(o.style(E,S),e,r))}}}const O=S=>this.styles.derive(S,e,r),j=(S,_,M)=>{u(_,S>>>z,o._ensure(S,_)),o.setStyle(S,_,M||this.styles.explicitDefault())};if(v&&x){ve(o,O,c);for(const[S,_]of o.rowStyles??[])c.push(["row",S,_]),o.rowStyles.set(S,O(_))}else if(x)for(let S=y;S<=C;S++)se(o,"col",S,O,c,j);else if(v)for(let S=g;S<=w;S++)se(o,"row",S,O,c,j)}const f=(g,y,w)=>{const C=o[g];w!==C.sizeOf(y)&&(h.push([g,y,C.isCustom(y)?C.sizeOf(y):null,C.isAuto(y)]),C.setSize(y,w,!0),g==="cols"&&(o.colsChanged=!0))},d=e.fontSize!==void 0||e.wrap!==void 0||e.rotation!==void 0||["justify","distributed"].includes(e.hAlign)||["justify","distributed"].includes(e.vAlign);for(const{r0:g,r1:y,c0:w,c1:C}of t){const x=Math.min(y,o.maxRow);if(d&&x-g<=2e3)for(let v=g;v<=x;v++)f("rows",v,ke(o,this,v,n));if(i)for(const v of i.cols){if(v<w||v>C||!o.columns[v]||o.cols.isCustom(v)&&!o.cols.isAuto(v))continue;let k=o.cols.sizeOf(v);for(const R of i.rows)R>=g&&R<=x&&(o.kind(R,v)&Z)===P&&(k=Math.max(k,i.measure(o,R,v)));f("cols",v,k)}}return V(l,c,h)})}}function ae(m,t,e){for(let n=t.count-1;n>=0;n--){const i=t.rows[n],r=t.cols[n];e?.push(m,i,r),m.setCell(i,r,t.kinds[n],t.values[n]),m.setStyle(i,r,t.styles[n]),m.setFormula(i,r,t.kinds[n]&F?t.formulas.get(n):null)}}function H(m,t){const e=()=>{const n=[];for(let i=t.length-1;i>=0;i--)n.push(m._restore(t[i]));return H(m,n)};return e.parts=t,e}function Oe(m,t){let e=null;for(const n of t.vms.keys()){const i=t.rows[n],r=t.cols[n];(m.kind(i,r)&~(W|J))===t.kinds[n]&&m.value(i,r)===t.values[n]||(e??=new Map(m.valueMeta),e.delete(i*b+r))}e!==null&&(m.valueMeta=e.size>0?e:null)}function ze(m,t,e,n){if(!t.size||typeof m!="string")return m;let i;try{i=Fe(m)}catch(o){if(o instanceof L)return m;throw o}let r="",s=0;for(let o=0;o<i.length;o++){const l=i[o],a=l.t==="name"?m.slice(l.s,l.e):l.t==="table"?l.v.name:null;if(!a)continue;const c=t.get(a.toUpperCase());if(!c)continue;let h=o-1;for(;i[h]?.t==="ws";)h--;const u=i[h];if(u?.t==="sheet"){const{book:p,sheet:f,sheet2:d}=u.v;if(p!==null||d!==null)continue;if(f.toUpperCase()===e.toUpperCase())r+=m.slice(s,u.s)+X(n)+"!",s=u.e;else if(f.toUpperCase()!==n.toUpperCase())continue}r+=m.slice(s,l.s)+c.name,s=l.s+a.length}return s?r+m.slice(s):m}function I(m){if(m===null||typeof m!="object")return m;if(Array.isArray(m))return m.map(I);if(m instanceof Map)return new Map([...m].map(([e,n])=>[e,I(n)]));if(m instanceof Set)return new Set([...m].map(I));if(ArrayBuffer.isView(m))return m.slice();const t={};for(const[e,n]of Object.entries(m))!e.startsWith("_")&&typeof n!="function"&&(t[e]=I(n));return t}function V(m,t,e=[]){return n=>{const i=[];for(let o=0;o<m.length;o+=3){const l=m[o],a=m[o+1],c=n.columns[l]?.[a];c!==void 0&&(i.push(l,a,c.styles),c.styles=m[o+2])}const r=[];for(let o=t.length-1;o>=0;o--){const[l,a,c]=t[o];if(l==="columns"){r.push([l,a,n.colStyles]),n.colStyles=c,n.colsChanged=!0;continue}l==="col"&&(n.colsChanged=!0);const h=l==="col"?"colStyles":"rowStyles",u=n[h]??=new Map;r.push([l,a,u.get(a)]),c===void 0?u.delete(a):u.set(a,c)}const s=[];for(let o=e.length-1;o>=0;o--){const[l,a,c,h]=e[o],u=n[l];s.push([l,a,u.isCustom(a)?u.sizeOf(a):null,u.isAuto(a)]),u.setSize(a,c,h),l==="cols"&&(n.colsChanged=!0)}return V(i,r,s)}}export{$ as CellLog,Ee as GROUP_LABELS,Ye as Workbook,H as foldEntries,ie as parseInput,V as swapStyles};