How to use a variable in an Excel formula when using the module Spreadsheet:WriteExcel?

Posted on 2007-08-12
Medium Priority
Last Modified: 2013-11-05
I'm using the Perl module Spreadsheet::WriteExcel.
I want to subsitute a variable for the cell rows, instead of writing each line like I did below??

Any ideas??

$worksheet->write(1, 3, '=IF(AND(F2="",H2="",J2=""),"",MAX(F2,H2,J2))', $format);
$worksheet->write(2, 3, '=IF(AND(F3="",H3="",J3=""),"",MAX(F3,H3,J3))', $format);
$worksheet->write(3, 3, '=IF(AND(F4="",H4="",J4=""),"",MAX(F4,H4,J4))', $format);
$worksheet->write(4, 3, '=IF(AND(F5="",H5="",J5=""),"",MAX(F5,H5,J5))', $format);
$worksheet->write(5, 3, '=IF(AND(F6="",H6="",J6=""),"",MAX(F6,H6,J6))', $format);
$worksheet->write(6, 3, '=IF(AND(F7="",H7="",J7=""),"",MAX(F7,H7,J7))', $format);
$worksheet->write(7, 3, '=IF(AND(F8="",H8="",J8=""),"",MAX(F8,H8,J8))', $format);
$worksheet->write(8, 3, '=IF(AND(F9="",H9="",J9=""),"",MAX(F9,H9,J9))', $format);
$worksheet->write(9, 3, '=IF(AND(F10="",H10="",J10=""),"",MAX(F10,H10,J10))', $format);
$worksheet->write(10, 3, '=IF(AND(F11="",H11="",J11=""),"",MAX(F11,H11,J11))', $format);
$worksheet->write(11, 3, '=IF(AND(F12="",H12="",J12=""),"",MAX(F12,H12,J12))', $format);
$worksheet->write(12, 3, '=IF(AND(F13="",H13="",J13=""),"",MAX(F13,H13,J13))', $format);
$worksheet->write(13, 3, '=IF(AND(F14="",H14="",J14=""),"",MAX(F14,H14,J14))', $format);
$worksheet->write(14, 3, '=IF(AND(F15="",H15="",J15=""),"",MAX(F15,H15,J15))', $format);
$worksheet->write(15, 3, '=IF(AND(F16="",H16="",J16=""),"",MAX(F16,H16,J16))', $format);
$worksheet->write(16, 3, '=IF(AND(F17="",H17="",J17=""),"",MAX(F17,H17,J17))', $format);
$worksheet->write(17, 3, '=IF(AND(F18="",H18="",J18=""),"",MAX(F18,H18,J18))', $format);
$worksheet->write(18, 3, '=IF(AND(F19="",H19="",J19=""),"",MAX(F19,H19,J19))', $format);
$worksheet->write(19, 3, '=IF(AND(F20="",H20="",J20=""),"",MAX(F20,H20,J20))', $format);
$worksheet->write(20, 3, '=IF(AND(F21="",H21="",J21=""),"",MAX(F21,H21,J21))', $format);
$worksheet->write(21, 3, '=IF(AND(F22="",H22="",J22=""),"",MAX(F22,H22,J22))', $format);
$worksheet->write(22, 3, '=IF(AND(F23="",H23="",J23=""),"",MAX(F23,H23,J23))', $format);
$worksheet->write(23, 3, '=IF(AND(F24="",H24="",J24=""),"",MAX(F24,H24,J24))', $format);

Question by:Richard Kreidl
  • 2
LVL 17

Expert Comment

ID: 19680057
Do you mean:

#!/usr/bin/perl -w
use strict;
my $count = 1;

for (1..23) {
    print "\$worksheet->write($count, 3, '=IF(AND(";
    print "F$count=\"\",H$count=\"\",J$count=\"\"),\"\",MAX(F$count,H$count,J$count))', \$format)\n";

Author Comment

by:Richard Kreidl
ID: 19680115
your solution justs print outs the formula 23 times.

I changed your code slightly to this:

my $count = 1;
for (1..23) {
    $worksheet->write($count, 3, '=IF(AND(F$count=\"\",H$count=\"\",J$count=\"\"),\"\",MAX(F$count,H$count,J$count))', $format);

but it still doesn't work
LVL 17

Accepted Solution

mjcoyne earned 2000 total points
ID: 19680435
#!/usr/bin/perl -w
use strict;
use Spreadsheet::WriteExcel;

my $workbook = Spreadsheet::WriteExcel->new("perl.xls");
my $worksheet = $workbook->add_worksheet();

my $format = $workbook->add_format();

my $row = 1;
my $col = 2;
for (1..23) {
    $worksheet->write($row, 3, "=IF(AND(F$col=\"\",H$col=\"\",J$col=\"\"),\"\",MAX(F$col,H$col,J$col))", $format);

Puts the formulas in the cells for me -- I haven't evaluated the accuracy of your fomula, but the code enters the requested formulas in cells D2 through D24...

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question