# $Header: /home/cvsroot/tcldb/ucodb/Tlib/doLoadSheet,v 1.4 1998/02/16 04:39:12 de Exp $
# tcl procs saved on Tue Sep 05 09:32:41 PDT 1995

proc doLoadSheet {t win filename} {

  global CellValues CellFormulae

#
#	future modes:  HTML, LaTeX
#

        set tw [sheetWin $t]
        set msgw $tw.msg

  set openrc [catch {set f [open $filename r]}]
  
  if $openrc==1 {
    setMsg $msgw "Sorry, cannot open $filename for writing."
    return -1
  }

  close $f

  set cells [$t curselection]

#       puts stderr "Got Cells $cells"
  set rows ""
  if {$cells == ""} {
        puts stderr "ingest whole sheet"
        set r1 1
        set c1 1
        set r2 [$t cget -rows]
        set c2 [$t cget -cols]
  } else {
        lassign [split [lindex $cells 0] ,] r1 c1
        lassign [split [lindex $cells end] ,] r2 c2
        puts stderr "position is $r1,$c1 extends to $r2,$c2"
  }


  set rs [$t cget -rows]
  set cs [$t cget -cols]

  set i $r1
  set c $c1
  for_file line $filename {
	set line [split $line \t]
	set ll [llength $line]
	loop j 1 [expr $ll+1] {
		set v [lvarpop line]
		if {$v == ""} {continue}
		if {[string first = $v] == 0} {
			set CellFormulae($i,$j) [crange $v 1 end]
			cellEval $t $i,$j
		} else {
			catch {set CellValues($i,$j) $v}
		}
	}
	incr i
  }
  setMsg $msgw "Loaded sheet contents from TSV $filename"
  destroy $win

}

