Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-office/blob/main/src/xlsx.om # omega-office Omega office documents: read and write office files in Omega Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-office / src / xlsx.om` 753 lines ยท 12865 bytes Raw Blame History 1 note ======== src/xlsx.om: cells of a SpreadsheetML worksheet as CSV (ECMA-376 Part 1, 5th edition) ======== 2 note Workbook (18.2.27): sheets (18.2.20) lists each sheet (18.2.19) with its name and relationship id 3 note r:id. Shared String Table (18.4.9): each si (18.4.8) is the text of its t (18.4.12), directly or in 4 note rich text runs r (18.4.4); phonetic runs rPh (18.4.6) are not part of the value. Worksheet 5 note (18.3.1.99): sheetData (18.3.1.80) holds rows row (18.3.1.73) of cells c (18.3.1.4); a cell's 6 note value is v (18.3.1.96), or is (18.3.1.53) for an inline string; its type is t (18.18.11 ST_CellType): 7 note n number (default), s shared string index, str formula string, inlineStr, b boolean, e error, 8 note d ISO 8601 date. Values print as stored: numbers and dates exactly as their text in the file 9 note (a date in a number-formatted cell is a number: formats are in the styles part, not read here); 10 note b prints TRUE or FALSE. Strings are ST_Xstring (22.9.2.19): _xHHHH_ is decoded on output. 11 note The sheet is read twice: pass 1 finds the last row and column that hold a value, pass 2 prints 12 note rows 1 .. last row, each with fields 1 .. last column, in RFC 4180 form (CRLF record ends, a field 13 note with a comma, a quote, CR or LF in double quotes, quotes doubled). A cell is placed by its reference 14 note r ("B3"); without r it follows the previous cell (rows likewise). Rows and cells must ascend. 15 16 const SSMAX ulong `1048576` 17 const SSPOOL ulong `16777216` 18 const XCV ulong `16777216` 19 const MAXCOL ulong 16384 20 const MAXROW ulong `1048576` 21 22 note shared strings: string k is ssp[sso[k] .. sso[k+1]) 23 global ssp uchar `16777216` 24 global sso ulong `1048577` 25 note cv: the value of the cell being read (pass 2) 26 global cv uchar `16777216` 27 global ss ulong 32 28 const SCNT ulong 0 29 const SSPL ulong 1 30 const SINSI ulong 2 31 const SINT ulong 3 32 const SRPH ulong 4 33 const SROW ulong 5 34 const SPR ulong 6 35 const SCOL ulong 7 36 const SLC ulong 8 37 const SMAXR ulong 9 38 const SMAXC ulong 10 39 const SPRT ulong 11 40 const SPC ulong 12 41 const SINC ulong 13 42 const SHASV ulong 14 43 const STYP ulong 15 44 const SINV ulong 16 45 const SINIS ulong 17 46 const SCVL ulong 18 47 const SINSD ulong 19 48 const SSKIP ulong 20 49 const SPASS ulong 21 50 const SWANT ulong 22 51 const SFND ulong 23 52 const SNUM ulong 24 53 const SK ulong 25 54 const SLIST ulong 26 55 const SARGL ulong 27 56 const SNFND ulong 28 57 58 note sarg: the SHEET argument; rid2: the r:id of the sheet at position SHEET when SHEET is a number 59 global sarg uchar 4096 60 global rid2 uchar 1024 61 global rid2l ulong 1 62 63 text kwb "workbook" 64 text ksheets "sheets" 65 text ksheet "sheet" 66 text kname "name" 67 text krid "id" 68 text ksst "sst" 69 text ksi "si" 70 text krph "rPh" 71 text kws "worksheet" 72 text ksd "sheetData" 73 text krow "row" 74 text kc "c" 75 text kv "v" 76 text kis "is" 77 text kref "r" 78 text kty "t" 79 text kts "s" 80 text ktstr "str" 81 text ktinl "inlineStr" 82 text ktb "b" 83 text kte "e" 84 text ktd "d" 85 text ktn "n" 86 text ktrue "TRUE" 87 text kfalse "FALSE" 88 text e83 "no sheet with that name or number" 89 text e84 "that sheet is not a worksheet" 90 text e85 "shared strings above `1048576` strings or `16777216` bytes, or one cell's text above `16777216` bytes" 91 text e86 "shared string index out of range" 92 text e87 "bad cell or row reference (column above XFD, row above `1048576`, or not ascending)" 93 text e88 "unknown cell type, or a boolean that is not 0 or 1" 94 95 fn sreset to void 96 for i ulong 32 97 put ss i 0 98 end 99 end 100 101 note ---- the workbook: mode MWB (find the sheet) and MWL (list the names) ---- 102 note sargeq: the value of attribute i is the SHEET argument. 103 fn sargeq i ulong to bool 104 n = get avlen i 105 if ne n get ss SARGL 106 ret false 107 end 108 o = get avoff i 109 for j ulong n bound 4096 110 if ne get apool add o j get sarg j 111 ret false 112 end 113 end 114 ret true 115 end 116 117 fn cprid2 i ulong to void 118 n = get avlen i 119 if gt n 1024 120 quit 89 e89 121 end 122 o = get avoff i 123 for j ulong n bound 1024 124 put rid2 j get apool add o j 125 end 126 put rid2l 0 n 127 end 128 129 fn wbstart to void 130 d = get xs QDEP 131 if eq d 1 132 if not eis NSS kwb 133 quit 89 e89 134 end 135 ret 136 end 137 if ne d 3 138 ret 139 end 140 if not eis NSS ksheet 141 ret 142 end 143 a = afind NSNO kname 144 r = afind NSR krid 145 if or eq a txnone eq r txnone 146 quit 89 e89 147 end 148 k = add get ss SK 1 149 put ss SK k 150 if eq get ss SLIST 1 151 o = get avoff a 152 for j ulong get avlen a bound 262144 153 oput ulong get apool add o j 154 end 155 oput 10 156 ret 157 end 158 if eq get ss SFND 1 159 ret 160 end 161 if eq get ss SWANT 0 162 cprid r 163 put ss SFND 1 164 ret 165 end 166 if sargeq a 167 cprid r 168 put ss SFND 1 169 ret 170 end 171 if eq get ss SNUM k 172 if eq get ss SNFND 0 173 cprid2 r 174 put ss SNFND 1 175 end 176 end 177 end 178 179 note ---- the Shared String Table: mode MSS ---- 180 fn ssstart to void 181 d = get xs QDEP 182 if eq d 1 183 if not eis NSS ksst 184 quit 89 e89 185 end 186 ret 187 end 188 if and eq d 2 eis NSS ksi 189 k = get ss SCNT 190 if eq k SSMAX 191 quit 85 e85 192 end 193 put sso k get ss SSPL 194 put ss SINSI 1 195 ret 196 end 197 if eq get ss SINSI 0 198 ret 199 end 200 if eis NSS krph 201 put ss SRPH add get ss SRPH 1 202 ret 203 end 204 if and eq get ss SRPH 0 eis NSS kt 205 put ss SINT 1 206 end 207 end 208 209 fn ssend to void 210 if eq get ss SINSI 0 211 ret 212 end 213 if and eq get xs QDEP 2 eis NSS ksi 214 k = add get ss SCNT 1 215 put ss SCNT k 216 put sso k get ss SSPL 217 put ss SINSI 0 218 ret 219 end 220 if eis NSS krph 221 put ss SRPH sub get ss SRPH 1 222 ret 223 end 224 if eis NSS kt 225 put ss SINT 0 226 end 227 end 228 229 fn sstext to void 230 if eq get ss SINT 0 231 ret 232 end 233 p = get ss SSPL 234 n = get xs QTL 235 if gt n sub SSPOOL p 236 quit 85 e85 237 end 238 for i ulong n bound 65536 239 put ssp add p i get tbuf i 240 end 241 put ss SSPL add p n 242 end 243 244 note ---- cell references (A1 style: column letters A..XFD, then row 1..`1048576`) ---- 245 note aref: attribute i as a cell reference: column << 32 | row; refusal 87 when malformed. 246 fn aref i ulong to ulong 247 o = get avoff i 248 n = get avlen i 249 col = ulong 0 250 j = ulong 0 251 while lt j n bound 262144 252 c = ulong get apool add o j 253 if or lt c 65 gt c 90 254 break 255 end 256 col = add mul col 26 sub c 64 257 if gt col MAXCOL 258 quit 87 e87 259 end 260 j = add j 1 261 end 262 if or eq col 0 eq j n 263 quit 87 e87 264 end 265 row = anum o j n 266 ret or shl col 32 row 267 end 268 269 note anum: decimal apool[o+j .. o+n) as a row number 1..`1048576`, else refusal 87. 270 fn anum o ulong j ulong n ulong to ulong 271 v = ulong 0 272 k = j 273 while lt k n bound 262144 274 c = ulong get apool add o k 275 if or lt c 48 gt c 57 276 quit 87 e87 277 end 278 v = add mul v 10 sub c 48 279 if gt v MAXROW 280 quit 87 e87 281 end 282 k = add k 1 283 end 284 if or eq v 0 eq k j 285 quit 87 e87 286 end 287 ret v 288 end 289 290 note ctype: the cell type of attribute t (or n when absent) as 0 n, 1 s, 2 str, 3 inlineStr, 4 b, 5 e, 6 d. 291 fn ctype to ulong 292 a = afind NSNO kty 293 if eq a txnone 294 ret 0 295 end 296 if aveq a ktn false 297 ret 0 298 end 299 if aveq a kts false 300 ret 1 301 end 302 if aveq a ktstr false 303 ret 2 304 end 305 if aveq a ktinl false 306 ret 3 307 end 308 if aveq a ktb false 309 ret 4 310 end 311 if aveq a kte false 312 ret 5 313 end 314 if aveq a ktd false 315 ret 6 316 end 317 quit 88 e88 318 ret 0 319 end 320 321 note ---- the worksheet: modes MS1 (pass 1) and MS2 (pass 2) ---- 322 fn commas k ulong to void 323 for i ulong k bound 16384 324 oput 44 325 end 326 end 327 328 fn crlf to void 329 oput 13 330 oput 10 331 end 332 333 note rowstart: rows must ascend; pass 2 prints the empty rows before this one. 334 fn rowstart to void 335 r = add get ss SPR 1 336 a = afind NSNO kref 337 if ne a txnone 338 r = anum get avoff a 0 get avlen a 339 end 340 if le r get ss SPR 341 quit 87 e87 342 end 343 put ss SROW r 344 put ss SPR r 345 put ss SLC 0 346 put ss SPC 0 347 put ss SSKIP 0 348 if eq get ss SPASS 1 349 ret 350 end 351 m = get ss SMAXR 352 if gt r m 353 put ss SSKIP 1 354 ret 355 end 356 p = add get ss SPRT 1 357 while lt p r bound `1048576` 358 commas sub get ss SMAXC 1 359 crlf 360 p = add p 1 361 end 362 end 363 364 fn rowend to void 365 if eq get ss SPASS 1 366 ret 367 end 368 if eq get ss SSKIP 1 369 ret 370 end 371 k = get ss SMAXC 372 c = get ss SPC 373 if eq c 0 374 commas sub k 1 375 else 376 commas sub k c 377 end 378 crlf 379 put ss SPRT get ss SROW 380 end 381 382 fn cellstart to void 383 c = add get ss SLC 1 384 a = afind NSNO kref 385 if ne a txnone 386 v = aref a 387 c = shr v 32 388 if ne and v 0xffffffff get ss SROW 389 quit 87 e87 390 end 391 end 392 if or le c get ss SLC gt c MAXCOL 393 quit 87 e87 394 end 395 put ss SLC c 396 put ss SCOL c 397 put ss STYP ctype 398 put ss SINC 1 399 put ss SHASV 0 400 put ss SCVL 0 401 end 402 403 note cvnum: cv as a decimal index (refusal 86 when it is not one). 404 fn cvnum to ulong 405 n = get ss SCVL 406 if eq n 0 407 quit 86 e86 408 end 409 v = ulong 0 410 for i ulong n bound `16777216` 411 c = ulong get cv i 412 if or lt c 48 gt c 57 413 quit 86 e86 414 end 415 v = add mul v 10 sub c 48 416 if gt v SSMAX 417 quit 86 e86 418 end 419 end 420 ret v 421 end 422 423 fn cvtext t view uchar * to void 424 n = len t 425 for i ulong n bound 8 426 put cv i get t i 427 end 428 put ss SCVL n 429 end 430 431 note cvfinal: turn a shared string index or a boolean into the text to print. 432 fn cvfinal to void 433 t = get ss STYP 434 if eq t 1 435 k = cvnum 436 if ge k get ss SCNT 437 quit 86 e86 438 end 439 o = get sso k 440 n = sub get sso add k 1 o 441 for i ulong n bound `16777216` 442 put cv i get ssp add o i 443 end 444 put ss SCVL n 445 ret 446 end 447 if eq t 4 448 n = get ss SCVL 449 if eq n 1 450 c = get cv 0 451 if eq c 48 452 cvtext kfalse 453 ret 454 end 455 if eq c 49 456 cvtext ktrue 457 ret 458 end 459 end 460 quit 88 e88 461 end 462 end 463 464 note xsat: the ST_Xstring escape _xHHHH_ at cv[p ..] (end n): its value, or txnone when there is none. 465 fn xsat p ulong n ulong to ulong 466 if gt add p 7 n 467 ret txnone 468 end 469 if or ne get cv p 95 ne get cv add p 1 120 470 ret txnone 471 end 472 if ne get cv add p 6 95 473 ret txnone 474 end 475 v = ulong 0 476 for i ulong 4 477 d = tx_hexv get cv add add p 2 i 478 if ge d 16 479 ret txnone 480 end 481 v = or shl v 4 d 482 end 483 ret v 484 end 485 486 note xsdec: the character at cv[p ..]: (code point << 8) | bytes it takes. An escape gives its code point 487 note (a high and a low surrogate escape together give one; a lone surrogate escape is left as text); 488 note otherwise the byte itself is the "code point" (UTF-8 bytes pass through one at a time). 489 fn xsdec p ulong n ulong to ulong 490 v = xsat p n 491 if eq v txnone 492 ret or shl ulong get cv p 8 1 493 end 494 if and ge v 0xd800 le v 0xdbff 495 w = xsat add p 7 n 496 if or eq w txnone or lt w 0xdc00 gt w 0xdfff 497 ret or shl ulong get cv p 8 1 498 end 499 ret or shl add 0x10000 or shl sub v 0xd800 10 sub w 0xdc00 8 14 500 end 501 if and ge v 0xdc00 le v 0xdfff 502 ret or shl ulong get cv p 8 1 503 end 504 ret or shl or v 0x80000000 8 7 505 end 506 507 note cvquote: the decoded value has a comma, a quote, CR or LF (RFC 4180 2.6). 508 fn cvquote to bool 509 n = get ss SCVL 510 p = ulong 0 511 while lt p n bound `16777216` 512 x = xsdec p n 513 c = and shr x 8 0x7fffffff 514 if or or eq c 44 eq c 34 or eq c 13 eq c 10 515 ret true 516 end 517 p = add p and x 255 518 end 519 ret false 520 end 521 522 note ucp: code point v as UTF-8 to the output. 523 fn ucp v ulong to void 524 if lt v 0x80 525 oput v 526 ret 527 end 528 if lt v 0x800 529 oput or 0xc0 shr v 6 530 oput or 0x80 and v 0x3f 531 ret 532 end 533 if lt v 0x10000 534 oput or 0xe0 shr v 12 535 oput or 0x80 and shr v 6 0x3f 536 oput or 0x80 and v 0x3f 537 ret 538 end 539 oput or 0xf0 shr v 18 540 oput or 0x80 and shr v 12 0x3f 541 oput or 0x80 and shr v 6 0x3f 542 oput or 0x80 and v 0x3f 543 end 544 545 note cvscan: 1 when cv has an underscore (an ST_Xstring escape may be present), plus 2 when it has a comma, 546 note a quote, CR or LF. 547 fn cvscan to ulong 548 n = get ss SCVL 549 r = ulong 0 550 for i ulong n bound `16777216` 551 c = get cv i 552 if eq c 95 553 r = or r 1 554 end 555 if or or eq c 44 eq c 34 or eq c 13 eq c 10 556 r = or r 2 557 end 558 end 559 ret r 560 end 561 562 note ocv: cv[0 .. n) to the output buffer in blocks. 563 fn ocv n ulong to void 564 i = ulong 0 565 while lt i n bound `16777216` 566 m = get on 0 567 k = sub 65536 m 568 if gt k sub n i 569 k = sub n i 570 end 571 for j ulong k bound 65536 572 put obuf add m j get cv add i j 573 end 574 put on 0 add m k 575 i = add i k 576 if eq add m k 65536 577 oflush 578 end 579 end 580 end 581 582 note cvout: the cell value as one RFC 4180 field. Without an underscore the bytes are the value: copied 583 note as they are, or quoted with each quote doubled; otherwise ST_Xstring escapes are decoded first. 584 fn cvout to void 585 n = get ss SCVL 586 r = cvscan 587 if eq r 0 588 ocv n 589 ret 590 end 591 q = cvquote 592 if q 593 oput 34 594 end 595 p = ulong 0 596 while lt p n bound `16777216` 597 x = xsdec p n 598 k = and x 255 599 c = and shr x 8 0x7fffffff 600 if eq k 1 601 oput c 602 else 603 ucp c 604 end 605 if and q eq c 34 606 oput 34 607 end 608 p = add p k 609 end 610 if q 611 oput 34 612 end 613 end 614 615 fn cellend to void 616 put ss SINC 0 617 if eq get ss SHASV 0 618 ret 619 end 620 c = get ss SCOL 621 if eq get ss SPASS 1 622 if gt c get ss SMAXC 623 put ss SMAXC c 624 end 625 if gt get ss SROW get ss SMAXR 626 put ss SMAXR get ss SROW 627 end 628 ret 629 end 630 if eq get ss SSKIP 1 631 ret 632 end 633 cvfinal 634 p = get ss SPC 635 if eq p 0 636 commas sub c 1 637 else 638 commas sub c p 639 end 640 cvout 641 put ss SPC c 642 end 643 644 fn shstart to void 645 d = get xs QDEP 646 if eq d 1 647 if not eis NSS kws 648 quit 89 e89 649 end 650 ret 651 end 652 if eq get ss SINSD 0 653 if and eq d 2 eis NSS ksd 654 put ss SINSD 1 655 end 656 ret 657 end 658 if eq d 3 659 if eis NSS krow 660 rowstart 661 end 662 ret 663 end 664 if eq d 4 665 if eis NSS kc 666 cellstart 667 end 668 ret 669 end 670 if eq get ss SINC 0 671 ret 672 end 673 if eq d 5 674 if eis NSS kv 675 put ss SHASV 1 676 put ss SINV 1 677 ret 678 end 679 if eis NSS kis 680 put ss SHASV 1 681 put ss SINIS 1 682 end 683 ret 684 end 685 if eq get ss SINIS 0 686 ret 687 end 688 if eis NSS krph 689 put ss SRPH add get ss SRPH 1 690 ret 691 end 692 if and eis NSS kt eq get ss SRPH 0 693 put ss SINT 1 694 end 695 end 696 697 fn shend to void 698 d = get xs QDEP 699 if eq get ss SINSD 0 700 ret 701 end 702 if eq d 2 703 put ss SINSD 0 704 ret 705 end 706 if eq d 3 707 if eis NSS krow 708 rowend 709 end 710 ret 711 end 712 if eq d 4 713 if eq get ss SINC 1 714 cellend 715 end 716 ret 717 end 718 if eq get ss SINC 0 719 ret 720 end 721 if eq d 5 722 put ss SINV 0 723 put ss SINIS 0 724 ret 725 end 726 if eis NSS krph 727 put ss SRPH sub get ss SRPH 1 728 ret 729 end 730 if eis NSS kt 731 put ss SINT 0 732 end 733 end 734 735 fn shtext to void 736 if eq get ss SPASS 1 737 ret 738 end 739 if eq get ss SINV 0 740 if or eq get ss SINT 0 ne get ss SRPH 0 741 ret 742 end 743 end 744 p = get ss SCVL 745 n = get xs QTL 746 if gt n sub XCV p 747 quit 85 e85 748 end 749 for i ulong n bound 65536 750 put cv add p i get tbuf i 751 end 752 put ss SCVL add p n 753 end