Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

spreadsheet formula issue

by PerlSufi (Friar)
on May 01, 2013 at 15:30 UTC ( #1031589=perlquestion: print w/replies, xml ) Need Help??
PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I am having a problem getting my script to write formulas for subtraction. Here is my code:
$sth = $dbh->prepare($query); $sth->execute(); # create new excel file with 2 worksheets my $xWB = Spreadsheet::WriteExcel->new('trusmart_billing_201302.xls'); my $xWS = $xWB->add_worksheet('Summary'); #set formats my $format = $xWB->add_format(); $format->set_bold(); $xWS->set_column('A:A', 20); $xWS->set_column('E:E', 20, $format); $xWS->set_column('C:C', 20, $format); $xWS->set_column('B:B', 20, $format); my $xWS2 = $xWB->add_worksheet('Details'); #set columns A through D $xWS2->set_column('A:D', 20); # Add column headers for first worksheet my $R=0; my $C=0; $xWS->write($R, $C++, $_) for @{$sth->{NAME}}; # Read the query results and write them into the spreadsheet while (my $ar=$sth->fetchrow_arrayref) { ++$R; $C=0; $xWS->write($R, $C++, $_) for @$ar; } $xWS->write(0,5, 'Additional Images'); $xWS->write_formula('E2', '=(D2-B2)'); $xWS->write_formula('E3', '=(D3-B3)'); $xWS->write_formula('E4', '=(D4-B4)'); $xWS->write_formula('E5', '=(D5-B5)'); $xWS->write_formula('E6', '=1+5'); # as a test, this also did not work

It is writing 0 as the results in the E column cells. why? Thank you for your input

Replies are listed 'Best First'.
Re: spreadsheet formula issue
by Kenosis (Priest) on May 01, 2013 at 15:58 UTC

    I ran your script using the following modified lines, as it didn't require a database transaction:

    #set columns A through D $xWS2->set_column( 'A:D', 20 ); # Add column headers for first worksheet my $R = 0; my $C = 0; $xWS->write( $R, $C++, $_ ) for 'A' .. 'Z'; # Read the query results and write them into the spreadsheet for ( 1 .. 26 ) { ++$R; $C = 0; $xWS->write( $R, $C++, $_ ); }

    Results in cells E2:E6:

    0 0 0 0 6

    The formulas in E2:E5 of the "Summary" sheet evalute to 0 because columns B and D are empty in the spreadsheet your script creates. However, you'll note that the '=1+5' in cell E6 is correctly evaluated.

    Hope this is helpful.

      Ok. But why is it empty? because the workbook is still open? Do I need to close it and then re open it?
Re: spreadsheet formula issue
by hdb (Monsignor) on May 01, 2013 at 16:25 UTC
      Thanks. I was just reading the parse excel documentation. I will try that
        FIXED. The problem was that I did not enable editing when I saved and opened the excel file to view it!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1031589]
Approved by marto
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-01-19 04:31 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.