interplane.Get Omega
Read in your language

omega-office

Omega office documents: read and write office files in Omega

Brought by drakestapleton

branch main Branches Tags History

omega-office / src / xlsx.om

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