-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcl-xlsx-reference.lisp
More file actions
579 lines (517 loc) · 26.9 KB
/
cl-xlsx-reference.lisp
File metadata and controls
579 lines (517 loc) · 26.9 KB
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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
;;;; cl-xlsx.lisp
(in-package #:cl-xlsx)
(defun starts-with-p (str substring)
"String starts/begins with substring?"
(let ((str-len (length str))
(sub-len (length substring)))
(and (>= str-len sub-len)
(string= substring (subseq str 0 sub-len)))))
(defun ends-with-p (str substring)
"String ends with substring?"
(let ((str-len (length str))
(sub-len (length substring)))
(and (>= str-len sub-len)
(string= substring (subseq str (- str-len sub-len) str-len)))))
(defun flatten (l &key (acc '()))
"Flatten a list - to avoid dependency."
(cond ((null l) acc)
((atom (car l)) (flatten (cdr l) :acc (append acc (list (car l)))))
(t (flatten (cdr l) :acc (append acc (flatten (car l)))))))
;;;-----------------------------------------------------------------------------
;;; cxml
;;;-----------------------------------------------------------------------------
(defun source-entry (xml xlsx)
"Get content xml file inside xlsx."
(zip:with-zipfile (zip xlsx)
(let ((entry (zip:get-zipfile-entry xml zip)))
(when entry
(cxml:make-source (babel:octets-to-string (zip:zipfile-entry-contents entry)))))))
;;;-----------------------------------------------------------------------------
;;; date/time stuff
;;;-----------------------------------------------------------------------------
(defun excel-date-serial-number-to-timestamp (dsn &optional (timezone local-time:*default-timezone*))
(local-time:adjust-timestamp
(local-time:encode-timestamp 0 1 0 0 1 1 1900 :timezone timezone)
(offset :day (1- (if (< dsn 60)
dsn
(1- dsn))))))
(defun decode-excel-date-serial-number (dsn)
(local-time:with-decoded-timestamp
(:year year :month month :day day :timezone local-time:+utc-zone+)
(excel-date-serial-number-to-timestamp dsn)
(values year month day)))
(defun print-iso-date (lt-date &optional stream (timezone local-time:*default-timezone*))
(local-time:format-timestring stream lt-date
:format local-time:+iso-8601-date-format+
:timezone timezone))
;;;-----------------------------------------------------------------------------
;;; xpath stuff
;;;-----------------------------------------------------------------------------
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter *package-relationships-namespace*
"http://schemas.openxmlformats.org/package/2006/relationships")
(defparameter *default-package-relationships-namespace*
`(("" ,*package-relationships-namespace*)))
(defparameter *office-document-relationships-namespace*
"http://schemas.openxmlformats.org/officeDocument/2006/relationships")
(defparameter *default-xlsx-namespaces*
`(("" "http://schemas.openxmlformats.org/spreadsheetml/2006/main")
("mc" "http://schemas.openxmlformats.org/markup-compatibility/2006")
("r" ,*office-document-relationships-namespace*)
("x15" "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main")
("xr" "http://schemas.microsoft.com/office/spreadsheetml/2014/revision")
("xr2" "http://schemas.microsoft.com/office/spreadsheetml/2015/revision2")
("xr6" "http://schemas.microsoft.com/office/spreadsheetml/2016/revision6")
("xr10" "http://schemas.microsoft.com/office/spreadsheetml/2016/revision10"))))
(defmacro with-package-relationships-namespaces ((&optional extra-namespaces) &body body)
`(xpath:with-namespaces ,(append *default-package-relationships-namespace*
extra-namespaces)
,@body))
(defmacro with-xlsx-namespaces ((&optional extra-namespaces) &body body)
`(xpath:with-namespaces ,(append *default-xlsx-namespaces* extra-namespaces)
,@body))
(defun guess-date-format-p (fmt-string)
(and (find #\D fmt-string :test 'char-equal)
(find #\M fmt-string :test 'char-equal)
(find #\Y fmt-string :test 'char-equal)))
(defun find-user-defined-style (styles style-num)
(let ((user-defined-style (cdr (assoc (elt (first styles) style-num)
(second styles)))))
(when user-defined-style
(cond ((guess-date-format-p user-defined-style)
:date)
(t :general)))))
(defun get-built-in-style-type (style-num)
(cond ((zerop style-num)
:general)
((or (<= 1 style-num 4)
(<= 37 style-num 40)
(= style-num 48))
:numeric)
((<= 9 style-num 10)
:percent)
((<= 14 style-num 17)
:date)
((or (<= 18 style-num 21)
(<= 45 style-num 47))
:time)
((= 22 style-num)
:date-time)
(t :general)))
(defun excel-col-index (col)
(flet ((char-num (char)
(1+ (- (char-code (char-upcase char)) (char-code #\A)))))
(1- (reduce (lambda (x y)
(+ (* x 26) (char-num y))) col :initial-value 0))))
(defun process-cell-node (node unique-strings styles current-col)
"Return value of cell node. Checks type 't' and if string 's', looks
up from unique-strings the right string. If numeric 'n', then
parses it using parse-number:parse-number, Otherwise return the
cell value directly."
(declare (optimize (debug 3)))
(with-xlsx-namespaces ()
(let ((element (fxml.stp:first-child node)))
(fxml.stp:with-attributes ((pos "r")
(cell-style "s")
(cell-type "t"))
element
(let ((val (let ((value-node (xpath:first-node (xpath:evaluate "v/text()" element))))
(cond ((equalp cell-type "s")
(elt unique-strings (parse-integer (xpath:string-value value-node))))
((equalp cell-type "n")
(let ((cell-value
(parse-number:parse-number (xpath:string-value value-node))))
(if cell-style
(let ((cell-style-num (parse-integer cell-style)))
(if cell-style-num
(let ((cell-style
(or (find-user-defined-style styles cell-style-num)
(get-built-in-style-type cell-style-num))))
(case cell-style
((:date :date-time)
(excel-date-serial-number-to-timestamp
cell-value))
(t cell-value)))
cell-value))
cell-value)))
(t
(when value-node
(xpath:string-value value-node)))))))
(let* ((pos-first-num (position-if #'digit-char-p pos))
(col (subseq pos 0 pos-first-num))
(col-index (excel-col-index col)))
(if (> col-index current-col)
(append (make-list (- col-index current-col))
(list val))
(list val))))))))
;;;-----------------------------------------------------------------------------
;;; sax parsing
;;;-----------------------------------------------------------------------------
(defun sax-attributes (sax)
(second sax))
(defun sax-attribute (sax attribute)
"Return value of attribute in sax."
(let ((attributes (sax-attributes sax)))
(car (last (assoc attribute attributes :key #'car :test #'string=)))))
;;; extractor functions for sax-parsed cell
(defun sax-cell-value (sax)
"Return value of sax-parsed cell tag (in child tag :v)."
(car (last (car (last sax)))))
(defun sax-cell-type (sax)
"Return type of sax-parsed excel cell tag (attribute 't')."
(cadr (assoc "t" (sax-attributes sax) :test #'equal)))
(defun sax-cell-pos (sax)
"Return position information of sax-parsed excel cell tag (attribute 'r')."
(cadr (assoc "r" (sax-attributes sax) :test #'equal)))
(defun process-sax-cell (sax unique-strings)
"Return value of sax-parsed excel cell tag. Checks type 't'
and if string 's', looks up from unique-strings the right string.
If numeric 'n', then parses it using lisp-reader.
Otherwise return string."
(let ((val-type (sax-cell-type sax)))
(cond ((equalp val-type "s") (elt unique-strings (parse-integer (sax-cell-value sax))))
((equalp val-type "n") (parse-number:parse-number (sax-cell-value sax)))
(t (sax-cell-value sax)))))
;;;-----------------------------------------------------------------------------
;;; read xlsx
;;;-----------------------------------------------------------------------------
(defun inner-files (xlsx)
"List all innter addresses in xlsx."
(zip:with-zipfile (inzip xlsx)
(let ((entries (zip:zipfile-entries inzip)))
(when entries
(loop :for k :being :the :hash-keys :of entries
:collect k)))))
(defun get-relationships (xlsx)
"Returns sheet relationships as list of lists (...)."
(let ((rels (fxml:parse (source-entry-stream "xl/_rels/workbook.xml.rels" xlsx)
(fxml.stp:make-builder))))
(with-package-relationships-namespaces ()
(xpath:map-node-set->list
(lambda (rel-node)
(fxml.stp:with-attributes ((id "Id")
(type "Type")
(target "Target"))
rel-node
(list id type target)))
(xpath:evaluate "/Relationships/Relationship" rels)))))
(defun get-sheets (xlsx)
"Return sheet informations as list of lists (sheet-name sheet-number sheet-id sheet-address)."
(let ((relationships (get-relationships xlsx)))
(let ((workbook (fxml:parse (source-entry-stream "xl/workbook.xml" xlsx)
(fxml.stp:make-builder))))
(with-xlsx-namespaces ()
(xpath:map-node-set->list
(lambda (sheet-node)
(fxml.stp:with-attributes ((r-id "r:id" *office-document-relationships-namespace*)
(sheet-id "sheetId")
(name "name"))
sheet-node
(let ((related-item (find r-id relationships :key #'car :test 'equal)))
(list name sheet-id r-id (elt related-item 2)))))
(xpath:evaluate "/workbook/sheets/sheet" workbook))))))
(defun sheets (xlsx)
"Return sheet informations as list of lists (sheet-name sheet-number sheet-address)."
(klacks:with-open-source (src (source-entry "xl/workbook.xml" xlsx))
(loop :for key = (klacks:peek src)
:for consumed = nil
:while key
:nconcing (case key
(:start-element
(let ((tag-name (klacks:current-qname src)))
(cond ((equal tag-name "sheet")
(setf consumed t)
(list (let* ((sax (klacks:serialize-element src (cxml-xmls:make-xmls-builder)))
(attributes (cadr sax)))
(list (cadr (assoc "name" attributes :test #'equal))
(parse-integer (cadr (assoc "sheetId" attributes :test #'equal)))
(concatenate 'string "xl/worksheets/sheet"
(cadr (assoc "sheetId" attributes :test #'equal))
".xml")))))
(t nil))))
(otherwise nil))
:do (unless consumed
(klacks:consume src)))))
;; (defun sheets (xlsx)
;; "Return sheet informations as list of lists (name sheet-number sheetaddress."
;; (let ((sheet-addresses (remove-if-not #'(lambda (x) (and (starts-with-p x "xl/worksheets/")
;; (ends-with-p x ".xml")))
;; (inner-files xlsx))))
;; (klacks:with-open-source (src (source-entry "xl/workbook.xml" xlsx))
;; (loop for key = (klacks:peek src)
;; while key
;; nconc (case key
;; (:start-element
;; (if (equal (klacks:current-qname src) "sheet")
;; (list (let* ((sax (klacks:serialize-element src (cxml-xmls:make-xmls-builder)))
;; (attributes (cdadr sax)))
;; (list (cadr (assoc "name" attributes :test #'equal))
;; (parse-integer (cadr (assoc "sheetId" attributes :test #'equal)))
;; (pop sheet-addresses))))
;; nil))
;; (otherwise nil))
;; do (klacks:consume src)))))
;; works
(defun sheet-names-xlsx (xlsx)
"List sheet names in xlsx file."
(mapcar #'car (sheets xlsx)))
(defun sheet-address (sheet xlsx)
"Return sheet ID when sheet name or index given as input. Note that
this is not the same as the user-visible sheet number and should not
be used as an offset into the list of sheets. Use get-sheets to get
more detailed information on sheets for that."
(typecase sheet
(string (caddr (assoc sheet (sheets xlsx) :test #'string=)))
(integer (cadr (assoc sheet (mapcar #'cdr (sheets xlsx)))))))
(defun source-entry-stream (xml xlsx)
"Get content xml file inside xlsx."
(zip:with-zipfile (zip xlsx)
(let ((entry (zip:get-zipfile-entry xml zip)))
(when entry
(zip:zipfile-entry-contents entry)))))
(defun get-unique-strings (xlsx)
(let ((doc (fxml:parse (source-entry-stream "xl/sharedStrings.xml" xlsx)
(fxml.stp:make-builder))))
(with-xlsx-namespaces ()
(xpath:map-node-set->list
(lambda (x)
(let ((rich-text-runs (xpath:evaluate "r/t/text()" x)))
(if (not (xpath:node-set-empty-p rich-text-runs))
(progn
(apply #'concatenate
'string
(xpath:map-node-set->list #'xpath:string-value rich-text-runs)))
(let ((text (xpath:string-value (xpath:evaluate "t/text()" x))))
(if text
text
)))))
(xpath:evaluate "/sst/si" doc)))))
(defun get-doc-number-formats (doc)
(with-xlsx-namespaces ()
(xpath:map-node-set->list
(lambda (x)
(fxml.stp:with-attributes ((format-code "formatCode")
(number-format-id "numFmtId"))
x
(cons (parse-integer number-format-id) format-code)))
(xpath:evaluate "/styleSheet/numFmts/numFmt" doc))))
(defun get-doc-cell-formats (doc)
(with-xlsx-namespaces ()
(xpath:map-node-set->list
(lambda (x)
(fxml.stp:with-attributes ((number-format-id "numFmtId"))
x
(parse-integer number-format-id)))
(xpath:evaluate "/styleSheet/cellXfs/xf" doc))))
(defun get-styles (xlsx)
(let ((doc (fxml:parse (source-entry-stream "xl/styles.xml" xlsx)
(fxml.stp:make-builder))))
(list (get-doc-cell-formats doc)
(get-doc-number-formats doc))))
(defun parse-xlsx-sheet (sheet-address xlsx &key unique-strings styles keep-empty-rows-as-nil)
"Return parsed content for a given sheet in a xlsx-fpath."
(klacks:with-open-source (s (source-entry (concatenate 'string "xl/" sheet-address) xlsx))
(let ((unique-strings (or unique-strings
(get-unique-strings xlsx))))
(loop :for key = (klacks:peek s)
:with row-counter = 1
:while key
:nconcing (case key
(:start-element
(let ((tag-name (klacks:current-qname s)))
(cond ((equal tag-name "row")
(let ((row-num (parse-number:parse-number (klacks:get-attribute s "r"))))
(loop :for key = (klacks:peek s)
:for consumed = nil
:with i = 0
:while key
:nconcing (case key
(:start-element
(cond ((equal (klacks:current-qname s) "c")
(setf consumed t)
(let ((cols
(progn
(process-cell-node
(klacks:serialize-element
s
(fxml.stp:make-builder))
unique-strings
styles
i))))
(incf i (length cols))
cols))
(t
(setf consumed nil)
nil)))
(:end-element
(when (equal (klacks:current-qname s) "row")
(let ((pad-rows (- row-num row-counter)))
(incf row-counter (1+ pad-rows))
(return (append (when keep-empty-rows-as-nil
(make-list pad-rows))
(list res)))))))
:into res
:do (unless consumed
(klacks:consume s)))))
(t nil))))
(otherwise nil))
:do (klacks:consume s))))) ;; works!
(defun parse-xlsx (xlsx &key keep-empty-rows-as-nil)
"Parse every sheet of xlsx and return as alist (sheet-name sheet-content-as-list)."
(let ((sheets (get-sheets xlsx))
(unique-strings (get-unique-strings xlsx))
(styles (get-styles xlsx)))
(mapcar #'(lambda (sheet)
(destructuring-bind (sheet-name sheet-number sheet-id sheet-address)
sheet
(declare (ignore sheet-number sheet-id))
(cons sheet-name (parse-xlsx-sheet sheet-address xlsx
:unique-strings unique-strings
:styles styles
:keep-empty-rows-as-nil keep-empty-rows-as-nil))))
sheets)))
;;;-----------------------------------------------------------------------------
;;; read ods
;;;-----------------------------------------------------------------------------
(defun parse-ods-cell (sax)
"Return string or number according to type of ods cell sax."
(let ((type (car (last (first (second sax)))))
(value (car (last (third sax)))))
(cond ((equalp type "float") (parse-number:parse-number value))
;; since we return the same for "string" and the default
;; case, no need to check "string" here.
;;
;; ((equalp type "string") value)
(t value))))
(defun sheet-names-ods (ods)
"Return sheet names."
(klacks:with-open-source (s (source-entry "content.xml" ods))
(loop :for key = (klacks:peek s)
:for consumed = nil
:while key
:nconcing (case key
(:start-element
(when (equal (klacks:current-qname s) "table:table")
(setf consumed t)
(let ((sax (klacks:serialize-element s (cxml-xmls:make-xmls-builder))))
(list (sax-attribute sax "name"))))))
:do (unless consumed
(klacks:consume s))))) ; works!
(defun number-columns-repeated-cell-p (table-cell-sax)
"Tester whether 'number-columns-repeated' table-cell-sax or not."
(equal (car (caaadr table-cell-sax)) "number-columns-repeated"))
;; requires flatten above
(defun all-nil-row-p (l)
(every #'null (flatten l)))
(defun parse-ods (ods)
"Return parsed content for xlsx-fpath."
(klacks:with-open-source (s (source-entry "content.xml" ods))
(loop :for key = (klacks:peek s)
:while key
:nconcing (case key
(:start-element
(cond ((equal (klacks:current-qname s) "table:table")
(loop :for key1 = (klacks:peek s)
:while key1
:nconcing (case key1
(:start-element
(cond ((equal (klacks:current-qname s) "table:table-row")
(loop :for key2 = (klacks:peek s)
:for consumed = nil
:while key2
:nconcing (case key2
(:start-element
(cond ((equal (klacks:current-qname s) "table:table-cell")
(setf consumed t)
(let ((sax (klacks:serialize-element s (cxml-xmls:make-xmls-builder))))
(if (number-columns-repeated-cell-p sax)
nil
(list (parse-ods-cell sax))))) ; this corrected the final NIL's in row
(t
(setf consumed nil)
nil)))
(:end-element
(if (equal (klacks:current-qname s) "table:table-row")
(return (if (all-nil-row-p inner-res) ; this is a hack to get rid of tags at end of sheet
nil ; remove NIL-only row constructs
(list inner-res)))))
(otherwise nil))
:into inner-res
:do (unless consumed
(klacks:consume s))))
(t nil)))
(:end-element
(if (equal (klacks:current-qname s) "table:table")
(return (list res))))
(otherwise nil))
:into res
:do (klacks:consume s)))
(t nil)))
(otherwise nil))
:do (klacks:consume s))))
;;;-----------------------------------------------------------------------------
;;; read xlsx/ods recognizing automatically
;;;-----------------------------------------------------------------------------
(defun app-name (xlsx)
"Return app-name of xlsx or ods/ots file."
(let ((inner-files (inner-files xlsx)))
(cond ((member "meta.xml" inner-files :test #'string=)
(let ((src (source-entry "meta.xml" xlsx)))
(klacks:find-element src "generator")
(car (last (klacks:serialize-element src (cxml-xmls:make-xmls-builder))))))
((member "docProps/app.xml" inner-files :test #'string=)
(let ((src (source-entry "docProps/app.xml" xlsx)))
(klacks:find-element src "Application")
(car (last (klacks:serialize-element src (cxml-xmls:make-xmls-builder))))))
(t
"")))) ; works!
(defun app-type (xlsx)
"Return type of xlsx file - ods or odt included."
(let ((inner-files (inner-files xlsx)))
(cond ((starts-with-p (app-name xlsx) "LibreOffice")
(if (member "meta.xml" inner-files :test #'string=)
"ods-libreoffice"
"xlsx-libreoffice"))
((or (starts-with-p (app-name xlsx) "Microsoft Excel")
(starts-with-p (app-name xlsx) "Microsoft Macintosh Excel")
(starts-with-p (app-name xlsx) "WPS 表格")
(starts-with-p (app-name xlsx) "Apache POI"))
"xlsx-microsoft"))))
(defun sheet-names (xlsx)
"Return sheet-names of xlsx."
(let ((type (app-type xlsx)))
(cond ((or (string= type "xlsx-microsoft")
(string= type "xlsx-libreoffice"))
(sheet-names-xlsx xlsx))
((string= type "ods-libreoffice")
(sheet-names-ods xlsx))
(t nil))))
(defun read-xlsx (xlsx &key keep-empty-rows-as-nil)
"Read xlsx and ods file with all sheets into a list of list of lists -
recognizing automatically type of file (xlsx or ods/odt)."
(let ((type (app-type xlsx)))
(cond ((or (string= type "xlsx-microsoft")
(string= type "xlsx-libreoffice"))
(parse-xlsx xlsx :keep-empty-rows-as-nil keep-empty-rows-as-nil))
((string= type "ods-libreoffice")
(loop :for sheet-name :in (sheet-names-ods xlsx)
:for sheet-content :in (parse-ods xlsx)
:collect (cons sheet-name sheet-content)))
(t nil))))
;; (defun read-xlsx (xlsx &key (sheet nil))
;; "Read xlsx and ods file with all sheets into a list of list of lists -
;; recognizing automatically type of file (xlsx or ods/odt)."
;; (let ((type (app-type xlsx)))
;; (if (null sheet)
;; (cond ((or (string= type "xlsx-microsoft")
;; (string= type "xlsx-libreoffice"))
;; (cond ((null sheet) (parse-xlsx xlsx))
;; ((atom sheet) (parse-xlsx-sheet xlsx))
;; ((consp sheet) (mapcar
;; )
;; ((string= type "ods-libreoffice")
;; (loop for sheet-name in (sheet-names-ods xlsx)
;; for sheet-content in (parse-ods xlsx)
;; collect (cons sheet-name sheet-content)))
;; (t nil))
;; (cond (())))))