--- loncom/interface/spreadsheet/Spreadsheet.pm	2005/06/23 17:55:01	1.52
+++ loncom/interface/spreadsheet/Spreadsheet.pm	2005/09/01 21:47:14	1.55
@@ -1,5 +1,5 @@
 #
-# $Id: Spreadsheet.pm,v 1.52 2005/06/23 17:55:01 albertel Exp $
+# $Id: Spreadsheet.pm,v 1.55 2005/09/01 21:47:14 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -191,6 +191,8 @@ sub make_default {
             {'spreadsheet_default_'.$self->{'type'} => $self->filename()},
                                      $self->{'cdom'},$self->{'cnum'});
     return $result if ($result ne 'ok');
+    &Apache::lonnet::appenv('course.'.$self->{'cid'}.'.spreadsheet_default_'.
+			    $self->{'type'} => $self->filename());
     my $symb = $self->{'symb'};
     $symb = '' if (! defined($symb));
     &Apache::lonnet::expirespread('','',$self->{'type'},$symb);    
@@ -968,25 +970,32 @@ sub formulas {
         $self->{'formulas'} = $formulas;
         $self->{'rows'} = [];
         $self->{'template_cells'} = [];
+	$self->{'loaded'} = 1;
         return;
     } else {
-	if (!$self->{'loaded'}) {
-	    $self->{'loaded'}=1;
-	    # Load in the spreadsheet definition
-	    if (exists($env{'form.workcopy'}) && 
-		$self->{'type'} eq $env{'form.workcopy'}) {
-		$self->load_tmp();
-	    } else {
-		$self->load();
-	    }
-	}
+	$self->check_formulas_loaded();
         return %{$self->{'formulas'}};
     }
 }
 
+sub check_formulas_loaded {
+    my $self=shift;
+    if (!$self->{'loaded'}) {
+	$self->{'loaded'}=1;
+	# Load in the spreadsheet definition
+	if (exists($env{'form.workcopy'}) && 
+	    $self->{'type'} eq $env{'form.workcopy'}) {
+	    $self->load_tmp();
+	} else {
+	    $self->load();
+	}
+    }
+}
+
 sub set_formula {
     my $self = shift;
     my ($cell,$formula) = @_;
+    $self->check_formulas_loaded();
     $self->{'formulas'}->{$cell}=$formula;
     return;
 }
@@ -996,7 +1005,7 @@ sub set_formula {
 ##
 sub formulas_keys {
     my $self = shift;
-    my @keys = keys(%{$self->{'formulas'}});
+    $self->check_formulas_loaded();
     return keys(%{$self->{'formulas'}});
 }
 
@@ -1007,6 +1016,7 @@ sub formulas_keys {
 sub formula {
     my $self = shift;
     my $cell = shift;
+    $self->check_formulas_loaded();
     if (defined($cell) && exists($self->{'formulas'}->{$cell})) {
         return $self->{'formulas'}->{$cell};
     }
@@ -1085,6 +1095,7 @@ sub rebuild_stats {
     my $self = shift;
     $self->{'rows'}=[];
     $self->{'template_cells'}=[];
+    $self->check_formulas_loaded();
     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
         push(@{$self->{'rows'}},$1) if ($cell =~ /^A(\d+)/ && $1 != 0);
         push(@{$self->{'template_cells'}},$1) if ($cell =~ /^template_(\w+)/);
@@ -1525,6 +1536,7 @@ sub outsheet_xml   {
     ## Will be rendered for the user
     ## But not on this day
     my $Str = '<spreadsheet type="'.$self->{'type'}.'">'."\n";
+    $self->check_formulas_loaded();
     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
         if ($cell =~ /^template_(\w+)/) {
             my $col = $1;
@@ -1653,8 +1665,10 @@ sub load {
             }
         }
         $filename=$self->filename(); # filename may have changed
-        $cachekey = join('_',($cnum,$cdom,$stype,$filename));
-        %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas};
+	$cachekey = join('_',($cnum,$cdom,$stype,$filename));
+	if (ref($formulas) eq 'HASH') {
+	    %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas};
+	}
 	$self->formulas($formulas);
 	$self->set_row_sources();
 	$self->set_row_numbers();
@@ -1672,6 +1686,7 @@ sub load {
 
 sub set_row_sources {
     my $self = shift;
+    $self->check_formulas_loaded();
     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
         next if ($cell !~ /^A(\d+)/ || $1 < 1);
         my $row = $1;
@@ -1682,6 +1697,7 @@ sub set_row_sources {
 
 sub set_row_numbers {
     my $self = shift;
+    $self->check_formulas_loaded();
     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
 	next if ($cell !~ /^A(\d+)$/);
         next if (! defined($value));
@@ -1735,6 +1751,8 @@ sub save {
                                 {'spreadsheet_default_'.$stype => $filename },
                                           $cdom,$cnum);
             return $reply if ($reply ne 'ok');
+	    &Apache::lonnet::appenv('course.'.$self->{'cid'}.'.spreadsheet_default_'.
+				    $self->{'type'} => $self->filename());
         } 
         if ($self->is_default()) {
             if ($self->{'type'} eq 'studentcalc') {