cheryld497 has asked for the wisdom of the Perl Monks concerning the following question:

I have a spreadsheet which is being used as a template. Beacuse I was having problems reading the formulas, the cells with formulas in the Excel document are actually text (eg. '=SUM(A1+A2)).

All the cells are read and written into a new document. Except for cells with formulas (in this case text) that link to another worksheet (=SHEET2!A1).

The error is: Unknown sheet name Sales in formula at excel_testing.pl line 265

The script loops through the sheets, rows, and columns, and writes them back like so:
$value = $storage_book->{$sheet}->{$row}->{$col}; $dest_sheet->write($row, $col, $value);

Any ideas as to why the unknown sheet name is happening?

Replies are listed 'Best First'.
Re: Perl Excel Formulas With Worksheet Links
by VSarkiss (Monsignor) on Feb 12, 2003 at 22:41 UTC

    I'm guessing from your code that you're using Spreadsheet::WriteExcel. The only way I can see that error happening is if the sheet name snuck into a formula. What I would suggest is to check for it explicitly, something like this:

    $value = $storage_book->{$sheet}->{$row}->{$col}; if ($value =~ /sales!/i) { print "The culprit cell is at $row:$col\n"; } $dest_sheet->write($row, $col, $value);
    This is just a SWAG. If that's not the problem, please post a reply with more details: some example data, the modules you're using, and a wider window around line 265.

Re: Perl Excel Formulas With Worksheet Links
by mugwumpjism (Hermit) on Feb 12, 2003 at 22:42 UTC

    You should really list the name of the module so we can look it up on CPAN to check the syntax :-).

    Presumably something is sanity checking what it's writing out; do you have a sheet called `Sales' in the source?

    Put debugging statements in; print to standard output the row column and value for each iteration, and you will see where in the source spreadsheet the problematic value is. The Perl debugger (see perldebug) is an invaluable tool for hunting down messages that might not make any sense on first inspection.

    Otherwise, there's not enough code here to help - how are you making sure that `Except for cells with formulas (in this case text) that link to another worksheet' - do you do that in your code? Let's see the line of code! Perhaps that's the problem.

    Good Luck,
    Bug Powder Dust and $_.

Re: Perl Excel Formulas With Worksheet Links
by jmcnamara (Monsignor) on Feb 12, 2003 at 23:53 UTC

    From the syntax and the error I'm going to guess that you are using the Spreadsheet::ParseExcel::SaveParser module.

    Spreadsheet::ParseExcel::SaveParser is a combination of Spreadsheet::ParseExcel and Spreadsheet::WriteExcel.

    Spreadsheet::WriteExcel requires that you add a worksheet to a workbook before you can reference it in a formula. This is due to the fact that it has to convert the literal sheet name to an index.

    However, Spreadsheet::ParseExcel::SaveParser iterates through the worksheets one at a time rewriting any data that it finds. Due to the limitation stated above this methodology fails for formulas.

    Here is a quick and dirty patch to fix it. In SaveParser.pm at line 111 change:

    for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) { my $oWkS = $oBook->{Worksheet}[$iSheet]; my $oWrS = $oWrEx->addworksheet($oWkS->{Name});

    to:

    for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) { my $oWkS = $oBook->{Worksheet}[$iSheet]; $oWrEx->addworksheet($oWkS->{Name}); } for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) { my $oWkS = $oBook->{Worksheet}[$iSheet]; my $oWrS = ($oWrEx->sheets())[$iSheet];

    --
    John.

Re: Perl Excel Formulas With Worksheet Links
by cheryld497 (Initiate) on Feb 12, 2003 at 23:37 UTC
    I am using
    use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel;
    Yes, there is a sheet named Sales in one of the formulas. From what I have read, this should be possible.

    Yes, their are two sheets. One named "How_Am_I_Doing" and the other "Sales".
    There are numerous other cells, that have other formulas, for instance on sheet Sales there is one '=D3*E2, which calculates just fine. But all the cells with worksheet links cause errors, by removing those cells from the spreadsheet, everything writes fine. Here is some more of the code:
    foreach my $sheet (keys %$storage_book){ my $dest_sheet = $dest_book->addworksheet($sheet); foreach my $row (keys %{$storage_book->{$sheet}}){ foreach my $col (keys %{$storage_book->{$sheet}->{$row}}){ if ($sheet eq "Sales") { if ($storage_book->{$sheet}->{$row}->{$col} eq 'Agent') { $getsales=1; #next row start getting sales } elsif ($storage_book->{$sheet}->{$row}->{$col} eq 'Total') { $getsales=0; #stop getting sales } #GET THE AGENTS NAME if (($getsales ==1) && ($col == 0)) { #performs a sql query here } } if (($getsales == 1) && ($col == 4) && ($sheet eq "Sales") && ($ +agentname eq "Agent")) { $dest_sheet->write($row, $col, $exchange_rate); } elsif (($getsales == 1) && ($col == 2) && ($sheet eq "Sales")) { if ($agentname eq "Agent") { $dest_sheet->write($row, $col, $storage_book->{$sheet}->{$ro +w}->{$col}); } else { $dest_sheet->write($row, $col, $cdnsales); } } elsif (($getsales == 1) && ($col == 3) && ($sheet eq "Sales")) { if ($agentname eq "Agent") { $dest_sheet->write($row, $col, $storage_book->{$sheet}->{$ro +w}->{$col}); } else { $dest_sheet->write($row, $col, $usdsales); } } else { if (($row == 0) && ($col == 1) && ($sheet eq "Sales")) { $dest_sheet->write($row, $col, $startdate); } elsif (($row == 0) && ($col == 3) && ($sheet eq "Sales")) { $dest_sheet->write($row, $col, $today); } else { ###HERE IS LINE 265 $dest_sheet->write($row, $col, $storage_book->{$sheet}->{$ro +w}->{$col}); } } } # foreach column } # foreach row } # foreach sheet $dest_book->close();

      It looks like my guess on the other node was wrong. :-)

      However, the solution is the same, iterate through the worksheets twice. The first time add the worksheets and the second time write the data:

      foreach my $sheet (keys %$storage_book){ $dest_book->addworksheet($sheet); } my $index = 0; foreach my $sheet (keys %$storage_book){ my $dest_sheet = ($dest_book->sheets())[$index++]; ...

      --
      John.

        Absolutely excellent. This did it, and now I understand these Excel modules a bit more. Thank you for your wonderful and timely response. Perlmonks will definitely be my new forum.
Re: Perl Excel Formulas With Worksheet Links
by physi (Friar) on Feb 12, 2003 at 23:27 UTC
    As far as I can remember you have to the formular for linking into another sheet like this:
    ='SHEET2'!A1
    but I'm not sure about that, cause my codes are in the company, I'll have a look tomorrow ...
    -----------------------------------
    --the good, the bad and the physi--
    -----------------------------------