ooxml

Changes On Branch populationspeed
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch populationspeed Excluding Merge-Ins

This is equivalent to a diff from 5cebb3f58f to 56d2fef577

2019-08-20
08:00
Version-1.3 check-in: 3e16defd13 user: alex tags: trunk, release, version-1.3
2019-08-16
21:24
merged from initnodecmds Leaf check-in: 56d2fef577 user: alex tags: populationspeed
21:15
execute initialization of node commands centrally and only once Closed-Leaf check-in: 1625832b7b user: alex tags: initnodecmds
20:06
merged from garbagecollection check-in: dd54fcbd9a user: alex tags: populationspeed
2019-08-13
21:10
Reworked the option parsing of the cell method for speed. check-in: 6c6aa2f98f user: rolf tags: populationspeed
2019-08-08
22:57
Added a simple bench test file, to have at least something handy within the source tree. Leaf check-in: 5cebb3f58f user: rolf tags: speed
22:09
Found and fixed one major problem: The code already sorts all cells once. Just store the results and use them easily looked up later. Still the # of milliseconds per cell needed while writing goes up slightly with the # of cells to write. But it's much better now. See [20c5ed5f11]. check-in: 998892470a user: rolf tags: speed

Changes to examples/sample1.tcl.

1
2
3
4

5
6

7
8
9
10
11
12
13
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"


#package require ooxml
source ../ooxml.tcl


source array.tcl

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  set center [$spreadsheet style -horizontal center]
  set date [$spreadsheet style -numfmt [$spreadsheet numberformat -datetime]]




>
|
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

source array.tcl

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  set center [$spreadsheet style -horizontal center]
  set date [$spreadsheet style -numfmt [$spreadsheet numberformat -datetime]]

Changes to examples/sample2.tcl.

1
2
3
4

5
6

7
8
9
10
11
12
13
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"


#package require ooxml
source ../ooxml.tcl


source array.tcl

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  set center [$spreadsheet style -horizontal center]
  set date [$spreadsheet style -numfmt [$spreadsheet numberformat -datetime]]




>
|
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

source array.tcl

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  set center [$spreadsheet style -horizontal center]
  set date [$spreadsheet style -numfmt [$spreadsheet numberformat -datetime]]

Changes to examples/sample3.tcl.

1
2
3
4
5
6
7

8
9

10
11
12
13
14
15
16
#!/bin/sh
#\
exec wish8.6 "$0" "$@"

package require Tk
package require tablelist


#package require ooxml
source ../ooxml.tcl


source array.tcl

# build Tablelist from array

set lb .lb
tablelist::tablelist $lb -height 16 -width 100 -showseparators 1 -titlecolumns 3 -labelcommand tablelist::sortByColumn







>
|
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/sh
#\
exec wish8.6 "$0" "$@"

package require Tk
package require tablelist

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

source array.tcl

# build Tablelist from array

set lb .lb
tablelist::tablelist $lb -height 16 -width 100 -showseparators 1 -titlecolumns 3 -labelcommand tablelist::sortByColumn

Changes to examples/sample4.tcl.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

lappend auto_path .
if {[catch {package require ooxml}]} {
    source ../ooxml.tcl
}

source array.tcl

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  $spreadsheet row $sheet




|

|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

source array.tcl

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  $spreadsheet row $sheet

Changes to examples/sample5.tcl.

1
2
3
4

5
6

7
8
9
10
11
12
13
14
15
16
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"


#package require ooxml
source ../ooxml.tcl


set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  set date [$spreadsheet style -numfmt [$spreadsheet numberformat -datetime]]
  $spreadsheet defaultdatestyle $date
  # 2018-03-02 17:39 -> 43161.73542
  $spreadsheet cell $sheet "2018-03-02 17:39" -index 0,0
  $spreadsheet write export5.xlsx
}
$spreadsheet destroy




>
|
|
>










1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Tabelle 1}]] > -1} {
  set date [$spreadsheet style -numfmt [$spreadsheet numberformat -datetime]]
  $spreadsheet defaultdatestyle $date
  # 2018-03-02 17:39 -> 43161.73542
  $spreadsheet cell $sheet "2018-03-02 17:39" -index 0,0
  $spreadsheet write export5.xlsx
}
$spreadsheet destroy

Changes to examples/sample6.tcl.

1
2
3
4

5
6

7
8
9
10
11
12
13
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"


#package require ooxml
source ../ooxml.tcl


set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Blatt 1}]] > -1} {
  set bold [$spreadsheet style -font [$spreadsheet font -bold]]
  set italic [$spreadsheet style -font [$spreadsheet font -italic]]
  set underline [$spreadsheet style -font [$spreadsheet font -underline]]





>
|
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

set spreadsheet [::ooxml::xl_write new -creator {Alexander Schöpe}]
if {[set sheet [$spreadsheet worksheet {Blatt 1}]] > -1} {
  set bold [$spreadsheet style -font [$spreadsheet font -bold]]
  set italic [$spreadsheet style -font [$spreadsheet font -italic]]
  set underline [$spreadsheet style -font [$spreadsheet font -underline]]

Changes to examples/sample7.tcl.

1
2
3
4

5
6

7
8
9
10
11
12
13
14
15
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"


#package require ooxml
source ../ooxml.tcl


array set workbook [ooxml::xl_read original_excel.xlsx]

set spreadsheet [::ooxml::xl_write new]
$spreadsheet presetstyles workbook
$spreadsheet presetsheets workbook
$spreadsheet write export7.xlsx
$spreadsheet destroy





>
|
|
>









1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

array set workbook [ooxml::xl_read original_excel.xlsx]

set spreadsheet [::ooxml::xl_write new]
$spreadsheet presetstyles workbook
$spreadsheet presetsheets workbook
$spreadsheet write export7.xlsx
$spreadsheet destroy

Changes to examples/sample8.tcl.

1
2
3
4

5
6

7
8
9
10
11
12
13
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"


#package require ooxml
source ../ooxml.tcl


array set workbook [ooxml::xl_read form8.xlsx]

set data(NAME) {Erika Mustermann}
set data(ANSCHRIFT) {Heidestrasse 17}
set data(PLZORT) {51147 Köln}
set data(positionen) 3




>
|
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

array set workbook [ooxml::xl_read form8.xlsx]

set data(NAME) {Erika Mustermann}
set data(ANSCHRIFT) {Heidestrasse 17}
set data(PLZORT) {51147 Köln}
set data(positionen) 3

Added examples/sample9.tcl.



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh
#\
exec tclsh8.6 "$0" "$@"

set auto_path [linsert $auto_path 0 ..]
if {[catch {package require ooxml}]} {
  source ../ooxml.tcl
}

set spreadsheet [::ooxml::xl_write new -creator {User A} -created {2019-08-10 10:01:30} -modifiedby {User B} -modified {2019-08-10 12:30:01} -application {Tcl Example Script 9}]
set wrap [$spreadsheet style -wrap]

if {[set sheet [$spreadsheet worksheet {Sheet 1}]] > -1} {
  $spreadsheet cell $sheet {this text will be automatically wrapped by excel} -index A1 -style $wrap
  $spreadsheet write export9.xlsx
}
$spreadsheet destroy

Changes to ooxml.tcl.

94
95
96
97
98
99
100




101
102
103
104
105
106
107
#   return workbookData
# 
# 
# ::ooxml::xl_write
# 
#   constructor args
#     -creator CREATOR




#     return class
# 
#   method numberformat args
#     -format FORMAT -general -date -time -datetime -iso8601 -number -decimal -red -separator -fraction -scientific -percent -text -string
#     return NUMFMTID
#
#   method defaultdatestyle STYLEID







>
>
>
>







94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#   return workbookData
# 
# 
# ::ooxml::xl_write
# 
#   constructor args
#     -creator CREATOR
#     -created UTC-TIMESTAMP
#     -modifiedby NAME
#     -modified UTC-TIMESTAMP
#     -application NAME
#     return class
# 
#   method numberformat args
#     -format FORMAT -general -date -time -datetime -iso8601 -number -decimal -red -separator -fraction -scientific -percent -text -string
#     return NUMFMTID
#
#   method defaultdatestyle STYLEID
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
# ::ooxml::tablelist_to_xl lb args
#   -callback CALLBACK -path PATH -file FILENAME -creator CREATOR -name NAME -rootonly -addtimestamp -globalstyle
#   Callback arguments
#     spreadsheet sheet maxcol column title width align sortmode hide
#


package require Tcl 8.6
package require vfs::zip
package require tdom 0.9.0-
package require msgcat


namespace eval ::ooxml {
  namespace export xl_sheets xl_read xl_write

  variable defaults

  variable predefNumFmts
  variable predefColors
  variable predefColorsName
  variable predefColorsARBG
  variable predefBorderLineStyles
  variable predefPatternType








|
<








>







159
160
161
162
163
164
165
166

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# ::ooxml::tablelist_to_xl lb args
#   -callback CALLBACK -path PATH -file FILENAME -creator CREATOR -name NAME -rootonly -addtimestamp -globalstyle
#   Callback arguments
#     spreadsheet sheet maxcol column title width align sortmode hide
#


package require Tcl 8.6.7-

package require tdom 0.9.0-
package require msgcat


namespace eval ::ooxml {
  namespace export xl_sheets xl_read xl_write

  variable defaults
  variable initNodeCmds
  variable predefNumFmts
  variable predefColors
  variable predefColorsName
  variable predefColorsARBG
  variable predefBorderLineStyles
  variable predefPatternType

457
458
459
460
461
462
463












































































464
465
466
467
468
469
470
  # zh - Chinese - 中文 (Zhōngwén), 汉语, 漢語
  msgcat::mcset zh LANGUAGE \u4e2d\u6587
  msgcat::mcset zh Book \u5de5\u4f5c\u7c3f
  msgcat::mcset zh Worksheets \u5de5\u4f5c\u8868
  msgcat::mcset zh Sheet \u5de5\u4f5c\u8868
}














































































proc ::ooxml::Default { name value } {
  variable defaults

  switch -- $name {
    path {
      set defaults($name) [string trim $value]







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
  # zh - Chinese - 中文 (Zhōngwén), 汉语, 漢語
  msgcat::mcset zh LANGUAGE \u4e2d\u6587
  msgcat::mcset zh Book \u5de5\u4f5c\u7c3f
  msgcat::mcset zh Worksheets \u5de5\u4f5c\u8868
  msgcat::mcset zh Sheet \u5de5\u4f5c\u8868
}

# ooxml::timet_to_dos
#
#        Convert a unix timestamp into a DOS timestamp for ZIP times.
#
#   DOS timestamps are 32 bits split into bit regions as follows:
#                  24                16                 8                 0
#   +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
#   |Y|Y|Y|Y|Y|Y|Y|m| |m|m|m|d|d|d|d|d| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s|
#   +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
#
# From tcllib / zipfile::mkzip      
proc ::ooxml::timet_to_dos {time_t} {
  set s [clock format $time_t -format {%Y %m %e %k %M %S}]
  scan $s {%d %d %d %d %d %d} year month day hour min sec
  expr {(($year-1980) << 25) | ($month << 21) | ($day << 16) 
        | ($hour << 11) | ($min << 5) | ($sec >> 1)}
}

# ooxml::add_str_to_archive --
#
#        Add a string as a single file with string as content with
#        argument path to a zip archive. The zipchan channel must
#        already be open and binary. The return value is the central
#        directory record that will need to be used when finalizing
#        the zip archive.
#
# Derived from tcllib / zipfile::mkzip::add_file_to_archive
proc ::ooxml::add_str_to_archive {zipchan path data {comment ""}} {
  set mtime [timet_to_dos [clock seconds]]
  set utfpath [encoding convertto utf-8 $path]
  set utfcomment [encoding convertto utf-8 $comment]
  set flags [expr {(1<<11)}] ;# utf-8 comment and path
  set method 0               ;# store 0, deflate 8
  set attr 0                 ;# text or binary (default binary)
  set version 20             ;# minumum version req'd to extract
  set extra ""
  set crc 0
  set size 0
  set csize 0
  set seekable [expr {[tell $zipchan] != -1}]
  set attrex 0x81b60020  ;# 0o100666 (-rw-rw-rw-)
  
  set utfdata [encoding convertto utf-8 $data]
  set size [string length $utfdata]
  
  set offset [tell $zipchan]
  set local [binary format a4sssiiiiss PK\03\04 \
                 $version $flags $method $mtime $crc $csize $size \
                 [string length $utfpath] [string length $extra]]
  append local $utfpath $extra
  puts -nonewline $zipchan $local
  
  set crc [::zlib crc32 $utfdata]
  set cdata [::zlib deflate $utfdata]
  if {[string length $cdata] < $size} {
    set method 8
    set utfdata $cdata
  }
  set csize [string length $utfdata]
  puts -nonewline $zipchan $utfdata

  # update the header
  set local [binary format a4sssiiii PK\03\04 \
                 $version $flags $method $mtime $crc $csize $size]
  set current [tell $zipchan]
  seek $zipchan $offset
  puts -nonewline $zipchan $local
  seek $zipchan $current
  
  set hdr [binary format a4ssssiiiisssssii PK\01\02 0x0317 \
               $version $flags $method $mtime $crc $csize $size \
               [string length $utfpath] [string length $extra]\
               [string length $utfcomment] 0 $attr $attrex $offset]
  append hdr $utfpath $extra $utfcomment
  return $hdr
}

proc ::ooxml::Default { name value } {
  variable defaults

  switch -- $name {
    path {
      set defaults($name) [string trim $value]
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
      }
      return [set ole [expr {[clock scan ${Y}${m}${d}T${H}${M}${S} -gmt 1] / 86400.0 + 25569}]]
    }
  }
  return {}
}


proc ::ooxml::ZipInitialize { *v file } {
  upvar ${*v} v

  set fd [open $file w]
  set v(fd) $fd
  set v(base) [tell $fd]
  set v(toc) {}
  fconfigure $fd -translation binary -encoding binary
}


proc ::ooxml::ZipEmit { *v s } {
  upvar ${*v} v

  puts -nonewline $v(fd) $s
}


proc ::ooxml::ZipDosTime { sec } {
  set f [clock format $sec -format {%Y %m %d %H %M %S} -gmt 1]
  regsub -all { 0(\d)} $f { \1} f
  foreach {Y M D h m s} $f break
  set date [expr {(($Y-1980)<<9) | ($M<<5) | $D}]
  set time [expr {($h<<11) | ($m<<5) | ($s>>1)}]
  return [list $date $time]
}


proc ::ooxml::ZipAddEntry { *v name contents {date {}} {force 0} } {
  upvar ${*v} v

  if {$date eq {}} {
    set date [clock seconds]
  }
  lassign [ZipDosTime $date] date time
  set flag 0
  set type 0 ;# stored
  set fsize [string length $contents]
  set csize $fsize
  set fnlen [string length $name]
  
  if {$force > 0 && $force != [string length $contents]} {
    set csize $fsize
    set fsize $force
    set type 8 ;# if we're passing in compressed data, it's deflated
  }
  
  if {[catch {zlib crc32 $contents} crc]} {
    set crc 0
  } elseif {$type == 0} {
    set cdata [zlib deflate $contents 9]
    if {[string length $cdata] < [string length $contents]} {
      set contents $cdata
      set csize [string length $cdata]
      set type 8 ;# deflate
    }
  }
  
  lappend v(toc) "[binary format a2c6ssssiiiss4ii PK {1 2 20 0 20 0} $flag $type $time $date $crc $csize $fsize $fnlen {0 0 0 0} 128 [tell $v(fd)]]$name"
  
  ZipEmit v [binary format a2c4ssssiiiss PK {3 4 20 0} $flag $type $time $date $crc $csize $fsize $fnlen 0]
  ZipEmit v $name
  ZipEmit v $contents
}


proc ::ooxml::ZipAddDirectory { *v name {date {}} {force 0} } {
  upvar ${*v} v

  set name "${name}/"
  if {$date eq {}} {
    set date [clock seconds]
  }
  lassign [ZipDosTime $date] date time
  set flag 0
  set type 0 ;# stored
  set fsize 0
  set csize 0
  set fnlen [string length $name]
  set crc 0
  
  lappend v(toc) "[binary format a2c6ssssiiiss4ii PK {1 2 20 0 20 0} $flag $type $time $date $crc $csize $fsize $fnlen {0 0 0 0} 128 [tell $v(fd)]]$name"
  
  ZipEmit v [binary format a2c4ssssiiiss PK {3 4 20 0} $flag $type $time $date $crc $csize $fsize $fnlen 0]
  ZipEmit v $name
}


proc ::ooxml::ZipFinalize { *v } {
  upvar ${*v} v

  set pos [tell $v(fd)]
  set ntoc [llength $v(toc)]
  foreach x $v(toc) {
    ZipEmit v $x
  }
  set v(toc) {}
  
  set len [expr {[tell $v(fd)] - $pos}]
  incr pos -$v(base)
  
  ZipEmit v [binary format a2c2ssssiis PK {5 6} 0 0 $ntoc $ntoc $len $pos 0]
  
  close $v(fd)
}


proc ::ooxml::Zip { zipfile directory files } {
  array set v { fd {} base {} toc {} }

  # this code is a rewrite and extension of the zipper code found
  # at http://equi4.com/critlib/ and http://wiki.tcl.tk/36689
  # by Tom Krehbiel 2012 krehbiel.tom at gmail dot com


  ZipInitialize v $zipfile
  foreach file $files {
    regsub {^\./} $file {} to
    set from [file join [file normalize $directory] $to]
    if {[file isfile $from]} {
      set fd [open $from r]
      fconfigure $fd -translation binary -encoding binary
      ZipAddEntry v $to [read $fd] [file mtime $from]
      close $fd
    } elseif {[file isdir $from]} {
      ZipAddDirectory v $to [file mtime $from]
      lappend dirs $file
    }
  }
  ZipFinalize v
}


proc ::ooxml::Column { col } {
  set name {}
  while {$col >= 0} {
    set char [binary format c [expr {($col % 26) + 65}]]
    set name $char$name
    set col [expr {$col / 26 -1}]
  }







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







745
746
747
748
749
750
751






































































































































752
753
754
755
756
757
758
      }
      return [set ole [expr {[clock scan ${Y}${m}${d}T${H}${M}${S} -gmt 1] / 86400.0 + 25569}]]
    }
  }
  return {}
}







































































































































proc ::ooxml::Column { col } {
  set name {}
  while {$col >= 0} {
    set char [binary format c [expr {($col % 26) + 65}]]
    set name $char$name
    set col [expr {$col / 26 -1}]
  }
896
897
898
899
900
901
902


903
904
905
906
907
908
909


#
# ooxml::xl_sheets
#

proc ::ooxml::xl_sheets { file } {


  set sheets {}

  set mnt [vfs::zip::Mount $file xlsx]

  set rels 0
  if {![catch {open xlsx/xl/_rels/workbook.xml.rels r} fd]} {
    fconfigure $fd -encoding utf-8







>
>







842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857


#
# ooxml::xl_sheets
#

proc ::ooxml::xl_sheets { file } {
  package require vfs::zip

  set sheets {}

  set mnt [vfs::zip::Mount $file xlsx]

  set rels 0
  if {![catch {open xlsx/xl/_rels/workbook.xml.rels r} fd]} {
    fconfigure $fd -encoding utf-8
951
952
953
954
955
956
957


958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
#
# ooxml::xl_read
#

proc ::ooxml::xl_read { file args } {
  variable predefNumFmts



  array set cellXfs {}
  array set numFmts [array get predefNumFmts]
  array set sharedStrings {}
  set sheets {}

  if {[::ooxml::Getopt opts {valuesonly keylist sheets.arg {} sheetnames.arg {} datefmt.arg {%Y-%m-%d %H:%M:%S} as.arg array} $args]} {
    error $opts(-errmsg)
  }
  if {[string trim $opts(sheets)] eq {} && [string trim $opts(sheetnames)] eq {}} {
    set opts(sheetnames) *
  }


  set mnt [vfs::zip::Mount $file xlsx]

  set rels 0
  if {![catch {open xlsx/xl/_rels/workbook.xml.rels r} fd]} {
    fconfigure $fd -encoding utf-8
    if {![catch {dom parse [read $fd]} rdoc]} {







>
>











<







899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918

919
920
921
922
923
924
925
#
# ooxml::xl_read
#

proc ::ooxml::xl_read { file args } {
  variable predefNumFmts

  package require vfs::zip

  array set cellXfs {}
  array set numFmts [array get predefNumFmts]
  array set sharedStrings {}
  set sheets {}

  if {[::ooxml::Getopt opts {valuesonly keylist sheets.arg {} sheetnames.arg {} datefmt.arg {%Y-%m-%d %H:%M:%S} as.arg array} $args]} {
    error $opts(-errmsg)
  }
  if {[string trim $opts(sheets)] eq {} && [string trim $opts(sheetnames)] eq {}} {
    set opts(sheetnames) *
  }


  set mnt [vfs::zip::Mount $file xlsx]

  set rels 0
  if {![catch {open xlsx/xl/_rels/workbook.xml.rels r} fd]} {
    fconfigure $fd -encoding utf-8
    if {![catch {dom parse [read $fd]} rdoc]} {
1444
1445
1446
1447
1448
1449
1450





































































1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477



1478


1479


















1480
1481
1482
1483
1484
1485
1486
    if {$column > $wb($sheet,max_column)} {
      set wb($sheet,max_column) $column
    }
  }
  return [array get wb]
}







































































#
# ooxml::xl_write
#


oo::class create ooxml::xl_write {
  constructor { args } {
    my variable obj
    my variable cells
    my variable sharedStrings
    my variable fonts
    my variable numFmts
    my variable styles
    my variable fills
    my variable borders
    my variable cols

    if {[::ooxml::Getopt opts {creator.arg {unknown}} $args]} {
      error $opts(-errmsg)
    }

    set obj(blockPreset) 0

    set obj(encoding) utf-8
    set obj(indent) none




    set obj(creator) $opts(creator)


    set obj(created) [clock format [clock seconds] -format %Y-%m-%dT%H:%M:%SZ -gmt 1]



















    set obj(sheets) 0
    array set sheets {}

    set obj(sharedStrings) 0
    set sharedStrings {}








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


















|








>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
    if {$column > $wb($sheet,max_column)} {
      set wb($sheet,max_column) $column
    }
  }
  return [array get wb]
}

# Internal helper
proc ooxml::Dom2zip {zf node path cd count} {
  upvar $cd mycd
  upvar $count mycount
  append mycd [::ooxml::add_str_to_archive $zf $path \
                   [$node asXML -indent none -xmlDeclaration 1 \
                        -encString "UTF-8"]]
  incr mycount
}


#
# ooxml::InitNodeCommands
#


proc ooxml::InitNodeCommands {} {
  variable initNodeCmds

  if {[info exists initNodeCmds] && $initNodeCmds} return

  set elementNodes {
    AppVersion Application
    Company
    Default DocSecurity
    HeadingPairs HyperlinksChanged
    LinksUpToDate
    Override
    Relationship
    ScaleCrop SharedDoc
    TitlesOfParts
    a:accent1 a:accent2 a:accent3 a:accent4 a:accent5 a:accent6 a:alpha a:bevelT a:bgFillStyleLst a:bodyPr a:camera
    a:clrScheme a:cs a:dk1 a:dk2 a:ea a:effectLst a:effectRef a:effectStyle a:effectStyleLst a:extraClrSchemeLst
    a:fillRef a:fillStyleLst a:fillToRect a:fmtScheme a:folHlink a:font a:fontRef a:fontScheme a:gradFill a:gs
    a:gsLst a:hlink a:latin a:lightRig a:lin a:ln a:lnDef a:lnRef a:lnStyleLst a:lstStyle a:lt1 a:lt2 a:majorFont
    a:minorFont a:objectDefaults a:outerShdw a:path a:prstDash a:rot a:satMod a:scene3d a:schemeClr a:shade
    a:solidFill a:sp3d a:spDef a:spPr a:srgbClr a:style a:sysClr a:themeElements a:tint
    alignment autoFilter
    b bgColor bookViews border borders bottom
    c calcPr cellStyle cellStyleXfs cellStyles cellXfs col color cols
    cp:lastModifiedBy
    dc:creator
    dcterms:created dcterms:modified
    definedName definedNames diagonal dimension dxfs
    f family fgColor fileVersion fill fills font fonts
    i
    left
    mergeCell mergeCells
    name numFmt numFmts
    pageMargins pane patternFill
    right row
    scheme sheet sheetData sheetFormatPr sheetView sheetViews sheets si sz
    t tableStyles top
    u
    v
    vt:i4 vt:lpstr vt:lpstrvt:lpstr vt:variant vt:vector
    workbookPr workbookView
    xf
  }

  namespace eval ::ooxml "dom createNodeCmd textNode Text; namespace export Text"

  foreach tag $elementNodes {
    namespace eval ::ooxml "dom createNodeCmd -tagName $tag elementNode Tag_$tag; namespace export Tag_$tag"
  }
  
  set initNodeCmds 1
}


#
# ooxml::xl_write
#


oo::class create ooxml::xl_write {
  constructor { args } {
    my variable obj
    my variable cells
    my variable sharedStrings
    my variable fonts
    my variable numFmts
    my variable styles
    my variable fills
    my variable borders
    my variable cols

    if {[::ooxml::Getopt opts {creator.arg {unknown} created.arg {} modifiedby.arg {} modified.arg {} application.arg {}} $args]} {
      error $opts(-errmsg)
    }

    set obj(blockPreset) 0

    set obj(encoding) utf-8
    set obj(indent) none

    if {[string trim $opts(creator)] eq {}} {
      set obj(creator) {unknown}
    } else {
      set obj(creator) $opts(creator)
    }
    if {[string trim $opts(created)] eq {} || [catch {clock scan $opts(created)}]} {
      set obj(created) [clock format [clock seconds] -format %Y-%m-%dT%H:%M:%SZ -gmt 1]
    } else {
      set obj(created) [clock format [clock scan $opts(created) -gmt 1] -format %Y-%m-%dT%H:%M:%SZ -gmt 1]
    }
    if {[string trim $opts(modifiedby)] eq {}} {
      set obj(lastModifiedBy) $opts(creator)
    } else {
      set obj(lastModifiedBy) $opts(modifiedby)
    }
    if {[string trim $opts(modified)] eq {} || [catch {clock scan $opts(modified)}]} {
      set obj(modified) [clock format [clock seconds] -format %Y-%m-%dT%H:%M:%SZ -gmt 1]
    } else {
      set obj(modified) [clock format [clock scan $opts(modified) -gmt 1] -format %Y-%m-%dT%H:%M:%SZ -gmt 1]
    }
    if {[string trim $opts(application)] eq {}} {
      set obj(application) {Tcl - Office Open XML - Spreadsheet}
    } else {
      set obj(application) $opts(application)
    }

    set obj(sheets) 0
    array set sheets {}

    set obj(sharedStrings) 0
    set sharedStrings {}

1971
1972
1973
1974
1975
1976
1977

































































1978
1979
1980

1981
1982
1983
1984
1985
1986
1987
  }

  method cell { sheet {data {}} args } {
    my variable obj
    my variable cells
    my variable cols


































































    if {[::ooxml::Getopt opts {index.arg {} style.arg 0 formula.arg {} string nozero globalstyle height.arg {}} $args]} {
      error $opts(-errmsg)
    }


    if {!$obj(callRow,$obj(sheets))} {
      set obj(callRow,$obj(sheets)) 1
      incr obj(row,$sheet)
    }

    if {[regexp {^\d+$} $opts(index)] && $opts(index) > -1} {







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
>







2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085

2086
2087
2088
2089
2090
2091
2092
2093
  }

  method cell { sheet {data {}} args } {
    my variable obj
    my variable cells
    my variable cols

    array set opts {
      index ""
      style 0
      formula ""
      string 0
      nozero 0
      globalstyle 0
      height ""
    }
    set len [llength $args]
    set loopInd 0
    while {$loopInd < $len} {
      switch -- [lindex $args $loopInd] {
        "-index" {
          incr loopInd
          if {$loopInd < $len} {
            set opts(index) [lindex $args $loopInd]
            incr loopInd
          } else {
            error "-index: missing argument"
          }            
        }
        "-style" {
          incr loopInd
          if {$loopInd < $len} {
            set opts(style) [lindex $args $loopInd]
            incr loopInd
          } else {
            error "-style: missing argument"
          }
        }
        "-formula"  {
          incr loopInd
          if {$loopInd < $len} {
            set opts(formula) [lindex $args $loopInd]
            incr loopInd
          } else {
            error "-formula: missing argument"
          }
        }
        "-string" {
          set opts(string) 1
          incr loopInd
        }
        "-nozero" {
          set opts(nozero) 1
        }
        "-globalstyle" {
          set opts(globalstyle) 1
          incr loopInd
        }
        "-height" {
          incr loopInd
          if {$loopInd < $len} {
            set opts(height) [lindex $args $loopInd]
            incr loopInd
          } else {
            error "-height: missing argument"
          }
        }
        default {
          error "unknown option [lindex $args $loopInd]"
        }
      }
    }
    # if {[::ooxml::Getopt opts {index.arg {} style.arg 0 formula.arg {} string nozero globalstyle height.arg {}} $args]} {
    #   error $opts(-errmsg)

    # }

    if {!$obj(callRow,$obj(sheets))} {
      set obj(callRow,$obj(sheets)) 1
      incr obj(row,$sheet)
    }

    if {[regexp {^\d+$} $opts(index)] && $opts(index) > -1} {
2223
2224
2225
2226
2227
2228
2229





















2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262

2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
    my variable borders
    my variable cols

    if {[::ooxml::Getopt opts {holdcontainerdirectory} $args]} {
      error $opts(-errmsg)
    }






















    foreach {n v} [array get cells] {
      if {[dict exists $v t] && [dict get $v t] eq {s} && [dict exists $v v] && [dict get $v v] ne {}} {
        set thisv [dict get $v v]
        if {[info exists lookup($thisv)]} {
          set pos $lookup($thisv)
        } else {
          set pos [llength $sharedStrings]
	  lappend sharedStrings $thisv
          set lookup($thisv) $pos
	}
        set obj(sharedStrings) 1
	dict set cells($n) v $pos
      }
    }
    unset -nocomplain n v
    array unset lookup
    
    # _rels/.rels
    set doc [set obj(doc,_rels/.rels) [dom createDocument Relationships]]
    set root [$doc documentElement]

    set rId 0

    dom createNodeCmd -tagName Relationship elementNode Tag_Relationship

    $root setAttribute xmlns http://schemas.openxmlformats.org/package/2006/relationships

    $root appendFromScript {
      Tag_Relationship Id rId1 Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument Target xl/workbook.xml {}
      Tag_Relationship Id rId2 Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties Target docProps/app.xml {}
      Tag_Relationship Id rId3 Type http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties Target docProps/core.xml {}
    }



    # [Content_Types].xml
    set doc [set obj(doc,\[Content_Types\].xml) [dom createDocument Types]]
    set root [$doc documentElement]

    foreach tag {Default Override} {
      dom createNodeCmd -tagName $tag elementNode Tag_$tag
    }

    $root setAttribute xmlns http://schemas.openxmlformats.org/package/2006/content-types

    $root appendFromScript {
      Tag_Default Extension xml ContentType application/xml {}
      Tag_Default Extension rels ContentType application/vnd.openxmlformats-package.relationships+xml {}
      Tag_Override PartName /xl/workbook.xml ContentType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml {}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


















|




<
<







|
>


|


<
<
<







2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379


2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393



2394
2395
2396
2397
2398
2399
2400
    my variable borders
    my variable cols

    if {[::ooxml::Getopt opts {holdcontainerdirectory} $args]} {
      error $opts(-errmsg)
    }

    ooxml::InitNodeCommands
    namespace import ::ooxml::Tag_* ::ooxml::Text

    # Initialize zip file
    set file [string trim $file]
    if {$file eq {}} {
      set file {spreadsheetml.xlsx}
    }
    if {[file extension $file] ne {.xlsx}} {
      append file {.xlsx}
    }
    if {[catch {set zf [open $file w]}]} {
      error "Unable to write $file"
    }
    fconfigure $zf \
        -encoding    binary \
        -translation binary \
        -eofchar     {}
    set count 0
    set cd ""
    
    foreach {n v} [array get cells] {
      if {[dict exists $v t] && [dict get $v t] eq {s} && [dict exists $v v] && [dict get $v v] ne {}} {
        set thisv [dict get $v v]
        if {[info exists lookup($thisv)]} {
          set pos $lookup($thisv)
        } else {
          set pos [llength $sharedStrings]
	  lappend sharedStrings $thisv
          set lookup($thisv) $pos
	}
        set obj(sharedStrings) 1
	dict set cells($n) v $pos
      }
    }
    unset -nocomplain n v
    array unset lookup
    
    # _rels/.rels
    set doc [dom createDocument Relationships]
    set root [$doc documentElement]

    set rId 0



    $root setAttribute xmlns http://schemas.openxmlformats.org/package/2006/relationships

    $root appendFromScript {
      Tag_Relationship Id rId1 Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument Target xl/workbook.xml {}
      Tag_Relationship Id rId2 Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties Target docProps/app.xml {}
      Tag_Relationship Id rId3 Type http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties Target docProps/core.xml {}
    }
    ::ooxml::Dom2zip $zf $root "_rels/.rels" cd count
    $doc delete

    # [Content_Types].xml
    set doc [dom createDocument Types]
    set root [$doc documentElement]





    $root setAttribute xmlns http://schemas.openxmlformats.org/package/2006/content-types

    $root appendFromScript {
      Tag_Default Extension xml ContentType application/xml {}
      Tag_Default Extension rels ContentType application/vnd.openxmlformats-package.relationships+xml {}
      Tag_Override PartName /xl/workbook.xml ContentType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml {}
2285
2286
2287
2288
2289
2290
2291
2292

2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
      }
      if {$obj(calcChain)} {
	Tag_Override PartName /xl/calcChain.xml ContentType application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml {}
      }
      Tag_Override PartName /docProps/core.xml ContentType application/vnd.openxmlformats-package.core-properties+xml {}
      Tag_Override PartName /docProps/app.xml ContentType application/vnd.openxmlformats-officedocument.extended-properties+xml {}
    }



    # docProps/app.xml
    set doc [set obj(doc,docProps/app.xml) [dom createDocument Properties]]
    set root [$doc documentElement]

    dom createNodeCmd textNode Text
    foreach tag {AppVersion Application Company DocSecurity HeadingPairs HyperlinksChanged LinksUpToDate ScaleCrop SharedDoc TitlesOfParts
                 vt:i4 vt:lpstrvt:lpstr vt:lpstr vt:variant vt:vector} {
      dom createNodeCmd -tagName $tag elementNode Tag_$tag
    }

    $root setAttribute xmlns http://schemas.openxmlformats.org/officeDocument/2006/extended-properties
    $root setAttribute xmlns:vt http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes

    $root appendFromScript {
      Tag_Application { Text {Tcl - Office Open XML - Spreadsheet} }
      Tag_DocSecurity { Text 0 }
      Tag_ScaleCrop { Text false }
      Tag_HeadingPairs {
	Tag_vt:vector size 2 baseType variant {
	  Tag_vt:variant {
	    Tag_vt:lpstr { Text [msgcat::mc Worksheets] }
	  }







|
>


|


<
<
<
<
<
<




|







2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421






2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
      }
      if {$obj(calcChain)} {
	Tag_Override PartName /xl/calcChain.xml ContentType application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml {}
      }
      Tag_Override PartName /docProps/core.xml ContentType application/vnd.openxmlformats-package.core-properties+xml {}
      Tag_Override PartName /docProps/app.xml ContentType application/vnd.openxmlformats-officedocument.extended-properties+xml {}
    }
    ::ooxml::Dom2zip $zf $root "\[Content_Types\].xml" cd count
    $doc delete

    # docProps/app.xml
    set doc [set obj(doc,) [dom createDocument Properties]]
    set root [$doc documentElement]







    $root setAttribute xmlns http://schemas.openxmlformats.org/officeDocument/2006/extended-properties
    $root setAttribute xmlns:vt http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes

    $root appendFromScript {
      Tag_Application { Text $obj(application) }
      Tag_DocSecurity { Text 0 }
      Tag_ScaleCrop { Text false }
      Tag_HeadingPairs {
	Tag_vt:vector size 2 baseType variant {
	  Tag_vt:variant {
	    Tag_vt:lpstr { Text [msgcat::mc Worksheets] }
	  }
2329
2330
2331
2332
2333
2334
2335
2336

2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359

2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382


2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404


2405


2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421


2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
      }
      Tag_Company {}
      Tag_LinksUpToDate { Text false }
      Tag_SharedDoc { Text false }
      Tag_HyperlinksChanged { Text false }
      Tag_AppVersion { Text 1.0 }
    }



    # docProps/core.xml
    set doc [set obj(doc,docProps/core.xml) [dom createDocument cp:coreProperties]]
    set root [$doc documentElement]

    dom createNodeCmd textNode Text
    foreach tag {cp:lastModifiedBy dc:creator dcterms:created dcterms:modified} {
      dom createNodeCmd -tagName $tag elementNode Tag_$tag
    }

    $root setAttribute xmlns:cp http://schemas.openxmlformats.org/package/2006/metadata/core-properties
    $root setAttribute xmlns:dc http://purl.org/dc/elements/1.1/
    $root setAttribute xmlns:dcterms http://purl.org/dc/terms/
    $root setAttribute xmlns:dcmitype http://purl.org/dc/dcmitype/
    $root setAttribute xmlns:xsi http://www.w3.org/2001/XMLSchema-instance

    $root appendFromScript {
      Tag_dc:creator { Text $obj(creator) }
      Tag_cp:lastModifiedBy { Text $obj(creator) }
      Tag_dcterms:created xsi:type dcterms:W3CDTF { Text $obj(created) }
      Tag_dcterms:modified xsi:type dcterms:W3CDTF { Text $obj(created) }
    }



    # xl/_rels/workbook.xml.rels
    set doc [set obj(doc,xl/_rels/workbook.xml.rels) [dom createDocument Relationships]]
    set root [$doc documentElement]

    dom createNodeCmd -tagName Relationship elementNode Tag_Relationship

    $root setAttribute xmlns http://schemas.openxmlformats.org/package/2006/relationships

    $root appendFromScript {
      for {set ws 1} {$ws <= $obj(sheets)} {incr ws} {
	Tag_Relationship Id rId$ws Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet Target worksheets/sheet${ws}.xml {}
      }
      set rId [incr ws -1]
      Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme Target theme/theme1.xml {}
      Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles Target styles.xml {}
      if {$obj(sharedStrings) > 0} {
	Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings Target sharedStrings.xml {}
      }
      if {$obj(calcChain)} {
	Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain Target calcChain.xml {}
      }
    }




    # xl/sharedStrings.xml
    if {$obj(sharedStrings) > 0} {
      set doc [set obj(doc,xl/sharedStrings.xml) [dom createDocument sst]]
      set root [$doc documentElement]

      dom createNodeCmd textNode Text
      foreach tag {si t} {
	dom createNodeCmd -tagName $tag elementNode Tag_$tag
      }

      $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
      $root setAttribute count [llength $sharedStrings]
      $root setAttribute uniqueCount [llength $sharedStrings]

      $root appendFromScript {
	foreach string $sharedStrings {
	  Tag_si {
	    Tag_t { Text $string }
	  }
	}


      }


    }


    # xl/calcChain.xml
    if {$obj(calcChain)} {
      set doc [set obj(doc,xl/calcChain.xml) [dom createDocument calcChain]]
      set root [$doc documentElement]

      dom createNodeCmd -tagName c elementNode Tag_c

      $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main

      $root appendFromScript {
	Tag_c r C1 i 3 l 1 {}
	Tag_c r A3 i 2 {}
      }


    }


    # xl/styles.xml
    set doc [set obj(doc,xl/styles.xml) [dom createDocument styleSheet]]
    set root [$doc documentElement]

    foreach tag {alignment b bgColor border borders bottom cellStyle cellStyleXfs cellStyles cellXfs color diagonal dxfs family
                 fgColor fill fills font fonts i left name numFmt numFmts patternFill right scheme sz tableStyles top u xf} {
      dom createNodeCmd -tagName $tag elementNode Tag_$tag
    }

    $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
    $root setAttribute xmlns:mc http://schemas.openxmlformats.org/markup-compatibility/2006
    $root setAttribute xmlns:x14ac http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac
    $root setAttribute mc:Ignorable x14ac

    $root appendFromScript {
      if {$obj(numFmts) > $::ooxml::defaults(numFmts,start)} {







|
>


|


<
<
<
<
<








|

|

|
>


|


<
<
















>
>




|


<
<
<
<
<










>
>

>
>





|


<
<






>
>




|


<
<
<
<
<







2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460





2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479


2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504





2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527


2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542





2543
2544
2545
2546
2547
2548
2549
      }
      Tag_Company {}
      Tag_LinksUpToDate { Text false }
      Tag_SharedDoc { Text false }
      Tag_HyperlinksChanged { Text false }
      Tag_AppVersion { Text 1.0 }
    }
    ::ooxml::Dom2zip $zf $root "docProps/app.xml" cd count
    $doc delete

    # docProps/core.xml
    set doc [dom createDocument cp:coreProperties]
    set root [$doc documentElement]






    $root setAttribute xmlns:cp http://schemas.openxmlformats.org/package/2006/metadata/core-properties
    $root setAttribute xmlns:dc http://purl.org/dc/elements/1.1/
    $root setAttribute xmlns:dcterms http://purl.org/dc/terms/
    $root setAttribute xmlns:dcmitype http://purl.org/dc/dcmitype/
    $root setAttribute xmlns:xsi http://www.w3.org/2001/XMLSchema-instance

    $root appendFromScript {
      Tag_dc:creator { Text $obj(creator) }
      Tag_cp:lastModifiedBy { Text $obj(lastModifiedBy) }
      Tag_dcterms:created xsi:type dcterms:W3CDTF { Text $obj(created) }
      Tag_dcterms:modified xsi:type dcterms:W3CDTF { Text $obj(modified) }
    }
    ::ooxml::Dom2zip $zf $root "docProps/core.xml" cd count
    $doc delete

    # xl/_rels/workbook.xml.rels
    set doc [dom createDocument Relationships]
    set root [$doc documentElement]



    $root setAttribute xmlns http://schemas.openxmlformats.org/package/2006/relationships

    $root appendFromScript {
      for {set ws 1} {$ws <= $obj(sheets)} {incr ws} {
	Tag_Relationship Id rId$ws Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet Target worksheets/sheet${ws}.xml {}
      }
      set rId [incr ws -1]
      Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme Target theme/theme1.xml {}
      Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles Target styles.xml {}
      if {$obj(sharedStrings) > 0} {
	Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings Target sharedStrings.xml {}
      }
      if {$obj(calcChain)} {
	Tag_Relationship Id rId[incr rId] Type http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain Target calcChain.xml {}
      }
    }
    ::ooxml::Dom2zip $zf $root "xl/_rels/workbook.xml.rels" cd count
    $doc delete


    # xl/sharedStrings.xml
    if {$obj(sharedStrings) > 0} {
      set doc [dom createDocument sst]
      set root [$doc documentElement]






      $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
      $root setAttribute count [llength $sharedStrings]
      $root setAttribute uniqueCount [llength $sharedStrings]

      $root appendFromScript {
	foreach string $sharedStrings {
	  Tag_si {
	    Tag_t { Text $string }
	  }
	}
	# garbage collection
	set sharedStrings {}
      }
      ::ooxml::Dom2zip $zf $root "xl/sharedStrings.xml" cd count
      $doc delete
    }


    # xl/calcChain.xml
    if {$obj(calcChain)} {
      set doc [dom createDocument calcChain]
      set root [$doc documentElement]



      $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main

      $root appendFromScript {
	Tag_c r C1 i 3 l 1 {}
	Tag_c r A3 i 2 {}
      }
      ::ooxml::Dom2zip $zf $root "xl/calcChain.xml" cd count
      $doc delete
    }


    # xl/styles.xml
    set doc [dom createDocument styleSheet]
    set root [$doc documentElement]






    $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
    $root setAttribute xmlns:mc http://schemas.openxmlformats.org/markup-compatibility/2006
    $root setAttribute xmlns:x14ac http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac
    $root setAttribute mc:Ignorable x14ac

    $root appendFromScript {
      if {$obj(numFmts) > $::ooxml::defaults(numFmts,start)} {
2557
2558
2559
2560
2561
2562
2563


2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
      }
      Tag_cellStyles count 1 {
	Tag_cellStyle name Standard xfId 0 builtinId 0 {}
      }
      Tag_dxfs count 0 {}
      Tag_tableStyles count 0 {}
    }




    # xl/theme/theme1.xml
    set doc [set obj(doc,xl/theme/theme1.xml) [dom createDocument a:theme]]
    set root [$doc documentElement]

    foreach tag {a:accent1 a:accent2 a:accent3 a:accent4 a:accent5 a:accent6 a:alpha a:bevelT a:bgFillStyleLst a:bodyPr a:camera
		 a:clrScheme a:cs a:dk1 a:dk2 a:ea a:effectLst a:effectRef a:effectStyle a:effectStyleLst a:extraClrSchemeLst
		 a:fillRef a:fillStyleLst a:fillToRect a:fmtScheme a:folHlink a:font a:fontRef a:fontScheme a:gradFill a:gs a:gsLst
		 a:hlink a:latin a:lightRig a:lin a:ln a:lnDef a:lnRef a:lnStyleLst a:lstStyle a:lt1 a:lt2 a:majorFont a:minorFont
		 a:objectDefaults a:outerShdw a:path a:prstDash a:rot a:satMod a:scene3d a:schemeClr a:shade a:solidFill a:sp3d
		 a:spDef a:spPr a:srgbClr a:style a:sysClr a:themeElements a:tint} {
      dom createNodeCmd -tagName $tag elementNode Tag_$tag
    }

    $root setAttribute xmlns:a http://schemas.openxmlformats.org/drawingml/2006/main
    $root setAttribute name Office-Design

    $root appendFromScript {
      Tag_a:themeElements {
	Tag_a:clrScheme name Office {
	  Tag_a:dk1 {







>
>



|


<
<
<
<
<
<
<
<
<







2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680









2681
2682
2683
2684
2685
2686
2687
      }
      Tag_cellStyles count 1 {
	Tag_cellStyle name Standard xfId 0 builtinId 0 {}
      }
      Tag_dxfs count 0 {}
      Tag_tableStyles count 0 {}
    }
    ::ooxml::Dom2zip $zf $root "xl/styles.xml" cd count
    $doc delete


    # xl/theme/theme1.xml
    set doc [dom createDocument a:theme]
    set root [$doc documentElement]










    $root setAttribute xmlns:a http://schemas.openxmlformats.org/drawingml/2006/main
    $root setAttribute name Office-Design

    $root appendFromScript {
      Tag_a:themeElements {
	Tag_a:clrScheme name Office {
	  Tag_a:dk1 {
2896
2897
2898
2899
2900
2901
2902


2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
	      Tag_a:schemeClr val tx1 {}
	    }
	  }
	}
      }
      Tag_a:extraClrSchemeLst {}
    }




    # xl/workbook.xml
    set doc [set obj(doc,xl/workbook.xml) [dom createDocument workbook]]
    set root [$doc documentElement]

    dom createNodeCmd textNode Text
    foreach tag {bookViews calcPr definedName definedNames fileVersion sheet sheets workbookPr workbookView} {
      dom createNodeCmd -tagName $tag elementNode Tag_$tag
    }

    $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
    $root setAttribute xmlns:r http://schemas.openxmlformats.org/officeDocument/2006/relationships

    $root appendFromScript {
      Tag_fileVersion appName xl lastEdited 5 lowestEdited 5 rupBuild 5000 {}
      Tag_workbookPr showInkAnnotation 0 autoCompressPictures 0 {}
      Tag_bookViews {







>
>



|


<
<
<
<
<







2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012





3013
3014
3015
3016
3017
3018
3019
	      Tag_a:schemeClr val tx1 {}
	    }
	  }
	}
      }
      Tag_a:extraClrSchemeLst {}
    }
    ::ooxml::Dom2zip $zf $root "xl/theme/theme1.xml" cd count
    $doc delete


    # xl/workbook.xml
    set doc [dom createDocument workbook]
    set root [$doc documentElement]






    $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
    $root setAttribute xmlns:r http://schemas.openxmlformats.org/officeDocument/2006/relationships

    $root appendFromScript {
      Tag_fileVersion appName xl lastEdited 5 lowestEdited 5 rupBuild 5000 {}
      Tag_workbookPr showInkAnnotation 0 autoCompressPictures 0 {}
      Tag_bookViews {
2929
2930
2931
2932
2933
2934
2935


2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
	Tag_definedNames {
	  Tag_definedName name _xlnm._FilterDatabase localSheetId 0 hidden 1 { Text Blatt1!$A$1:$C$1 }
	}
      }
      Tag_calcPr calcId 140000 concurrentCalc 0 {}
      # fullCalcOnLoad 1
    }




    # xl/worksheets/sheet1.xml SHEET
    dom createNodeCmd textNode Text
    foreach tag {autoFilter c col cols dimension f mergeCell mergeCells pageMargins pane row sheetData sheetFormatPr sheetView sheetViews v} {
      dom createNodeCmd -tagName $tag elementNode Tag_$tag
    }

    for {set ws 1} {$ws <= $obj(sheets)} {incr ws} {
      set doc [set obj(doc,xl/worksheets/sheet$ws.xml) [dom createDocument worksheet]]
      set root [$doc documentElement]
      $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
      $root setAttribute xmlns:r http://schemas.openxmlformats.org/officeDocument/2006/relationships
      $root setAttribute xmlns:mc http://schemas.openxmlformats.org/markup-compatibility/2006
      $root setAttribute xmlns:x14ac http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac
      $root setAttribute mc:Ignorable x14ac








>
>



<
<
<
|
<

|







3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039



3040

3041
3042
3043
3044
3045
3046
3047
3048
3049
	Tag_definedNames {
	  Tag_definedName name _xlnm._FilterDatabase localSheetId 0 hidden 1 { Text Blatt1!$A$1:$C$1 }
	}
      }
      Tag_calcPr calcId 140000 concurrentCalc 0 {}
      # fullCalcOnLoad 1
    }
    ::ooxml::Dom2zip $zf $root "xl/workbook.xml" cd count
    $doc delete


    # xl/worksheets/sheet1.xml SHEET





    for {set ws 1} {$ws <= $obj(sheets)} {incr ws} {
      set doc [dom createDocument worksheet]
      set root [$doc documentElement]
      $root setAttribute xmlns http://schemas.openxmlformats.org/spreadsheetml/2006/main
      $root setAttribute xmlns:r http://schemas.openxmlformats.org/officeDocument/2006/relationships
      $root setAttribute xmlns:mc http://schemas.openxmlformats.org/markup-compatibility/2006
      $root setAttribute xmlns:x14ac http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac
      $root setAttribute mc:Ignorable x14ac

2994
2995
2996
2997
2998
2999
3000


3001
3002
3003
3004
3005
3006
3007
		    if {[dict exists $cells($idx) f] && [dict get $cells($idx) f] ne {}} {
		      Tag_f { Text [dict get $cells($idx) f] }
		    }
		  }
		} elseif {[dict exists $cells($idx) s] && [string is integer -strict [dict get $cells($idx) s]] && [dict get $cells($idx) s] > 0} {
		  Tag_c r [::ooxml::RowColumnToString $row,$col] s [dict get $cells($idx) s] {}
		}


	      }
	    }
	  }
	}
	if {$obj(autofilter,$ws) ne {}} {
	  Tag_autoFilter ref $obj(autofilter,$ws) {}
	}







>
>







3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
		    if {[dict exists $cells($idx) f] && [dict get $cells($idx) f] ne {}} {
		      Tag_f { Text [dict get $cells($idx) f] }
		    }
		  }
		} elseif {[dict exists $cells($idx) s] && [string is integer -strict [dict get $cells($idx) s]] && [dict get $cells($idx) s] > 0} {
		  Tag_c r [::ooxml::RowColumnToString $row,$col] s [dict get $cells($idx) s] {}
		}
		# garbage collection
	        unset -nocomplain cells($idx)
	      }
	    }
	  }
	}
	if {$obj(autofilter,$ws) ne {}} {
	  Tag_autoFilter ref $obj(autofilter,$ws) {}
	}
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068

3069
3070
3071

3072
3073
3074


3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
		lappend attr customWidth [dict get $cols($idx) customwidth]
	      }
	      Tag_col {*}$attr {}
	    }
	  }
	}
      }
    }

    # Content-Type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    set file [string trim $file]
    if {$file eq {}} {
      set file {spreadsheetml.xlsx}
    }
    if {[file extension $file] ne {.xlsx}} {
      append file {.xlsx}
    }
    set path [file dirname $file]
    set uid [format xl_%X [clock microseconds]]
    set filesToZip {}
    foreach {tag doc} [array get obj doc,*] {
      lappend filesToZip [set docname [lindex [split $tag ,] 1]]
      set xmlfile [file join $path $uid $docname]
      file mkdir [file dirname $xmlfile]
      if {![catch {open $xmlfile w} fd]} {
	fconfigure $fd -encoding utf-8
	#puts $fd "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
	puts $fd [[$doc documentElement] asXML -indent $obj(indent) -xmlDeclaration 1 -encString [string toupper $obj(encoding)]]
	close $fd
	$doc delete
      }
    }

    set pwd [pwd]
    cd [file join $path $uid]
    if {$path eq {.}} {

      set file [file join .. $file]
    }
    ::ooxml::Zip $file . $filesToZip


    cd $pwd
    if {!$opts(holdcontainerdirectory)} {
      file delete -force [file join $path $uid]
    }
    return 0
  }
}


#
# ooxml::tablelist_to_xl







<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
>
|
<
<
>
|
<
<
>
>
|
<
<
<







3136
3137
3138
3139
3140
3141
3142

3143




















3144
3145
3146
3147
3148


3149
3150


3151
3152
3153



3154
3155
3156
3157
3158
3159
3160
		lappend attr customWidth [dict get $cols($idx) customwidth]
	      }
	      Tag_col {*}$attr {}
	    }
	  }
	}
      }

      ::ooxml::Dom2zip $zf $root "xl/worksheets/sheet$ws.xml" cd count




















      $doc delete
    }

    # Finalize zip.
    set cdoffset [tell $zf]


    set endrec [binary format a4ssssiis PK\05\06 0 0 \
                    $count $count [string length $cd] $cdoffset 0]


    puts -nonewline $zf $cd
    puts -nonewline $zf $endrec
    close $zf



    return 0
  }
}


#
# ooxml::tablelist_to_xl