[Xotcl] a small spreadsheet in xotcl

Gustaf Neumann neumann at wu-wien.ac.at
Fri Apr 13 01:57:22 CEST 2001

Dear Community,
here is a little spreadsheet programm in xotcl (well, it adds rows
and columns). maybe, someone has a usage for it.
best regards -gustaf

# a little spreadsheet class, translated from Richard Suchenwirth's example
# using tcl with namespages -gustaf neumann

Class SpreadSheet -parameter {widget {rows 3} {cols 2} {width 8}}
SpreadSheet instproc cell {pair value} {
  [self] set data($pair) $value
SpreadSheet instproc init {} {
  [self] instvar rows cols width widget
  [self] set last $rows,$cols  ;# keep grand total field
  trace var [self]::data w [list [self] redo]
  frame $widget
  for {set y 0} {$y <= $rows} {incr y} {
    set row [list]
    for {set x 0} {$x <= $cols} {incr x} {
      set e [entry $widget.$y,$x -width $width \
		 -textvar [self]::data($y,$x) -just right]
      if {$x==$cols || $y==$rows} {
	$e config -state disabled -background grey -relief flat
      lappend row $e
    eval grid $row -sticky news
  $e config -relief solid
  set widget
SpreadSheet instproc redo {varname el op} {
  [self] instvar cols rows
  if {$el != [[self] set last]} {
    foreach {y x} [split $el ,] break
    if {$x!=""} {
      [self] sum $y,* $y,$cols
      [self] sum *,$x $rows,$x
    } ;# otherwise 'el' was not a cell index
  }   ;# prevent endless recalculation of grand total
SpreadSheet instproc sum {pat target} {
  set sum 0
  set it "" ;# default if no addition succeeds
  foreach i [[self] array names data $pat] {
    if {$i!=$target} {
      catch {set it [set sum [expr {$sum+[[self] set data($i)]}]]}
    } ;# don't pull the total into the total
  [self] cell $target $it

# test and usage example 
SpreadSheet x -widget .s -cell 0,0 Spread1 -cell 1,0 47 -cell 2,1 11
SpreadSheet y -widget .t -rows 4 -cols 4 -cell 0,0 Spread2 -cell 1,0 12 -cell 2,2 22

pack .s .t -fill both

More information about the Xotcl mailing list