Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Excel::Writer::XLSX; Multi-worksheets formulas disappear on loading

by Bananorpion (Initiate)
on Jan 14, 2018 at 23:01 UTC ( #1207240=perlquestion: print w/replies, xml ) Need Help??
Bananorpion has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks

After a year far from Perl, I've been drawn to it once again, and for the first time in a while, I've stumbled upon something I can't google my way out.

Here's the context: the current script I use for work creates a csv file, which is subsequently pasted into a worksheet, in an xlsx file. This works fine, but this week-end I decided to improve it, and to create the Excel file from scratch via the script, removing the human manipulation inbetween. It almost works, barring one detail: some formulas (Every formula referring to cells in another worksheet.) disappear when Microsoft Excel opens the file, with a not-really-helpful error message. The formulas are correct : they are exactly the same I used manually before, and if I input them myself in the newly created file, everything works.

As it may be obvious, I can't fully assert this is a Perl problem, but this is the track I want to follow first.

Below is a shortest-I-could version of the code I'm using (With random data and no formats, but the exact same structure regarding the problematic formula), which generates a file with the same error message on loading.

#!/usr/bin/env perl use v5.010; use warnings; use strict; use Excel::Writer::XLSX; my $out = 'testfile'; my $excel = Excel::Writer::XLSX->new("$out.xlsx"); my $README = $excel->add_worksheet('README'); my $log_table = $excel->add_worksheet($out); # Log table ___________________ $log_table->write_row('A1', ['ColA', 'ColB', 'ColC', 'ColD', 'ColE']); sub randChar { @_ = qw/A B C D E/; return $_[(int rand 4)]; } my $current_line = 2; for (0 .. 9) { $log_table->write_row("A$current_line", [randChar(), randChar(), r +andChar(), randChar(), lc randChar()]); $current_line++; } $log_table->autofilter("A1:K$current_line"); # README ___________________ $current_line = 1; for (qw/a b c d e/) { $README->write_row("A$current_line", [$_, "=NB.SI('$out'!E:E;A$cur +rent_line)"]); $current_line++; } $excel->close();

Has anyone ever encountered the same error and found how to fix it? I've been browsing the Excel doc, then the Excel::Writer::XLSX module doc, but I can't find anything related, and I prefer to ask for opinions before exploring in-depth how the module creates the files.. (There is no hurry, but I don't like leaving a script unfinished.)

(If relevant, this has only be tested in Windows 10 OS, since it's supposed to eventually run on a Windows server, I haven't tried it in a Linux OS yet.)

Thanks in advance for any help. \o/

  • Comment on Excel::Writer::XLSX; Multi-worksheets formulas disappear on loading
  • Download Code

Replies are listed 'Best First'.
Re: Excel::Writer::XLSX; Multi-worksheets formulas disappear on loading
by tangent (Vicar) on Jan 15, 2018 at 00:39 UTC
    In the docs for Excel::Writer::XLSX there is a section on formulas titled 'Non US Excel functions and syntax' which states:
    Excel stores formulas in the format of the US English version, regardless of the language or locale of the end-user's version of Excel. Therefore all formula function names written using Excel::Writer::XLSX must be in English.
    So you need to change the =NB.SI to =COUNTIF. The same section also states:
    Formulas must be written with the US style separator/range operator which is a comma (not semi-colon).
    So you need to change the E;A$current_line to E,A$current_line.
    $README->write_row("A$current_line", [$_, "=COUNTIF('$out'!E:E,A$curre +nt_line)"]);
    When I made those two changes to your example code the formulas worked correctly.

      Indeed, I wasn't on the right track and totally missed that, and I didn't even know US functions had different names.. That's two things I learned then. This all runs fine now, thanks a lot. ;)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-07-21 17:03 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (449 votes). Check out past polls.