Home » excel » Perl: Reading cell value as string set via write_formula() using Excel Modules

Perl: Reading cell value as string set via write_formula() using Excel Modules

Posted by: admin April 23, 2020 Leave a comment


I am new to Perl and trying to automate some test cases. I am using Spreadsheet::WriteExcel and Spreadsheet::ParseExcel modules.

I am not able to read a particular cell value as string though when I open the same .xls file in Windows I can see the value as string (PASS/FAIL).

I am using write_formula() method to write the string. I have created a sample project to simulate the same. I have made it as simple as possible.

Little bit of explanation as to what the code below is trying to do:

Write the following in a row => 10 10 PASS 10 10 PASS

The while loop controls how many time we write 10 10 PASS

1st 10 is the actual value, 2nd 10 is the expected value and then PASS or FAIL using formula.

Can you please suggest why I can not read PASS or FAIL as string using value() method in the code below:

use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Utility;

my $filename = "sample.xls";

my $workbook = Spreadsheet::WriteExcel->new($filename);

my $worksheet = $workbook->add_worksheet();

my $col = 0;
my $row = 0;
my $next_test_offset = 0;
my $new_value_offset = 1;
my $result_offset = 2;

while($col < 2) {
    $worksheet->write($row, $col+$next_test_offset, 10);
    $worksheet->write($row, $col+$next_test_offset+$new_value_offset, 10);

    my $old_test_cell = xl_rowcol_to_cell($row, $col+$next_test_offset);
    my $new_test_cell = xl_rowcol_to_cell($row, $col+$next_test_offset+$new_value_offset);

    $worksheet->write_formula($row, $col+$next_test_offset+$result_offset, sprintf("=IF(%s<>%s, \"FAIL\", \"PASS\")", $old_test_cell, $new_test_cell));

    $next_test_offset +=2;


my $parser = Spreadsheet::ParseExcel->new();
my $parser_workbook = $parser->parse($filename);

if(!defined $parser_workbook)
    die $parser->error(), ".\n";

for my $parser_worksheet ($parser_workbook->worksheets())
    my ($row_min, $row_max) = $parser_worksheet->row_range();
    my ($col_min, $col_max) = $parser_worksheet->col_range();

    for my $row ($row_min .. $row_max)
        for my $col ($col_min .. $col_max)
            my $cell = $parser_worksheet->get_cell($row, $col);
            next unless $cell;

            print "Row: ", $row, " Col: ", $col, " Value: ", $cell->value(), "\n";

Forgot to paste the output

Row: 0 Col: 0 Value: 10
Row: 0 Col: 1 Value: 10
Row: 0 Col: 2 Value: 0
Row: 0 Col: 3 Value: 10
Row: 0 Col: 4 Value: 10
Row: 0 Col: 5 Value: 0

expected PASS at Row: 0 Col: 2 and Row: 0 Col: 5

Thanks in Advance!
– Arun

How to&Answers:

This is a documented problem (see http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel-0.59/lib/Spreadsheet/ParseExcel.pm#KNOWN_PROBLEMS):

This module cannot read the values of formulas from files created with
Spreadsheet::WriteExcel unless the user specified the values when
creating the file (which is generally not the case).

As a workaround you may replace the formula with an equivalent perl condition (e.g. $old_test_cell == $new_test_cell ? "PASS" : "FAIL") and just write the computed value as a simple value, not as a formula.