# excel.tcl -- package provide excel 1.0 namespace eval excel:: { variable workbooks 0 variable workbooksArray variable workSheets variable workSheetsArray variable styles variable columnDefault variable data variable rowCounter } proc excel::createWorkbook {} { # # @comment create a workbook pointer # @argument str string to decode # @result undecoded string # incr excel::workbooks set workbookName workbook$excel::workbooks set excel::workbooksArray($workbookName) 1 return $workbookName } proc excel::createWorkSheet {workbook name} { # # @comment decode a string previously encoded using excel::encode # @argument str string to decode # @result undecoded string # variable data if {[info exists excel::workbooksArray($workbook)]} { if {![info exists ::excel::workSheets($workbook)]} { set excel::workSheets($workbook) 1 } else { incr excel::workSheets($workbook) } set workSheetName workSheet[string range ${workbook} 8 end].$excel::workSheets($workbook) set data(workSheet,$::excel::workSheets($workbook),name) $name set data(workSheet,$::excel::workSheets($workbook)) $workSheetName set data(workSheet,$workSheetName) 1 set excel::rowCounter($workSheetName) 0 return $workSheetName } else { error "$workbook is not a valid workbook" } } proc excel::createStyle {workbook args} { # # @comment decode a string previously encoded using excel::encode # @argument str string to decode # @result undecoded string # variable data if {[info exists excel::styles($workbook)]} { incr excel::styles($workbook) } else { set excel::styles($workbook) 2 } set styleName s$excel::styles($workbook) foreach {name value} $args { # check that name is valid if {[lsearch "-font -fontcolor -background -bold" $name]==-1} { error "style option $name option is not supported" } set data($workbook,styles,$styleName,$name) $value } return $styleName } proc excel::setColumnType {workSheet columnIndex type} { # # @comment # @argument str string to encode # @result encoded string # variable data _checkSpreadSheet $workSheet set data($workSheet,row,$columnIndex,type) [string totitle $type] } proc excel::_checkSpreadSheet {workSheet} { variable data if {![info exists data(workSheet,$workSheet)]} { error "$workSheet is not a valid workSheet" } } proc excel::addRow {workSheet args} { # # @comment reverses hebrew # @argument str string to reverse # @result reversed string # variable data set i 0 incr excel::rowCounter($workSheet) set data($workSheet,$excel::rowCounter($workSheet),length) [llength $args] foreach arg $args { incr i if {[llength $arg]>1} { if {[llength $arg]>2} { set data($workSheet,$excel::rowCounter($workSheet),$i,style) [lindex $arg 2] } set data($workSheet,$excel::rowCounter($workSheet),$i,type) [string totitle [lindex $arg 1]] } set data($workSheet,$excel::rowCounter($workSheet),$i,data) [lindex $arg 0] } return row$excel::rowCounter($workSheet) } proc excel::asXml {workbook} { # # @comment change hebrew string to dos hebrew # @argument str string to change to dos format # @result string in dos format # variable data variable rowCounter set xml "\ \ \ \ Ashrait\ [clock format [clock seconds] -format {%Y-%m-%dT%H:%M:%SZ}]\ Xor Technologies\ \ \ " if {[info exists excel::styles($workbook)]} { for {set d 2} {$d<=$excel::styles($workbook)} {incr d} { set styleName s$d append xml "" } } append xml "" for {set d 1} {$d<=$excel::workSheets($workbook)} {incr d} { append xml "\ " set workSheet $excel::data(workSheet,$d) for {set i 1} {$i<=$excel::rowCounter($workSheet)} {incr i} { append xml "" for {set j 1} {$j<=$data($workSheet,$i,length)} {incr j} { if {[info exists data($workSheet,$i,$j,type)]} { set type $data($workSheet,$i,$j,type) } else { if {[info exists data($workSheet,row,$j,type]} { set type $data($workSheet,row,$j,type) } else { set type "Number" } } set dataValue $data($workSheet,$i,$j,data) if {[string index $dataValue 0]=="="} { append xml "" } else { append xml ">" } append xml "$dataValue" } append xml "" } append xml "
" } append xml "
" } proc excel::deleteWorkbook {book} { # # @comment create a workbook pointer # @argument str string to decode # @result undecoded string # variable data for {set d 1} {$d<=$excel::workSheets($workbook)} {incr d} { array unset data $d set workSheet $excel::data(workSheet,$d) for {set i 1} {$i<=$excel::rowCounter($workSheet)} {incr i} { array unset data $workSheet* } unset $excel::rowCounter($workSheet) } }