View · Search · Index
No registered users in community xowiki
in last 10 minutes

[Xotcl] a small spreadsheet in xotcl

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Fri, 13 Apr 2001 01:57:22 +0200

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

#!/usr/local/bin/xowish
# 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