ooxml

Check-in [3e16defd13]
Login

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

Overview
Comment:Version-1.3
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | release | version-1.3
Files: files | file ages | folders
SHA3-256: 3e16defd13a801555d59f8f70fc9867e0a3b238357f266a58c8378b35d5cdf0e
User & Date: alex 2019-08-20 08:00:06
Context
2019-10-29
06:48
Getopt removed for more speed check-in: 3d420672dd user: alex tags: trunk
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
2019-08-10
09:33
added sample9.tcl and timestamp correction doc core check-in: 4a610b3601 user: alex tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added examples/bench.tcl.





















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
if {[catch {
    package require ooxml
} errMsg]} {
    if {[catch {
        source [file join [file dirname [info script]] ../ooxml.tcl]
    } err2]} {
        puts $err2
        error $errMsg
    }
}

proc doit {rows cols} {
    set spreadsheet [::ooxml::xl_write new -creator "ich"]
    set sheet [$spreadsheet worksheet "Meine Daten"]
    set row 0
    while {$row < $rows} {
        set col 0
        while {$col < $cols} {
            $spreadsheet cell $sheet "Cell $row,$col"
            incr col
        }
        $spreadsheet row $sheet
        incr row
    }
    $spreadsheet write "$rows-$cols.xlsx"
    $spreadsheet destroy
}

foreach {rows cols} {
    10 10
    100 10
    500 10
    1000 10
    3000 10
    5000 10
} {
    set msec [lindex [time {doit $rows $cols}] 0]
    set nrcells [expr {$rows * $cols}]
    set perCell [expr {$msec / $nrcells}]
    set seconds [format "%.3f" [expr {double($msec)/1000000}]]
    puts "$rows/$cols: $nrcells cells, $seconds seconds, $perCell per cell"
}

Changes to examples/sample9.tcl.

7
8
9
10
11
12
13
14
15
16
17
  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 {my text the will be automatically wrapped by excel} -index A1 -style $wrap
  $spreadsheet write export9.xlsx
}
$spreadsheet destroy







|



7
8
9
10
11
12
13
14
15
16
17
  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.

1
2
3
4
5
6
7

8
9
10
11
12
13
14
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

176
177
178
179
180
181
182
...
461
462
463
464
465
466
467












































































468
469
470
471
472
473
474
...
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
813
814
815
816
...
900
901
902
903
904
905
906


907
908
909
910
911
912
913
...
955
956
957
958
959
960
961


962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
....
1447
1448
1449
1450
1451
1452
1453





































































1454
1455
1456
1457
1458
1459
1460
....
1998
1999
2000
2001
2002
2003
2004

































































2005
2006
2007

2008
2009
2010
2011
2012
2013
2014
....
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
2278
2279
2280
2281
2282
2283


2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
....
2307
2308
2309
2310
2311
2312
2313


2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
....
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
....
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
2441
2442
2443


2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
....
2579
2580
2581
2582
2583
2584
2585


2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
....
2918
2919
2920
2921
2922
2923
2924


2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
....
2951
2952
2953
2954
2955
2956
2957


2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
....
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
....
3022
3023
3024
3025
3026
3027
3028


3029
3030
3031
3032
3033
3034
3035
....
3065
3066
3067
3068
3069
3070
3071


3072
3073
3074
3075
3076
3077
3078

3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092

3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
....
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
#
#  ooxml ECMA-376 Office Open XML File Formats
#  https://www.ecma-international.org/publications/standards/Ecma-376.htm
#
#  $Id: ooxml.tcl,v 1.28 2018/06/20 15:01:38 alex Exp $
#
#  Copyright (C) 2018 Alexander Schoepe, Bochum, DE, <schoepe@users.sourceforge.net>

#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without modification,
#  are permitted provided that the following conditions are met:
#
#  1. Redistributions of source code must retain the above copyright notice, this
#     list of conditions and the following disclaimer.
................................................................................
# ::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

................................................................................
  # 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]
................................................................................
      }
      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}]
  }
................................................................................


#
# 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
................................................................................
#
# 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]} {
................................................................................
    }
    if {$column > $wb($sheet,max_column)} {
      set wb($sheet,max_column) $column
    }
  }
  return [array get wb]
}







































































#
# ooxml::xl_write
#


................................................................................
  }

  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} {
................................................................................
    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 {}} {
	if {[set pos [lsearch -exact $sharedStrings [dict get $v v]]] == -1} {





	  lappend sharedStrings [dict get $v v]
	  set pos [lsearch -exact $sharedStrings [dict get $v v]]

	}
	set obj(sharedStrings) 1
	dict set cells($n) v $pos
      }
    }
    unset -nocomplain n v


    # _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 {}
................................................................................
      }
      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 $obj(application) }
      Tag_DocSecurity { Text 0 }
      Tag_ScaleCrop { Text false }
................................................................................
      }
      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(lastModifiedBy) }
      Tag_dcterms:created xsi:type dcterms:W3CDTF { Text $obj(created) }
      Tag_dcterms:modified xsi:type dcterms:W3CDTF { Text $obj(modified) }
    }




    # 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]
................................................................................
      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)} {
................................................................................
      }
      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 {
................................................................................
	      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 {
................................................................................
	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

................................................................................
	  }
	}
	Tag_sheetFormatPr baseColWidth 10 defaultRowHeight 16 x14ac:dyDescent 0.2 {}
	if {[info exists obj($ws,cols)] && $obj($ws,cols) > 0} {
	  Tag_cols {}
	}
	Tag_sheetData {
	  set lastRow -1
	  set rows {}
	  foreach idx [lsort -dictionary [array names cells $ws,*,*]] {
	    lassign [split $idx ,] sheet row col
	    lappend rows $row
	  }
	  foreach row [lsort -unique -integer $rows] {
	    set maxCol $col
	    if {$row != $lastRow} {
	      set lastRow $row
	      set minCol $col
	    }
	    set attr {}
	    if {[dict exists $obj(rowHeight,$ws) $row]} {
	      lappend attr ht [dict get $obj(rowHeight,$ws) $row] customHeight 1
	    }
	    # lappend attr spans [expr {$minCol + 1}]:[expr {$maxCol + 1}]
	    Tag_row r [expr {$row + 1}] {*}$attr {
	      foreach idx [lsort -dictionary [array names cells $ws,$row,*]] {
		lassign [split $idx ,] sheet row col
		if {([dict exists $cells($idx) v] && [string trim [dict get $cells($idx) v]] ne {}) || ([dict exists $cells($idx) f] && [string trim [dict get $cells($idx) f]] ne {})} {
		  set attr {}
		  if {[dict exists $cells($idx) s] && [dict get $cells($idx) s] > 0} {
		    lappend attr s [dict get $cells($idx) s]
		  }
		  if {[dict exists $cells($idx) t] && [dict get $cells($idx) t] ne {n}} {
		    lappend attr t [dict get $cells($idx) t]
................................................................................
		    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) {}
	}
................................................................................
		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
................................................................................
      default {
        $spreadsheet column $sheet -index $column -style $left
      }
    }
  }
}

package provide ooxml 1.2

# Local Variables:
# tcl-indent-level: 2
# End:






|
>







 







|
<








>







 







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







 







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







 







>
>







 







>
>











<







 







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







 







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







 







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


<
>
>
>
>
>
|
<
>

|




>
|

|




<
<







>
>

<

|


<
<
<







 







>
>

<

|


<
<
<
<
<
<







 







>
>

<

|


<
<
<
<
<












>
>

<

|


<
<







 







>
>




|


<
<
<
<
<










>
>

>
>





|


<
<






>
>




|


<
<
<
<
<







 







>
>



|


<
<
<
<
<
<
<
<
<







 







>
>



|


<
<
<
<
<







 







>
>



<
<
<
|
<

|







 







<
|


|

|
<
<
<
<
<






|
|







 







>
>







 







>
>


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







 







|




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
160
161
162
163
164
165
166
167

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
...
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
...
746
747
748
749
750
751
752






































































































































753
754
755
756
757
758
759
...
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
...
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
926
....
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
....
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
2094
....
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
....
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418

2419
2420
2421
2422






2423
2424
2425
2426
2427
2428
2429
....
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
....
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
2550
....
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681









2682
2683
2684
2685
2686
2687
2688
....
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013





3014
3015
3016
3017
3018
3019
3020
....
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040



3041

3042
3043
3044
3045
3046
3047
3048
3049
3050
....
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
3086
....
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
....
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147


3148
3149

3150
3151
3152












3153
3154













3155
3156
3157
3158
3159
3160
3161
....
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
#
#  ooxml ECMA-376 Office Open XML File Formats
#  https://www.ecma-international.org/publications/standards/Ecma-376.htm
#
#  $Id: ooxml.tcl,v 1.28 2018/06/20 15:01:38 alex Exp $
#
#  Copyright (C) 2018-2019 Alexander Schoepe, Bochum, DE, <schoepe@users.sourceforge.net>
#  Copyright (C) 2019 Rolf Ade, DE
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without modification,
#  are permitted provided that the following conditions are met:
#
#  1. Redistributions of source code must retain the above copyright notice, this
#     list of conditions and the following disclaimer.
................................................................................
# ::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

................................................................................
  # 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]
................................................................................
      }
      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}]
  }
................................................................................


#
# 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
................................................................................
#
# 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]} {
................................................................................
    }
    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
#


................................................................................
  }

  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} {
................................................................................
    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 {}
................................................................................
      }
      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_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]
................................................................................
      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)} {
................................................................................
      }
      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 {
................................................................................
	      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 {
................................................................................
	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

................................................................................
	  }
	}
	Tag_sheetFormatPr baseColWidth 10 defaultRowHeight 16 x14ac:dyDescent 0.2 {}
	if {[info exists obj($ws,cols)] && $obj($ws,cols) > 0} {
	  Tag_cols {}
	}
	Tag_sheetData {

	  array unset rows
	  foreach idx [lsort -dictionary [array names cells $ws,*,*]] {
	    lassign [split $idx ,] sheet row col
            lappend rows($row) $col
	  }
	  foreach row [lsort -integer [array names rows]] {





	    set attr {}
	    if {[dict exists $obj(rowHeight,$ws) $row]} {
	      lappend attr ht [dict get $obj(rowHeight,$ws) $row] customHeight 1
	    }
	    # lappend attr spans [expr {$minCol + 1}]:[expr {$maxCol + 1}]
	    Tag_row r [expr {$row + 1}] {*}$attr {
              foreach col $rows($row) {
                set idx "$ws,$row,$col"
		if {([dict exists $cells($idx) v] && [string trim [dict get $cells($idx) v]] ne {}) || ([dict exists $cells($idx) f] && [string trim [dict get $cells($idx) f]] ne {})} {
		  set attr {}
		  if {[dict exists $cells($idx) s] && [dict get $cells($idx) s] > 0} {
		    lappend attr s [dict get $cells($idx) s]
		  }
		  if {[dict exists $cells($idx) t] && [dict get $cells($idx) t] ne {n}} {
		    lappend attr t [dict get $cells($idx) t]
................................................................................
		    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) {}
	}
................................................................................
		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
................................................................................
      default {
        $spreadsheet column $sheet -index $column -style $left
      }
    }
  }
}

package provide ooxml 1.3

# Local Variables:
# tcl-indent-level: 2
# End:

Changes to pkgIndex.tcl.

1
2
if {![package vsatisfies [package provide Tcl] 8.6]} return
package ifneeded ooxml 1.2 [list source [file join $dir ooxml.tcl]]

|
1
2
if {![package vsatisfies [package provide Tcl] 8.6]} return
package ifneeded ooxml 1.3 [list source [file join $dir ooxml.tcl]]