├── .gitignore ├── LICENSE ├── README.md ├── example.mjs ├── examples └── my.xlsx ├── index.d.mts ├── index.d.ts ├── index.js ├── index.mjs ├── index.ts ├── my.xlsx ├── my.xlsx.png ├── package-lock.json ├── package.json ├── tsconfig.json ├── utils.ts ├── xlsx-dynamic ├── appxml.ts ├── corexml.ts ├── stylesxml.ts └── workbookxml.ts └── xlsx-static ├── _rels.xml ├── content-types.xml ├── index.ts ├── theme1.xml └── workbook-rels.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2024 George MacKerron 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xlsxtable 2 | 3 | A small, simple library to create nice `.xlsx` Excel files from tabular data, which: 4 | 5 | * Emboldens and (optionally) freezes and autofilters the headings 6 | * Sets column widths based on cell content 7 | * Converts dates and times to native Excel format (roughly: floating-point days since 0 Jan 1900) 8 | * Works everywhere, including Node and browsers 9 | 10 | Size: under 7KB gzipped. The only runtime dependency is [littlezipper](https://github.com/jawj/littlezipper) (which is by the same author, tiny, and has no runtime dependencies of its own). 11 | 12 | This library [powers `.xlsx` download in the Neon SQL Editor](https://neon.tech/blog/export-to-csv-json-and-xlsx-from-the-neon-console). 13 | 14 | 15 | ## How do you say _xlsxtable_? 16 | 17 | Pronunciation rhymes with [Hextable](https://en.wikipedia.org/wiki/Hextable), or is similar to vegetable. That is: _ex-el-ess-EX-tuh-bl_. 18 | 19 | 20 | ## Types 21 | 22 | Types are defined on a per-column basis. The library supports Excel numbers, strings, dates/times, and empty cells. 23 | 24 | Excel has no concept of time zones, so the date and time types have local and UTC variants. The local variants produce a date or time that's the same as the one you get from `date.toString()` (minus the local timezone information, and formatted differently). The UTC variants produce a date or time that's the same as the one shown by `date.toISOString()` (minus the `Z`, and formatted differently). 25 | 26 | * For `XlsxTypes.String` columns, cell values will be coerced to `string`. 27 | * For `XlsxTypes.Number` columns, cell values must be provided as either `number` or (numeric) `string`. 28 | * For `XlsxTypes.LocalDate`, `XlsxTypes.UTCDate`, `XlsxTypes.LocalTime`, `XlsxTypes.UTCTime`, `XlsxTypes.LocalDateTime` and `XlsxTypes.UTCDateTime` columns, cell values should be provided as `Date` objects, with `string` as a fallback (e.g. if the date is infinite, or before 1900, or otherwise unsupported by Excel). 29 | * For all column types, `null` or `undefined` cell values result in an empty cell. 30 | 31 | 32 | ## Example usage 33 | 34 | To write an `.xlsx` file in Node: 35 | 36 | ```javascript 37 | import { createXlsx, XlsxTypes as Xl } from 'xlsxtable'; 38 | import { writeFileSync } from 'fs'; 39 | 40 | const now = new Date(); 41 | 42 | createXlsx({ 43 | // sheet data 44 | headings: ['id', 'name', 'dob', 'wake_up', 'lastUpdated'], 45 | types: [Xl.Number, Xl.String, Xl.LocalDate, Xl.LocalTime, Xl.LocalDateTime], 46 | data: [ 47 | [1, 'Anna', new Date(1979, 0, 1), new Date(0, 0, 0, 7), now], 48 | [2, 'Bryn', new Date(1989, 1, 2), new Date(0, 0, 0, 8), now], 49 | [3, 'Chip', new Date(1999, 2, 3), new Date(0, 0, 0, 9), now], 50 | ], 51 | // options 52 | sheetName: 'Sheet 1', // shown on the tab at the bottom: limited character range allowed 53 | freeze: true, // freeze the top/header row 54 | autoFilter: true, // enable autofilter for headers 55 | wrapText: true, // wrap long text cells 56 | // metadata 57 | creator: 'Diane', 58 | title: 'Blughupsnitch data', 59 | description: 'Data about the blughupsnitch', 60 | company: 'Dogoodnever Inc.', 61 | }) 62 | .then(xlsx => writeFileSync('/path/to/my.xlsx', xlsx)); 63 | ``` 64 | 65 | This produces [`my.xlsx`](my.xlsx): 66 | 67 | ![Screenshot](my.xlsx.png) 68 | 69 | To provide a download in browsers, something like this works well: 70 | 71 | ```javascript 72 | const xlsx = await createXlsx(/* ... */); 73 | 74 | const url = URL.createObjectURL(new Blob([xlsx])); 75 | const link = document.createElement('a'); 76 | link.style.display = 'none'; 77 | document.body.appendChild(link); 78 | link.href = url; 79 | link.download = 'my.xlsx'; 80 | link.click(); 81 | setTimeout(() => { 82 | URL.revokeObjectURL(url); 83 | document.body.removeChild(link); 84 | }, 0); 85 | ``` 86 | 87 | ## License 88 | 89 | [MIT licensed](LICENSE). 90 | -------------------------------------------------------------------------------- /example.mjs: -------------------------------------------------------------------------------- 1 | import { createXlsx, XlsxTypes as Xl } from './index.mjs'; 2 | import { writeFileSync } from 'fs'; 3 | 4 | const now = new Date(); 5 | 6 | createXlsx({ 7 | // sheet data 8 | headings: ['id', 'name', 'dob', 'wake_up', 'lastUpdated', 'notes'], 9 | types: [Xl.Number, Xl.String, Xl.LocalDate, Xl.LocalTime, Xl.LocalDateTime, Xl.String], 10 | data: [ 11 | [1, 'Anna', new Date('1979-01-01'), new Date(0, 0, 0, 7), now, ''], 12 | [2, 'Bryn', new Date('1989-02-03'), new Date(0, 0, 0, 8), now, ''], 13 | [3, 'Chip', new Date('1999-03-04'), new Date(0, 0, 0, 9), now, '😆'], 14 | [4, 'Diane', undefined, undefined, undefined, undefined], 15 | [null, null, new Date('2009-04-05'), new Date(0, 0, 0, 10), now, ''], 16 | [6, 'Francis Ysidro Edgeworth', new Date('1899-04-05'), new Date(0, 0, 0, 11), now, 'Founding editor of the Economic Journal\nFrankie boy'], 17 | ], 18 | // options 19 | sheetName: 'People', // shown on the tab at the bottom: limited character range allowed 20 | // metadata 21 | creator: 'Blughupsnitch', 22 | title: 'Blughupsnitch data', 23 | description: 'Data about the blughupsnitch', 24 | company: 'Dogoodnever Inc.', 25 | }) 26 | .then(xlsx => writeFileSync('examples/my.xlsx', xlsx)); 27 | -------------------------------------------------------------------------------- /examples/my.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jawj/xlsxtable/8ed74727bb8f1a807be8aa01d32a4d3b4d53ac8d/examples/my.xlsx -------------------------------------------------------------------------------- /index.d.mts: -------------------------------------------------------------------------------- 1 | 2 | export enum XlsxTypes { 3 | Number, 4 | String, 5 | LocalDate, 6 | LocalTime, 7 | LocalDateTime, 8 | UTCDate, 9 | UTCTime, 10 | UTCDateTime, 11 | } 12 | 13 | interface XlsxRequiredConfig { 14 | headings: string[]; 15 | types: XlsxTypes[]; 16 | data: any[][]; 17 | } 18 | 19 | interface XlsxOptionalConfig { 20 | wrapText: boolean, 21 | freeze: boolean; 22 | autoFilter: boolean; 23 | sheetName: string; 24 | creator: string; 25 | title: string; 26 | description: string; 27 | company: string; 28 | minColWidth: number; // includes padWidth 29 | maxColWidth: number; // includes padWidth 30 | padWidth: number; 31 | stringCharWidth: number; 32 | } 33 | 34 | interface XlsxConfig extends XlsxRequiredConfig, Partial { } 35 | 36 | export function createXlsx(config: XlsxConfig): Promise; 37 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | 2 | export enum XlsxTypes { 3 | Number, 4 | String, 5 | LocalDate, 6 | LocalTime, 7 | LocalDateTime, 8 | UTCDate, 9 | UTCTime, 10 | UTCDateTime, 11 | } 12 | 13 | interface XlsxRequiredConfig { 14 | headings: string[]; 15 | types: XlsxTypes[]; 16 | data: any[][]; 17 | } 18 | 19 | interface XlsxOptionalConfig { 20 | wrapText: boolean, 21 | freeze: boolean; 22 | autoFilter: boolean; 23 | sheetName: string; 24 | creator: string; 25 | title: string; 26 | description: string; 27 | company: string; 28 | minColWidth: number; // includes padWidth 29 | maxColWidth: number; // includes padWidth 30 | padWidth: number; 31 | stringCharWidth: number; 32 | } 33 | 34 | interface XlsxConfig extends XlsxRequiredConfig, Partial { } 35 | 36 | export function createXlsx(config: XlsxConfig): Promise; 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict";var G=Object.defineProperty;var he=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var ye=Object.prototype.hasOwnProperty;var xe=(a,r)=>{for(var n in r)G(a,n,{get:r[n],enumerable:!0})},ue=(a,r,n,o)=>{if(r&&typeof r=="objec\ 2 | t"||typeof r=="function")for(let s of ge(r))!ye.call(a,s)&&s!==n&&G(a,s,{get:()=>r[s],enumerable:!(o= 3 | he(r,s))||o.enumerable});return a};var Ce=a=>ue(G({},"__esModule",{value:!0}),a);var Ue={};xe(Ue,{XlsxTypes:()=>de,createXlsx:()=>Ee});module.exports=Ce(Ue);var z,ve=()=>{let a=Int32Array,r=new a(256),n=new a(4096),o,s,f;for(s=0;s<256;s++)o=s,o=o&1?-306674912^ 4 | o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o= 5 | o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,n[s]=r[s]=o&1?-306674912^ 6 | o>>>1:o>>>1;for(s=0;s<256;s++)for(f=r[s],o=256+s;o<4096;o+=256)f=n[o]=f>>>8^r[f&255];for(z=[r],s=1;s< 7 | 16;s++)z[s]=n.subarray(s*256,(s+1)*256)},Te=(a,r=0)=>{z||ve();let[n,o,s,f,w,N,p,A,M,R,X,t,e,D,i,x]=z, 8 | y=r^-1,u=a.length-15,c=0;for(;c>8&255]^D[a[c++]^y>>16&255]^e[a[c++]^ 9 | y>>>24]^t[a[c++]]^X[a[c++]]^R[a[c++]]^M[a[c++]]^A[a[c++]]^p[a[c++]]^N[a[c++]]^w[a[c++]]^f[a[c++]]^s[a[c++]]^ 10 | o[a[c++]]^n[a[c++]];for(u+=15;c>>8^n[(y^a[c++])&255];return~y},be=typeof CompressionStream<"\ 11 | u",ee=new TextEncoder,te=a=>a.reduce((r,n)=>r+n.length,0),ae=Uint8Array;function Se(a){let r=new CompressionStream( 12 | "gzip"),n=r.writable.getWriter(),o=r.readable.getReader();return n.write(a),n.close(),()=>o.read()}async function oe(a,r=!0,n=Se){ 13 | let o=[],s=be&&r,f=a.length,w=a.map(i=>ee.encode(i.path)),N=a.map(({data:i})=>typeof i=="string"?ee. 14 | encode(i):i instanceof ArrayBuffer?new ae(i):i),p=te(N),A=te(w),M=f*46+A,R=p+f*30+A+M+22,X=new Date, 15 | t=new ae(R),e=0;for(let i=0;i>8,t[e++]=H&255,t[e++]=H>>8;let C=e;if(e+=8,t[e++]=c& 19 | 255,t[e++]=c>>8&255,t[e++]=c>>16&255,t[e++]=c>>24,t[e++]=y&255,t[e++]=y>>8,t[e++]=t[e++]=0,t.set(x,e), 20 | e+=y,s){let L=e,O=n(u),v,P=0,B=0;e:{for(;;){let l=await O();if(l.done)throw new Error("Bad gzip data"); 21 | if(v=l.value,P=B,B=P+v.length,P<=3&&B>3&&v[3-P]&30){U=!0;break e}if(B>=10){v=v.subarray(10-P);break}} 22 | for(;;){let l=e-L,d=v.length;if(l+d>=c+8){U=!0;break e}t.set(v,e),e+=d;let h=await O();if(h.done)break; 23 | v=h.value}}if(U)for(;;){let l=v.length,d=8-l,h=e;e=L;for(let m=0;m<8;m++)t[e++]=m>8&255, 26 | t[C++]=L>>16&255,t[C++]=L>>24}t[C++]=$&255,t[C++]=$>>8&255,t[C++]=$>>16&255,t[C++]=$>>24}let D=e;for(let i=0;i< 27 | f;i++){let x=o[i],y=w[i],u=y.length;t[e++]=80,t[e++]=75,t[e++]=1,t[e++]=2,t[e++]=20,t[e++]=0,t[e++]= 28 | 20,t[e++]=0,t.set(t.subarray(x+6,x+30),e),e+=24,t[e++]=t[e++]=t[e++]=t[e++]=t[e++]=t[e++]=t[e++]=t[e++]= 29 | t[e++]=t[e++]=0,t[e++]=x&255,t[e++]=x>>8&255,t[e++]=x>>16&255,t[e++]=x>>24,t.set(y,e),e+=u}return t[e++]= 30 | 80,t[e++]=75,t[e++]=5,t[e++]=6,t[e++]=t[e++]=t[e++]=t[e++]=0,t[e++]=f&255,t[e++]=f>>8,t[e++]=f&255,t[e++]= 31 | f>>8,t[e++]=M&255,t[e++]=M>>8&255,t[e++]=M>>16&255,t[e++]=M>>24,t[e++]=D&255,t[e++]=D>>8&255,t[e++]= 32 | D>>16&255,t[e++]=D>>24,t[e++]=t[e++]=0,t.subarray(0,e)}var Fe={"<":"<",">":">","&":"&","'":"'",'"':"""},F=a=>a.replace(/[<>&'"]/g,r=>Fe[r]), 33 | we=a=>{let r="";for(;;){let n=a%26;if(r=String.fromCharCode(65+n)+r,a=(a-n)/26-1,a<0)return r}},W=(a,r)=>`${we( 34 | a)}${r+1}`,De=Date.UTC(1899,11,31)/1e3/3600/24,re=(a,r,n)=>{let o;r?(n&&a.setUTCFullYear(1899,11,31), 35 | o=a.getTime()):o=Date.UTC(n?1899:a.getFullYear(),n?11:a.getMonth(),n?31:a.getDate(),a.getHours(),a.getMinutes(), 36 | a.getSeconds(),a.getMilliseconds());let s=o/1e3/3600/24-De;if(!(s<0)&&!(s<1&&!n))return s>=60&&(s+=1), 37 | s};function J(a,r=1/0){let n=0,o=-1,s,f=a.length;for(;;){s=a.indexOf(` 38 | `,++o),s===-1&&(s=f);let w=s-o;if(w>n&&(n=w),s===f||n>=r)break;o=s}return n}function k(a){return(a<10? 39 | "0":"")+a}function se(a){return(a<10?"00":a<100?"0":"")+a}function ne(a){return(a<10?"000":a<100?"00": 40 | a<1e3?"0":"")+a}function le(a,r,n){return(r?`${ne(a.getFullYear())}-${k(a.getMonth()+1)}-${k(a.getDate())}`: 41 | "")+(r&&n?" ":"")+(n?`${k(a.getHours())}:${k(a.getMinutes())}:${k(a.getSeconds())}.${se(a.getMilliseconds())}`: 42 | "")}function ie(a,r,n){return(r?`${ne(a.getUTCFullYear())}-${k(a.getUTCMonth()+1)}-${k(a.getUTCDate())}`: 43 | "")+(r&&n?" ":"")+(n?`${k(a.getUTCHours())}:${k(a.getUTCMinutes())}:${k(a.getUTCSeconds())}.${se(a.getUTCMilliseconds())}`: 44 | "")}var K=` 45 | 46 | 48 | 49 | 51 | 53 | 55 | 57 | 59 | 61 | 63 | `;var q=` 64 | 65 | 68 | 71 | 74 | `;var Z=` 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 366 | 367 | 368 | `;var _=` 369 | 370 | 373 | 376 | 379 | 382 | `;var ce=({company:a})=>` 383 | 384 | ${F(a)} 385 | `;var pe=({title:a,description:r,creator:n,creationDate:o})=>{let s=o.toISOString();return` 387 | 392 | ${F(a)} 393 | ${F(r)} 394 | ${F(n)} 395 | ${F(n)} 396 | ${s} 397 | ${s} 398 | `};var me=({sheetName:a})=>` 399 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | `;var fe=({wrapText:a})=>` 408 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 475 | 476 | 478 | 479 | 480 | 482 | 483 | 484 | 485 | `;var de=(p=>(p[p.Number=0]="Number",p[p.String=1]="String",p[p.LocalDate=2]="LocalDate",p[p.LocalTime= 486 | 3]="LocalTime",p[p.LocalDateTime=4]="LocalDateTime",p[p.UTCDate=5]="UTCDate",p[p.UTCTime=6]="UTCTime", 487 | p[p.UTCDateTime=7]="UTCDateTime",p))(de||{}),$e={wrapText:!0,freeze:!0,autoFilter:!0,sheetName:"Shee\ 488 | t 1",creator:"xlsxtable",title:"",description:"",company:"",minColWidth:5,maxColWidth:50,padWidth:2, 489 | stringCharWidth:1.1},Ee=a=>{let{headings:r,types:n,data:o,wrapText:s,freeze:f,autoFilter:w,creator:N, 490 | title:p,description:A,sheetName:M,company:R,minColWidth:X,maxColWidth:t,padWidth:e,stringCharWidth:D}={ 491 | ...$e,...a},i=r.length;if(i!==n.length||i!==o[0].length)throw new Error(`Number of headings (${i}), \ 492 | types (${n.length}) and data columns (first row: ${o[0].length}) must be the same`);let x=o.length,y=new Date, 493 | u=new Map,c=0,I=0,S=[],H={5:10+e,6:8+e,7:18+e,2:10+e,3:8+e,4:18+e},$=e+(w?2:0),U=Math.ceil(t/D)-$;for(let l=0;l< 494 | i;l++){let d=r[l],g=J(d,U)*D+$;S[l]=gt?t:g}let j=Math.ceil(t/D)-e,C=t-e;for(let l=0;l=t)continue;let d=n[l],h=H[d];if(h!==void 0){S[l]=j))break}let m=g*D+e;S[l]t?t:m)}else if(d=== 497 | 0){let g=0;for(let E=0;Eg&& 498 | (g=b),g>=C)break}let m=g+e;S[l]t?t:m)}}let L=`${S.map((l,d)=>``).join("")}`,O=`${r.map((l,d)=>`${F(l)}`).join( 501 | "")}`,v=`${o.map((l,d)=>`${l.map((h,g)=>{if(h==null)return"";let m=n[g], 502 | E=m>=2,T;if(E){let b=m===6||m===3,V=m===5||m===2,Y=!V&&!b,Q=m>=5;T=b?4:V?3:2,h instanceof Date&&(h=re( 503 | h,Q,b)??(Q?ie(h,V||Y,b||Y):le(h,V||Y,b||Y))),typeof h=="string"&&(m=1)}if(m===1){c++;let b=u.get(h); 504 | return b===void 0&&(b=I,u.set(h,b),I++),`${b}`}return`${h}`}).join("")}`).join("")}`,P=` 507 | 509 | ${[...u].map(([l])=>`${F(l)}`).join("")} 510 | `,B=` 511 | 517 | 518 | 519 | 520 | ${f?'':""} 521 | 522 | 523 | 524 | 525 | ${L} 526 | 527 | ${O} 528 | ${v} 529 | 530 | ${w?``:""} 531 | 532 | `;return oe([{path:"[Content_Types].xml",data:K},{path:"_rels/.rels",data:q},{path:"xl/_\ 533 | rels/workbook.xml.rels",data:_},{path:"xl/workbook.xml",data:me({sheetName:M})},{path:"xl/styles.xml", 534 | data:fe({wrapText:s})},{path:"xl/theme/theme1.xml",data:Z},{path:"xl/sharedStrings.xml",data:P},{path:"\ 535 | xl/worksheets/sheet1.xml",data:B},{path:"docProps/core.xml",data:pe({title:p,description:A,creator:N, 536 | creationDate:y})},{path:"docProps/app.xml",data:ce({company:R})}])}; 537 | -------------------------------------------------------------------------------- /index.mjs: -------------------------------------------------------------------------------- 1 | var z,fe=()=>{let a=Int32Array,n=new a(256),r=new a(4096),o,s,f;for(s=0;s<256;s++)o=s,o=o&1?-306674912^ 2 | o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o= 3 | o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,o=o&1?-306674912^o>>>1:o>>>1,r[s]=n[s]=o&1?-306674912^ 4 | o>>>1:o>>>1;for(s=0;s<256;s++)for(f=n[s],o=256+s;o<4096;o+=256)f=r[o]=f>>>8^n[f&255];for(z=[n],s=1;s< 5 | 16;s++)z[s]=r.subarray(s*256,(s+1)*256)},de=(a,n=0)=>{z||fe();let[r,o,s,f,w,N,p,A,M,R,X,t,e,D,i,x]=z, 6 | y=n^-1,u=a.length-15,c=0;for(;c>8&255]^D[a[c++]^y>>16&255]^e[a[c++]^ 7 | y>>>24]^t[a[c++]]^X[a[c++]]^R[a[c++]]^M[a[c++]]^A[a[c++]]^p[a[c++]]^N[a[c++]]^w[a[c++]]^f[a[c++]]^s[a[c++]]^ 8 | o[a[c++]]^r[a[c++]];for(u+=15;c>>8^r[(y^a[c++])&255];return~y},he=typeof CompressionStream<"\ 9 | u",Q=new TextEncoder,ee=a=>a.reduce((n,r)=>n+r.length,0),te=Uint8Array;function ge(a){let n=new CompressionStream( 10 | "gzip"),r=n.writable.getWriter(),o=n.readable.getReader();return r.write(a),r.close(),()=>o.read()}async function ae(a,n=!0,r=ge){ 11 | let o=[],s=he&&n,f=a.length,w=a.map(i=>Q.encode(i.path)),N=a.map(({data:i})=>typeof i=="string"?Q.encode( 12 | i):i instanceof ArrayBuffer?new te(i):i),p=ee(N),A=ee(w),M=f*46+A,R=p+f*30+A+M+22,X=new Date,t=new te( 13 | R),e=0;for(let i=0;i>8,t[e++]=H&255,t[e++]=H>>8;let C=e;if(e+=8,t[e++]=c&255,t[e++]= 17 | c>>8&255,t[e++]=c>>16&255,t[e++]=c>>24,t[e++]=y&255,t[e++]=y>>8,t[e++]=t[e++]=0,t.set(x,e),e+=y,s){let L=e, 18 | O=r(u),v,P=0,B=0;e:{for(;;){let l=await O();if(l.done)throw new Error("Bad gzip data");if(v=l.value, 19 | P=B,B=P+v.length,P<=3&&B>3&&v[3-P]&30){U=!0;break e}if(B>=10){v=v.subarray(10-P);break}}for(;;){let l=e- 20 | L,d=v.length;if(l+d>=c+8){U=!0;break e}t.set(v,e),e+=d;let h=await O();if(h.done)break;v=h.value}}if(U) 21 | for(;;){let l=v.length,d=8-l,h=e;e=L;for(let m=0;m<8;m++)t[e++]=m>8&255,t[C++]=L>>16& 24 | 255,t[C++]=L>>24}t[C++]=$&255,t[C++]=$>>8&255,t[C++]=$>>16&255,t[C++]=$>>24}let D=e;for(let i=0;i>8&255,t[e++]=x>>16&255,t[e++]=x>>24,t.set(y,e),e+=u}return t[e++]=80, 28 | t[e++]=75,t[e++]=5,t[e++]=6,t[e++]=t[e++]=t[e++]=t[e++]=0,t[e++]=f&255,t[e++]=f>>8,t[e++]=f&255,t[e++]= 29 | f>>8,t[e++]=M&255,t[e++]=M>>8&255,t[e++]=M>>16&255,t[e++]=M>>24,t[e++]=D&255,t[e++]=D>>8&255,t[e++]= 30 | D>>16&255,t[e++]=D>>24,t[e++]=t[e++]=0,t.subarray(0,e)}var ye={"<":"<",">":">","&":"&","'":"'",'"':"""},F=a=>a.replace(/[<>&'"]/g,n=>ye[n]), 31 | xe=a=>{let n="";for(;;){let r=a%26;if(n=String.fromCharCode(65+r)+n,a=(a-r)/26-1,a<0)return n}},W=(a,n)=>`${xe( 32 | a)}${n+1}`,ue=Date.UTC(1899,11,31)/1e3/3600/24,oe=(a,n,r)=>{let o;n?(r&&a.setUTCFullYear(1899,11,31), 33 | o=a.getTime()):o=Date.UTC(r?1899:a.getFullYear(),r?11:a.getMonth(),r?31:a.getDate(),a.getHours(),a.getMinutes(), 34 | a.getSeconds(),a.getMilliseconds());let s=o/1e3/3600/24-ue;if(!(s<0)&&!(s<1&&!r))return s>=60&&(s+=1), 35 | s};function G(a,n=1/0){let r=0,o=-1,s,f=a.length;for(;;){s=a.indexOf(` 36 | `,++o),s===-1&&(s=f);let w=s-o;if(w>r&&(r=w),s===f||r>=n)break;o=s}return r}function k(a){return(a<10? 37 | "0":"")+a}function re(a){return(a<10?"00":a<100?"0":"")+a}function se(a){return(a<10?"000":a<100?"00": 38 | a<1e3?"0":"")+a}function ne(a,n,r){return(n?`${se(a.getFullYear())}-${k(a.getMonth()+1)}-${k(a.getDate())}`: 39 | "")+(n&&r?" ":"")+(r?`${k(a.getHours())}:${k(a.getMinutes())}:${k(a.getSeconds())}.${re(a.getMilliseconds())}`: 40 | "")}function le(a,n,r){return(n?`${se(a.getUTCFullYear())}-${k(a.getUTCMonth()+1)}-${k(a.getUTCDate())}`: 41 | "")+(n&&r?" ":"")+(r?`${k(a.getUTCHours())}:${k(a.getUTCMinutes())}:${k(a.getUTCSeconds())}.${re(a.getUTCMilliseconds())}`: 42 | "")}var J=` 43 | 44 | 46 | 47 | 49 | 51 | 53 | 55 | 57 | 59 | 61 | `;var K=` 62 | 63 | 66 | 69 | 72 | `;var q=` 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 364 | 365 | 366 | `;var Z=` 367 | 368 | 371 | 374 | 377 | 380 | `;var ie=({company:a})=>` 381 | 382 | ${F(a)} 383 | `;var ce=({title:a,description:n,creator:r,creationDate:o})=>{let s=o.toISOString();return` 385 | 390 | ${F(a)} 391 | ${F(n)} 392 | ${F(r)} 393 | ${F(r)} 394 | ${s} 395 | ${s} 396 | `};var pe=({sheetName:a})=>` 397 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | `;var me=({wrapText:a})=>` 406 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 473 | 474 | 476 | 477 | 478 | 480 | 481 | 482 | 483 | `;var Se=(p=>(p[p.Number=0]="Number",p[p.String=1]="String",p[p.LocalDate=2]="LocalDate",p[p.LocalTime= 484 | 3]="LocalTime",p[p.LocalDateTime=4]="LocalDateTime",p[p.UTCDate=5]="UTCDate",p[p.UTCTime=6]="UTCTime", 485 | p[p.UTCDateTime=7]="UTCDateTime",p))(Se||{}),Fe={wrapText:!0,freeze:!0,autoFilter:!0,sheetName:"Shee\ 486 | t 1",creator:"xlsxtable",title:"",description:"",company:"",minColWidth:5,maxColWidth:50,padWidth:2, 487 | stringCharWidth:1.1},qe=a=>{let{headings:n,types:r,data:o,wrapText:s,freeze:f,autoFilter:w,creator:N, 488 | title:p,description:A,sheetName:M,company:R,minColWidth:X,maxColWidth:t,padWidth:e,stringCharWidth:D}={ 489 | ...Fe,...a},i=n.length;if(i!==r.length||i!==o[0].length)throw new Error(`Number of headings (${i}), \ 490 | types (${r.length}) and data columns (first row: ${o[0].length}) must be the same`);let x=o.length,y=new Date, 491 | u=new Map,c=0,I=0,S=[],H={5:10+e,6:8+e,7:18+e,2:10+e,3:8+e,4:18+e},$=e+(w?2:0),U=Math.ceil(t/D)-$;for(let l=0;l< 492 | i;l++){let d=n[l],g=G(d,U)*D+$;S[l]=gt?t:g}let j=Math.ceil(t/D)-e,C=t-e;for(let l=0;l=t)continue;let d=r[l],h=H[d];if(h!==void 0){S[l]=j))break}let m=g*D+e;S[l]t?t:m)}else if(d=== 495 | 0){let g=0;for(let E=0;Eg&& 496 | (g=b),g>=C)break}let m=g+e;S[l]t?t:m)}}let L=`${S.map((l,d)=>``).join("")}`,O=`${n.map((l,d)=>`${F(l)}`).join( 499 | "")}`,v=`${o.map((l,d)=>`${l.map((h,g)=>{if(h==null)return"";let m=r[g], 500 | E=m>=2,T;if(E){let b=m===6||m===3,V=m===5||m===2,Y=!V&&!b,_=m>=5;T=b?4:V?3:2,h instanceof Date&&(h=oe( 501 | h,_,b)??(_?le(h,V||Y,b||Y):ne(h,V||Y,b||Y))),typeof h=="string"&&(m=1)}if(m===1){c++;let b=u.get(h); 502 | return b===void 0&&(b=I,u.set(h,b),I++),`${b}`}return`${h}`}).join("")}`).join("")}`,P=` 505 | 507 | ${[...u].map(([l])=>`${F(l)}`).join("")} 508 | `,B=` 509 | 515 | 516 | 517 | 518 | ${f?'':""} 519 | 520 | 521 | 522 | 523 | ${L} 524 | 525 | ${O} 526 | ${v} 527 | 528 | ${w?``:""} 529 | 530 | `;return ae([{path:"[Content_Types].xml",data:J},{path:"_rels/.rels",data:K},{path:"xl/_\ 531 | rels/workbook.xml.rels",data:Z},{path:"xl/workbook.xml",data:pe({sheetName:M})},{path:"xl/styles.xml", 532 | data:me({wrapText:s})},{path:"xl/theme/theme1.xml",data:q},{path:"xl/sharedStrings.xml",data:P},{path:"\ 533 | xl/worksheets/sheet1.xml",data:B},{path:"docProps/core.xml",data:ce({title:p,description:A,creator:N, 534 | creationDate:y})},{path:"docProps/app.xml",data:ie({company:R})}])};export{Se as XlsxTypes,qe as createXlsx}; 535 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | 2 | import { createZip } from 'littlezipper'; 3 | import { xmlesc, cellRef, excelDate, ymdLocal, ymdUTC, longestLineLengthInString } from './utils'; 4 | import { contentTypesXml, relsXml, theme1Xml, workbookXmlRels } from './xlsx-static'; 5 | import appXml from './xlsx-dynamic/appxml'; 6 | import coreXml from './xlsx-dynamic/corexml'; 7 | import workbookXml from './xlsx-dynamic/workbookxml'; 8 | import stylesXml from './xlsx-dynamic/stylesxml'; 9 | 10 | export enum XlsxTypes { 11 | // note: owing to the logic used below, the order of these enum types must not be changed 12 | Number, 13 | String, 14 | LocalDate, 15 | LocalTime, 16 | LocalDateTime, 17 | UTCDate, 18 | UTCTime, 19 | UTCDateTime, 20 | } 21 | 22 | interface XlsxRequiredConfig { 23 | headings: string[]; 24 | types: XlsxTypes[]; 25 | data: any[][]; 26 | } 27 | 28 | interface XlsxOptionalConfig { 29 | wrapText: boolean, 30 | freeze: boolean; 31 | autoFilter: boolean; 32 | sheetName: string; 33 | creator: string; 34 | title: string; 35 | description: string; 36 | company: string; 37 | minColWidth: number; // includes padWidth 38 | maxColWidth: number; // includes padWidth 39 | padWidth: number; 40 | stringCharWidth: number; 41 | } 42 | 43 | interface XlsxConfig extends XlsxRequiredConfig, Partial { } 44 | 45 | const defaultConfig: XlsxOptionalConfig = { 46 | wrapText: true, 47 | freeze: true, 48 | autoFilter: true, 49 | sheetName: 'Sheet 1', 50 | creator: 'xlsxtable', 51 | title: '', 52 | description: '', 53 | company: '', 54 | minColWidth: 5, // includes padWidth 55 | maxColWidth: 50, // includes padWidth 56 | padWidth: 2, // allow this many characters at left or right of cell 57 | stringCharWidth: 1.1, // all 'm's would be about 1.55, so this doesn't guarantee no wrapping 58 | }; 59 | 60 | export const createXlsx = (config: XlsxConfig) => { 61 | const { 62 | headings, types, data, 63 | wrapText, freeze, autoFilter, 64 | creator, title, description, sheetName, company, 65 | minColWidth, maxColWidth, padWidth, stringCharWidth, 66 | } = { ...defaultConfig, ...config }; 67 | 68 | const cols = headings.length; 69 | if (cols !== types.length || cols !== data[0].length) { 70 | throw new Error(`Number of headings (${cols}), types (${types.length}) and data columns (first row: ${data[0].length}) must be the same`); 71 | } 72 | 73 | const rows = data.length; 74 | const creationDate = new Date(); 75 | 76 | const sharedStrings = new Map(); 77 | let totalStringCount = 0; 78 | let uniqueStringCount = 0; 79 | 80 | const colWidths = []; 81 | 82 | const typeWidths = { 83 | [XlsxTypes.UTCDate]: 10 + padWidth, 84 | [XlsxTypes.UTCTime]: 8 + padWidth, 85 | [XlsxTypes.UTCDateTime]: 18 + padWidth, 86 | [XlsxTypes.LocalDate]: 10 + padWidth, 87 | [XlsxTypes.LocalTime]: 8 + padWidth, 88 | [XlsxTypes.LocalDateTime]: 18 + padWidth, 89 | } 90 | 91 | // start with heading lengths, clamped to min and max 92 | const headingPadWidth = padWidth + (autoFilter ? 2 : 0); // extra space to allow for autofilter controls 93 | const maxHeaderStringChars = Math.ceil(maxColWidth / stringCharWidth) - headingPadWidth; 94 | for (let colIndex = 0; colIndex < cols; colIndex++) { 95 | const heading = headings[colIndex]; 96 | const longestLineLength = longestLineLengthInString(heading, maxHeaderStringChars); 97 | const headingWidth = longestLineLength * stringCharWidth + headingPadWidth; 98 | colWidths[colIndex] = 99 | headingWidth < minColWidth ? minColWidth : 100 | headingWidth > maxColWidth ? maxColWidth : 101 | headingWidth; 102 | } 103 | 104 | // update for data rows 105 | const maxColStringChars = Math.ceil(maxColWidth / stringCharWidth) - padWidth; 106 | const maxColNumberChars = maxColWidth - padWidth; 107 | 108 | for (let colIndex = 0; colIndex < cols; colIndex++) { 109 | // if already at max width from column name, skip 110 | if (colWidths[colIndex] >= maxColWidth) continue; 111 | 112 | // check if there's a type-defined width 113 | const type = types[colIndex] 114 | const typeWidth = typeWidths[type as keyof typeof typeWidths]; 115 | if (typeWidth !== undefined) { 116 | if (colWidths[colIndex] < typeWidth) colWidths[colIndex] = typeWidth; 117 | continue; // no need to check multiple rows 118 | } 119 | 120 | // for a string column, check max line length within all strings 121 | if (type === XlsxTypes.String) { 122 | let longestLineLength = 0; 123 | for (let rowIndex = 0; rowIndex < rows; rowIndex++) { 124 | let cell = data[rowIndex][colIndex]; 125 | if (cell == null /* or undefined */) continue; 126 | cell = String(cell); 127 | longestLineLength = longestLineLengthInString(cell, maxColStringChars); 128 | if (longestLineLength >= maxColStringChars) break; 129 | } 130 | const colWidth = longestLineLength * stringCharWidth + padWidth; 131 | if (colWidths[colIndex] < colWidth) colWidths[colIndex] = colWidth > maxColWidth ? maxColWidth : colWidth; 132 | 133 | // check max number length 134 | } else if (type === XlsxTypes.Number) { 135 | let longestNumLength = 0; 136 | for (let rowIndex = 0; rowIndex < rows; rowIndex++) { 137 | let cell = data[rowIndex][colIndex]; 138 | if (cell == null /* or undefined */) continue; 139 | cell = String(cell); 140 | const numLength = cell.length; 141 | if (numLength > longestNumLength) longestNumLength = numLength; 142 | if (longestNumLength >= maxColNumberChars) break; 143 | } 144 | const colWidth = longestNumLength + padWidth; 145 | if (colWidths[colIndex] < colWidth) colWidths[colIndex] = colWidth > maxColWidth ? maxColWidth : colWidth; 146 | } 147 | } 148 | 149 | const colsXml = `${colWidths.map( 150 | (colWidth, colIndex) => ``).join('') 151 | }`; 152 | 153 | // currently we deal with headings separately, as inline strings 154 | const headingsXml = `${headings.map( 155 | (cell, colIndex) => `${xmlesc(cell)}`) 156 | .join('')}`; 157 | 158 | const rowsXml = `${data.map((row, rowIndex) => `${row.map( 159 | (cell, colIndex) => { 160 | if (cell == null /* or undefined */) return ''; 161 | 162 | let type = types[colIndex]; 163 | const isDateOrTime = type >= XlsxTypes.LocalDate; 164 | 165 | let styleIndex; 166 | if (isDateOrTime) { 167 | const isTime = type === XlsxTypes.UTCTime || type === XlsxTypes.LocalTime; 168 | const isDate = type === XlsxTypes.UTCDate || type === XlsxTypes.LocalDate; 169 | const isDateTime = !isDate && !isTime; 170 | const isUTC = type >= XlsxTypes.UTCDate; 171 | 172 | styleIndex = isTime ? 4 : isDate ? 3 : 2; 173 | 174 | if (cell instanceof Date) { 175 | cell = excelDate(cell, isUTC, isTime) ?? // for invalid dates, fall back to a string 176 | (isUTC ? 177 | ymdUTC(cell, isDate || isDateTime, isTime || isDateTime) : 178 | ymdLocal(cell, isDate || isDateTime, isTime || isDateTime) 179 | ); 180 | } 181 | 182 | if (typeof cell === 'string') type = XlsxTypes.String; 183 | } 184 | 185 | if (type === XlsxTypes.String) { 186 | totalStringCount++; 187 | let stringIndex = sharedStrings.get(cell); 188 | if (stringIndex === undefined) { 189 | stringIndex = uniqueStringCount; 190 | sharedStrings.set(cell, stringIndex); 191 | uniqueStringCount++; 192 | } 193 | return `${stringIndex}`; 194 | } 195 | 196 | return `${cell}`; 197 | } 198 | ).join('')}`).join('')}`; 199 | 200 | const sharedStringsXml = ` 201 | 202 | ${[...sharedStrings].map(([k]) => `${xmlesc(k)}`).join('')} 203 | `; 204 | 205 | const sheetXml = ` 206 | 212 | 213 | 214 | 215 | ${freeze ? `` : ''} 216 | 217 | 218 | 219 | 220 | ${colsXml} 221 | 222 | ${headingsXml} 223 | ${rowsXml} 224 | 225 | ${autoFilter ? `` : ''} 226 | 227 | `; 228 | 229 | return createZip([ 230 | { path: '[Content_Types].xml', data: contentTypesXml }, 231 | { path: '_rels/.rels', data: relsXml }, 232 | { path: 'xl/_rels/workbook.xml.rels', data: workbookXmlRels }, 233 | { path: 'xl/workbook.xml', data: workbookXml({ sheetName }) }, 234 | { path: 'xl/styles.xml', data: stylesXml({ wrapText }) }, 235 | { path: 'xl/theme/theme1.xml', data: theme1Xml }, 236 | { path: 'xl/sharedStrings.xml', data: sharedStringsXml }, 237 | { path: 'xl/worksheets/sheet1.xml', data: sheetXml }, 238 | { path: 'docProps/core.xml', data: coreXml({ title, description, creator, creationDate }) }, 239 | { path: 'docProps/app.xml', data: appXml({ company }) }, 240 | ]); 241 | }; 242 | -------------------------------------------------------------------------------- /my.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jawj/xlsxtable/8ed74727bb8f1a807be8aa01d32a4d3b4d53ac8d/my.xlsx -------------------------------------------------------------------------------- /my.xlsx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jawj/xlsxtable/8ed74727bb8f1a807be8aa01d32a4d3b4d53ac8d/my.xlsx.png -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xlsxtable", 3 | "version": "0.3.1", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "xlsxtable", 9 | "version": "0.3.1", 10 | "license": "MIT", 11 | "dependencies": { 12 | "littlezipper": "^0.1.4" 13 | }, 14 | "devDependencies": { 15 | "esbuild": "^0.21.3", 16 | "typescript": "^5.4.2" 17 | } 18 | }, 19 | "node_modules/@esbuild/aix-ppc64": { 20 | "version": "0.21.3", 21 | "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.3.tgz", 22 | "integrity": "sha512-yTgnwQpFVYfvvo4SvRFB0SwrW8YjOxEoT7wfMT7Ol5v7v5LDNvSGo67aExmxOb87nQNeWPVvaGBNfQ7BXcrZ9w==", 23 | "cpu": [ 24 | "ppc64" 25 | ], 26 | "dev": true, 27 | "optional": true, 28 | "os": [ 29 | "aix" 30 | ], 31 | "engines": { 32 | "node": ">=12" 33 | } 34 | }, 35 | "node_modules/@esbuild/android-arm": { 36 | "version": "0.21.3", 37 | "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.21.3.tgz", 38 | "integrity": "sha512-bviJOLMgurLJtF1/mAoJLxDZDL6oU5/ztMHnJQRejbJrSc9FFu0QoUoFhvi6qSKJEw9y5oGyvr9fuDtzJ30rNQ==", 39 | "cpu": [ 40 | "arm" 41 | ], 42 | "dev": true, 43 | "optional": true, 44 | "os": [ 45 | "android" 46 | ], 47 | "engines": { 48 | "node": ">=12" 49 | } 50 | }, 51 | "node_modules/@esbuild/android-arm64": { 52 | "version": "0.21.3", 53 | "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.21.3.tgz", 54 | "integrity": "sha512-c+ty9necz3zB1Y+d/N+mC6KVVkGUUOcm4ZmT5i/Fk5arOaY3i6CA3P5wo/7+XzV8cb4GrI/Zjp8NuOQ9Lfsosw==", 55 | "cpu": [ 56 | "arm64" 57 | ], 58 | "dev": true, 59 | "optional": true, 60 | "os": [ 61 | "android" 62 | ], 63 | "engines": { 64 | "node": ">=12" 65 | } 66 | }, 67 | "node_modules/@esbuild/android-x64": { 68 | "version": "0.21.3", 69 | "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.21.3.tgz", 70 | "integrity": "sha512-JReHfYCRK3FVX4Ra+y5EBH1b9e16TV2OxrPAvzMsGeES0X2Ndm9ImQRI4Ket757vhc5XBOuGperw63upesclRw==", 71 | "cpu": [ 72 | "x64" 73 | ], 74 | "dev": true, 75 | "optional": true, 76 | "os": [ 77 | "android" 78 | ], 79 | "engines": { 80 | "node": ">=12" 81 | } 82 | }, 83 | "node_modules/@esbuild/darwin-arm64": { 84 | "version": "0.21.3", 85 | "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.21.3.tgz", 86 | "integrity": "sha512-U3fuQ0xNiAkXOmQ6w5dKpEvXQRSpHOnbw7gEfHCRXPeTKW9sBzVck6C5Yneb8LfJm0l6le4NQfkNPnWMSlTFUQ==", 87 | "cpu": [ 88 | "arm64" 89 | ], 90 | "dev": true, 91 | "optional": true, 92 | "os": [ 93 | "darwin" 94 | ], 95 | "engines": { 96 | "node": ">=12" 97 | } 98 | }, 99 | "node_modules/@esbuild/darwin-x64": { 100 | "version": "0.21.3", 101 | "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.21.3.tgz", 102 | "integrity": "sha512-3m1CEB7F07s19wmaMNI2KANLcnaqryJxO1fXHUV5j1rWn+wMxdUYoPyO2TnAbfRZdi7ADRwJClmOwgT13qlP3Q==", 103 | "cpu": [ 104 | "x64" 105 | ], 106 | "dev": true, 107 | "optional": true, 108 | "os": [ 109 | "darwin" 110 | ], 111 | "engines": { 112 | "node": ">=12" 113 | } 114 | }, 115 | "node_modules/@esbuild/freebsd-arm64": { 116 | "version": "0.21.3", 117 | "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.21.3.tgz", 118 | "integrity": "sha512-fsNAAl5pU6wmKHq91cHWQT0Fz0vtyE1JauMzKotrwqIKAswwP5cpHUCxZNSTuA/JlqtScq20/5KZ+TxQdovU/g==", 119 | "cpu": [ 120 | "arm64" 121 | ], 122 | "dev": true, 123 | "optional": true, 124 | "os": [ 125 | "freebsd" 126 | ], 127 | "engines": { 128 | "node": ">=12" 129 | } 130 | }, 131 | "node_modules/@esbuild/freebsd-x64": { 132 | "version": "0.21.3", 133 | "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.21.3.tgz", 134 | "integrity": "sha512-tci+UJ4zP5EGF4rp8XlZIdq1q1a/1h9XuronfxTMCNBslpCtmk97Q/5qqy1Mu4zIc0yswN/yP/BLX+NTUC1bXA==", 135 | "cpu": [ 136 | "x64" 137 | ], 138 | "dev": true, 139 | "optional": true, 140 | "os": [ 141 | "freebsd" 142 | ], 143 | "engines": { 144 | "node": ">=12" 145 | } 146 | }, 147 | "node_modules/@esbuild/linux-arm": { 148 | "version": "0.21.3", 149 | "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.21.3.tgz", 150 | "integrity": "sha512-f6kz2QpSuyHHg01cDawj0vkyMwuIvN62UAguQfnNVzbge2uWLhA7TCXOn83DT0ZvyJmBI943MItgTovUob36SQ==", 151 | "cpu": [ 152 | "arm" 153 | ], 154 | "dev": true, 155 | "optional": true, 156 | "os": [ 157 | "linux" 158 | ], 159 | "engines": { 160 | "node": ">=12" 161 | } 162 | }, 163 | "node_modules/@esbuild/linux-arm64": { 164 | "version": "0.21.3", 165 | "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.21.3.tgz", 166 | "integrity": "sha512-vvG6R5g5ieB4eCJBQevyDMb31LMHthLpXTc2IGkFnPWS/GzIFDnaYFp558O+XybTmYrVjxnryru7QRleJvmZ6Q==", 167 | "cpu": [ 168 | "arm64" 169 | ], 170 | "dev": true, 171 | "optional": true, 172 | "os": [ 173 | "linux" 174 | ], 175 | "engines": { 176 | "node": ">=12" 177 | } 178 | }, 179 | "node_modules/@esbuild/linux-ia32": { 180 | "version": "0.21.3", 181 | "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.21.3.tgz", 182 | "integrity": "sha512-HjCWhH7K96Na+66TacDLJmOI9R8iDWDDiqe17C7znGvvE4sW1ECt9ly0AJ3dJH62jHyVqW9xpxZEU1jKdt+29A==", 183 | "cpu": [ 184 | "ia32" 185 | ], 186 | "dev": true, 187 | "optional": true, 188 | "os": [ 189 | "linux" 190 | ], 191 | "engines": { 192 | "node": ">=12" 193 | } 194 | }, 195 | "node_modules/@esbuild/linux-loong64": { 196 | "version": "0.21.3", 197 | "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.21.3.tgz", 198 | "integrity": "sha512-BGpimEccmHBZRcAhdlRIxMp7x9PyJxUtj7apL2IuoG9VxvU/l/v1z015nFs7Si7tXUwEsvjc1rOJdZCn4QTU+Q==", 199 | "cpu": [ 200 | "loong64" 201 | ], 202 | "dev": true, 203 | "optional": true, 204 | "os": [ 205 | "linux" 206 | ], 207 | "engines": { 208 | "node": ">=12" 209 | } 210 | }, 211 | "node_modules/@esbuild/linux-mips64el": { 212 | "version": "0.21.3", 213 | "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.21.3.tgz", 214 | "integrity": "sha512-5rMOWkp7FQGtAH3QJddP4w3s47iT20hwftqdm7b+loe95o8JU8ro3qZbhgMRy0VuFU0DizymF1pBKkn3YHWtsw==", 215 | "cpu": [ 216 | "mips64el" 217 | ], 218 | "dev": true, 219 | "optional": true, 220 | "os": [ 221 | "linux" 222 | ], 223 | "engines": { 224 | "node": ">=12" 225 | } 226 | }, 227 | "node_modules/@esbuild/linux-ppc64": { 228 | "version": "0.21.3", 229 | "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.21.3.tgz", 230 | "integrity": "sha512-h0zj1ldel89V5sjPLo5H1SyMzp4VrgN1tPkN29TmjvO1/r0MuMRwJxL8QY05SmfsZRs6TF0c/IDH3u7XYYmbAg==", 231 | "cpu": [ 232 | "ppc64" 233 | ], 234 | "dev": true, 235 | "optional": true, 236 | "os": [ 237 | "linux" 238 | ], 239 | "engines": { 240 | "node": ">=12" 241 | } 242 | }, 243 | "node_modules/@esbuild/linux-riscv64": { 244 | "version": "0.21.3", 245 | "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.21.3.tgz", 246 | "integrity": "sha512-dkAKcTsTJ+CRX6bnO17qDJbLoW37npd5gSNtSzjYQr0svghLJYGYB0NF1SNcU1vDcjXLYS5pO4qOW4YbFama4A==", 247 | "cpu": [ 248 | "riscv64" 249 | ], 250 | "dev": true, 251 | "optional": true, 252 | "os": [ 253 | "linux" 254 | ], 255 | "engines": { 256 | "node": ">=12" 257 | } 258 | }, 259 | "node_modules/@esbuild/linux-s390x": { 260 | "version": "0.21.3", 261 | "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.21.3.tgz", 262 | "integrity": "sha512-vnD1YUkovEdnZWEuMmy2X2JmzsHQqPpZElXx6dxENcIwTu+Cu5ERax6+Ke1QsE814Zf3c6rxCfwQdCTQ7tPuXA==", 263 | "cpu": [ 264 | "s390x" 265 | ], 266 | "dev": true, 267 | "optional": true, 268 | "os": [ 269 | "linux" 270 | ], 271 | "engines": { 272 | "node": ">=12" 273 | } 274 | }, 275 | "node_modules/@esbuild/linux-x64": { 276 | "version": "0.21.3", 277 | "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.21.3.tgz", 278 | "integrity": "sha512-IOXOIm9WaK7plL2gMhsWJd+l2bfrhfilv0uPTptoRoSb2p09RghhQQp9YY6ZJhk/kqmeRt6siRdMSLLwzuT0KQ==", 279 | "cpu": [ 280 | "x64" 281 | ], 282 | "dev": true, 283 | "optional": true, 284 | "os": [ 285 | "linux" 286 | ], 287 | "engines": { 288 | "node": ">=12" 289 | } 290 | }, 291 | "node_modules/@esbuild/netbsd-x64": { 292 | "version": "0.21.3", 293 | "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.21.3.tgz", 294 | "integrity": "sha512-uTgCwsvQ5+vCQnqM//EfDSuomo2LhdWhFPS8VL8xKf+PKTCrcT/2kPPoWMTs22aB63MLdGMJiE3f1PHvCDmUOw==", 295 | "cpu": [ 296 | "x64" 297 | ], 298 | "dev": true, 299 | "optional": true, 300 | "os": [ 301 | "netbsd" 302 | ], 303 | "engines": { 304 | "node": ">=12" 305 | } 306 | }, 307 | "node_modules/@esbuild/openbsd-x64": { 308 | "version": "0.21.3", 309 | "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.21.3.tgz", 310 | "integrity": "sha512-vNAkR17Ub2MgEud2Wag/OE4HTSI6zlb291UYzHez/psiKarp0J8PKGDnAhMBcHFoOHMXHfExzmjMojJNbAStrQ==", 311 | "cpu": [ 312 | "x64" 313 | ], 314 | "dev": true, 315 | "optional": true, 316 | "os": [ 317 | "openbsd" 318 | ], 319 | "engines": { 320 | "node": ">=12" 321 | } 322 | }, 323 | "node_modules/@esbuild/sunos-x64": { 324 | "version": "0.21.3", 325 | "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.21.3.tgz", 326 | "integrity": "sha512-W8H9jlGiSBomkgmouaRoTXo49j4w4Kfbl6I1bIdO/vT0+0u4f20ko3ELzV3hPI6XV6JNBVX+8BC+ajHkvffIJA==", 327 | "cpu": [ 328 | "x64" 329 | ], 330 | "dev": true, 331 | "optional": true, 332 | "os": [ 333 | "sunos" 334 | ], 335 | "engines": { 336 | "node": ">=12" 337 | } 338 | }, 339 | "node_modules/@esbuild/win32-arm64": { 340 | "version": "0.21.3", 341 | "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.21.3.tgz", 342 | "integrity": "sha512-EjEomwyLSCg8Ag3LDILIqYCZAq/y3diJ04PnqGRgq8/4O3VNlXyMd54j/saShaN4h5o5mivOjAzmU6C3X4v0xw==", 343 | "cpu": [ 344 | "arm64" 345 | ], 346 | "dev": true, 347 | "optional": true, 348 | "os": [ 349 | "win32" 350 | ], 351 | "engines": { 352 | "node": ">=12" 353 | } 354 | }, 355 | "node_modules/@esbuild/win32-ia32": { 356 | "version": "0.21.3", 357 | "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.21.3.tgz", 358 | "integrity": "sha512-WGiE/GgbsEwR33++5rzjiYsKyHywE8QSZPF7Rfx9EBfK3Qn3xyR6IjyCr5Uk38Kg8fG4/2phN7sXp4NPWd3fcw==", 359 | "cpu": [ 360 | "ia32" 361 | ], 362 | "dev": true, 363 | "optional": true, 364 | "os": [ 365 | "win32" 366 | ], 367 | "engines": { 368 | "node": ">=12" 369 | } 370 | }, 371 | "node_modules/@esbuild/win32-x64": { 372 | "version": "0.21.3", 373 | "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.21.3.tgz", 374 | "integrity": "sha512-xRxC0jaJWDLYvcUvjQmHCJSfMrgmUuvsoXgDeU/wTorQ1ngDdUBuFtgY3W1Pc5sprGAvZBtWdJX7RPg/iZZUqA==", 375 | "cpu": [ 376 | "x64" 377 | ], 378 | "dev": true, 379 | "optional": true, 380 | "os": [ 381 | "win32" 382 | ], 383 | "engines": { 384 | "node": ">=12" 385 | } 386 | }, 387 | "node_modules/esbuild": { 388 | "version": "0.21.3", 389 | "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.3.tgz", 390 | "integrity": "sha512-Kgq0/ZsAPzKrbOjCQcjoSmPoWhlcVnGAUo7jvaLHoxW1Drto0KGkR1xBNg2Cp43b9ImvxmPEJZ9xkfcnqPsfBw==", 391 | "dev": true, 392 | "hasInstallScript": true, 393 | "bin": { 394 | "esbuild": "bin/esbuild" 395 | }, 396 | "engines": { 397 | "node": ">=12" 398 | }, 399 | "optionalDependencies": { 400 | "@esbuild/aix-ppc64": "0.21.3", 401 | "@esbuild/android-arm": "0.21.3", 402 | "@esbuild/android-arm64": "0.21.3", 403 | "@esbuild/android-x64": "0.21.3", 404 | "@esbuild/darwin-arm64": "0.21.3", 405 | "@esbuild/darwin-x64": "0.21.3", 406 | "@esbuild/freebsd-arm64": "0.21.3", 407 | "@esbuild/freebsd-x64": "0.21.3", 408 | "@esbuild/linux-arm": "0.21.3", 409 | "@esbuild/linux-arm64": "0.21.3", 410 | "@esbuild/linux-ia32": "0.21.3", 411 | "@esbuild/linux-loong64": "0.21.3", 412 | "@esbuild/linux-mips64el": "0.21.3", 413 | "@esbuild/linux-ppc64": "0.21.3", 414 | "@esbuild/linux-riscv64": "0.21.3", 415 | "@esbuild/linux-s390x": "0.21.3", 416 | "@esbuild/linux-x64": "0.21.3", 417 | "@esbuild/netbsd-x64": "0.21.3", 418 | "@esbuild/openbsd-x64": "0.21.3", 419 | "@esbuild/sunos-x64": "0.21.3", 420 | "@esbuild/win32-arm64": "0.21.3", 421 | "@esbuild/win32-ia32": "0.21.3", 422 | "@esbuild/win32-x64": "0.21.3" 423 | } 424 | }, 425 | "node_modules/littlezipper": { 426 | "version": "0.1.4", 427 | "resolved": "https://registry.npmjs.org/littlezipper/-/littlezipper-0.1.4.tgz", 428 | "integrity": "sha512-7F+NxDNMfN3JyrpcKGbejFAJ2WI+3GGHzWworYXZuj4jzQ6nUPvTX/eXhSDShnoJ5MnTid1ean3sNuMBrh/99w==" 429 | }, 430 | "node_modules/typescript": { 431 | "version": "5.4.5", 432 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", 433 | "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", 434 | "dev": true, 435 | "bin": { 436 | "tsc": "bin/tsc", 437 | "tsserver": "bin/tsserver" 438 | }, 439 | "engines": { 440 | "node": ">=14.17" 441 | } 442 | } 443 | } 444 | } 445 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xlsxtable", 3 | "version": "0.3.1", 4 | "description": "Create nice Excel files from tabular data", 5 | "files": [ 6 | "index.js", 7 | "index.mjs", 8 | "index.d.ts", 9 | "index.d.mts", 10 | "README.md" 11 | ], 12 | "exports": { 13 | "require": "./index.js", 14 | "import": "./index.mjs" 15 | }, 16 | "scripts": { 17 | "check": "tsc --noEmit", 18 | "buildCjs": "esbuild index.ts --bundle --external:littlezip --format=cjs --minify --line-limit=100 --loader:.xml=text --outfile=index.js", 19 | "buildEsm": "esbuild index.ts --bundle --external:littlezip --format=esm --minify --line-limit=100 --loader:.xml=text --outfile=index.mjs", 20 | "build": "npm run check && npm run buildCjs && npm run buildEsm && cp index.d.ts index.d.mts", 21 | "example": "npm run build && node example.mjs" 22 | }, 23 | "keywords": [ 24 | "xlsx", 25 | "xls", 26 | "Excel", 27 | "tables" 28 | ], 29 | "author": "George MacKerron", 30 | "license": "MIT", 31 | "devDependencies": { 32 | "esbuild": "^0.21.3", 33 | "typescript": "^5.4.2" 34 | }, 35 | "repository": { 36 | "type": "git", 37 | "url": "git+https://github.com/jawj/xlsxtable.git" 38 | }, 39 | "dependencies": { 40 | "littlezipper": "^0.1.4" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig to read more about this file */ 4 | 5 | /* Projects */ 6 | // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ 7 | // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ 8 | // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ 9 | // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ 10 | // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ 11 | // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ 12 | 13 | /* Language and Environment */ 14 | "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ 15 | // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ 16 | // "jsx": "preserve", /* Specify what JSX code is generated. */ 17 | // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ 18 | // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ 19 | // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ 20 | // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ 21 | // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ 22 | // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ 23 | // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ 24 | // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ 25 | // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ 26 | 27 | /* Modules */ 28 | "module": "commonjs", /* Specify what module code is generated. */ 29 | // "rootDir": "./", /* Specify the root folder within your source files. */ 30 | // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ 31 | // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ 32 | // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ 33 | // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ 34 | // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ 35 | // "types": [], /* Specify type package names to be included without being referenced in a source file. */ 36 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 37 | // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ 38 | // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ 39 | // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ 40 | // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ 41 | // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ 42 | // "resolveJsonModule": true, /* Enable importing .json files. */ 43 | // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ 44 | // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ 45 | 46 | /* JavaScript Support */ 47 | // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ 48 | // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ 49 | // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ 50 | 51 | /* Emit */ 52 | // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ 53 | // "declarationMap": true, /* Create sourcemaps for d.ts files. */ 54 | // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ 55 | // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ 56 | // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ 57 | // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ 58 | // "outDir": "./", /* Specify an output folder for all emitted files. */ 59 | // "removeComments": true, /* Disable emitting comments. */ 60 | // "noEmit": true, /* Disable emitting files from a compilation. */ 61 | // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ 62 | // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ 63 | // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ 64 | // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ 65 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 66 | // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ 67 | // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ 68 | // "newLine": "crlf", /* Set the newline character for emitting files. */ 69 | // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ 70 | // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ 71 | // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ 72 | // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ 73 | // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ 74 | // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ 75 | 76 | /* Interop Constraints */ 77 | // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ 78 | // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ 79 | // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ 80 | "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ 81 | // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ 82 | "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ 83 | 84 | /* Type Checking */ 85 | "strict": true, /* Enable all strict type-checking options. */ 86 | // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ 87 | // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ 88 | // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ 89 | // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ 90 | // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ 91 | // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ 92 | // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ 93 | // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ 94 | // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ 95 | // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ 96 | // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ 97 | // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ 98 | // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ 99 | // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ 100 | // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ 101 | // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ 102 | // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ 103 | // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ 104 | 105 | /* Completeness */ 106 | // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ 107 | "skipLibCheck": true /* Skip type checking all .d.ts files. */ 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /utils.ts: -------------------------------------------------------------------------------- 1 | const xmlEscapeMap = { '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' } as const; 2 | export const xmlesc = (s: string) => s.replace(/[<>&'"]/g, m => xmlEscapeMap[m as keyof typeof xmlEscapeMap]); 3 | 4 | export const colRef = (colIndex: number) => { // colIndex starts at zero 5 | let ref = ''; 6 | for (; ;) { 7 | const remainder = colIndex % 26; 8 | ref = String.fromCharCode(65 + remainder) + ref; 9 | colIndex = (colIndex - remainder) / 26 - 1; // minus 1 because AA (not BA) follows Z: this isn't quite base 26 10 | if (colIndex < 0) return ref; 11 | } 12 | }; 13 | export const cellRef = (colIndex: number, rowIndex: number) => `${colRef(colIndex)}${rowIndex + 1}`; // colIndex, rowIndex both start at zero 14 | 15 | const excelEpochMs = Date.UTC(1899, 11, 31) / 1000 / 3600 / 24; // Excel considers 1 Jan 1900 as day 1, so 31 Dec 1899 is day 0 16 | export const excelDate = (d: Date, isUtc: boolean, isTime: boolean) => { 17 | let utcMs; 18 | if (isUtc) { 19 | if (isTime) d.setUTCFullYear(1899, 11, 31); // Excel uses this date for time-only values 20 | utcMs = d.getTime(); 21 | 22 | } else { 23 | // to match the provided date in local time, convert to a UTC date with the same wall clock date/time 24 | utcMs = Date.UTC( 25 | isTime ? 1899 : d.getFullYear(), isTime ? 11 : d.getMonth(), isTime ? 31 : d.getDate(), 26 | d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds() 27 | ); 28 | } 29 | let days = utcMs / 1000 / 3600 / 24 - excelEpochMs; 30 | if (days < 0) return undefined; 31 | if (days < 1 && !isTime) return undefined; // Excel allows day zero for a time without a date 32 | if (days >= 60) days += 1; // Excel wrongly considers 1900 a leap year: http://www.cpearson.com/excel/datetime.htm 33 | return days; 34 | } 35 | 36 | export function longestLineLengthInString(s: string, max = Infinity) { 37 | let longestLineLength = 0; 38 | let lastNewlineIndex = -1; 39 | let newlineIndex; 40 | let cellChars = s.length; 41 | for (; ;) { 42 | newlineIndex = s.indexOf('\n', ++lastNewlineIndex); 43 | if (newlineIndex === -1) newlineIndex = cellChars; 44 | const lineLength = newlineIndex - lastNewlineIndex; 45 | if (lineLength > longestLineLength) longestLineLength = lineLength; 46 | if (newlineIndex === cellChars || longestLineLength >= max) break; 47 | lastNewlineIndex = newlineIndex; 48 | } 49 | return longestLineLength; 50 | } 51 | 52 | function d2(n: number) { 53 | return (n < 10 ? '0' : '') + n; 54 | } 55 | 56 | function d3(n: number) { 57 | return (n < 10 ? '00' : n < 100 ? '0' : '') + n; 58 | } 59 | 60 | function d4(n: number) { 61 | return (n < 10 ? '000' : n < 100 ? '00' : n < 1000 ? '0' : '') + n; 62 | } 63 | 64 | export function ymdLocal(d: Date, date: boolean, time: boolean) { 65 | return (date ? `${d4(d.getFullYear())}-${d2(d.getMonth() + 1)}-${d2(d.getDate())}` : '') + 66 | ((date && time) ? ' ' : '') + 67 | (time ? `${d2(d.getHours())}:${d2(d.getMinutes())}:${d2(d.getSeconds())}.${d3(d.getMilliseconds())}` : ''); 68 | } 69 | 70 | export function ymdUTC(d: Date, date: boolean, time: boolean) { 71 | return (date ? `${d4(d.getUTCFullYear())}-${d2(d.getUTCMonth() + 1)}-${d2(d.getUTCDate())}` : '') + 72 | ((date && time) ? ' ' : '') + 73 | (time ? `${d2(d.getUTCHours())}:${d2(d.getUTCMinutes())}:${d2(d.getUTCSeconds())}.${d3(d.getUTCMilliseconds())}` : ''); 74 | } 75 | -------------------------------------------------------------------------------- /xlsx-dynamic/appxml.ts: -------------------------------------------------------------------------------- 1 | import { xmlesc } from '../utils'; 2 | 3 | export default ({ company }: { company: string }) => ` 4 | 5 | ${xmlesc(company)} 6 | `; 7 | -------------------------------------------------------------------------------- /xlsx-dynamic/corexml.ts: -------------------------------------------------------------------------------- 1 | import { xmlesc } from '../utils'; 2 | 3 | interface CoreProps { 4 | title: string; 5 | description: string; 6 | creator: string; 7 | creationDate: Date; 8 | } 9 | 10 | export default ({ title, description, creator, creationDate }: CoreProps) => { 11 | const isoDate = creationDate.toISOString(); 12 | 13 | return ` 14 | 19 | ${xmlesc(title)} 20 | ${xmlesc(description)} 21 | ${xmlesc(creator)} 22 | ${xmlesc(creator)} 23 | ${isoDate} 24 | ${isoDate} 25 | ` 26 | }; 27 | -------------------------------------------------------------------------------- /xlsx-dynamic/stylesxml.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | export default ({ wrapText }: { wrapText: boolean }) => ` 4 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 73 | 74 | 75 | 77 | 78 | 79 | 80 | `; 81 | -------------------------------------------------------------------------------- /xlsx-dynamic/workbookxml.ts: -------------------------------------------------------------------------------- 1 | import { xmlesc } from '../utils'; 2 | 3 | export default ({ sheetName }: { sheetName: string }) => 4 | ` 5 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | `; 14 | -------------------------------------------------------------------------------- /xlsx-static/_rels.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | 12 | -------------------------------------------------------------------------------- /xlsx-static/content-types.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 9 | 11 | 13 | 15 | 17 | 19 | -------------------------------------------------------------------------------- /xlsx-static/index.ts: -------------------------------------------------------------------------------- 1 | // @ts-nocheck -- TS doesn't understand this esbuild feature 2 | 3 | import contentTypesXml from './content-types.xml'; 4 | import relsXml from './_rels.xml'; 5 | import theme1Xml from './theme1.xml'; 6 | import workbookXmlRels from './workbook-rels.xml'; 7 | 8 | export { contentTypesXml, relsXml, theme1Xml, workbookXmlRels }; 9 | -------------------------------------------------------------------------------- /xlsx-static/theme1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 293 | 294 | 295 | -------------------------------------------------------------------------------- /xlsx-static/workbook-rels.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | 12 | 15 | --------------------------------------------------------------------------------