Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7889973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:14:04+00:00 2026-06-03T06:14:04+00:00

I need to create a dynamic Excel formula through a Perl script. I store

  • 0

I need to create a dynamic Excel formula through a Perl script.

I store a formula that contains a fake range

my $stored_formula = $worksheet->store_formula('=MEDIAN(A1:A2)');

In the script, I calculate the new range

$formula_range = $mm_arr[$mmindx-1] . "!" . 
                 num2col(2+$form_off) . ((($serv-1)*5)+5) . ":" .
                 num2col((2+31+$form_off)) . ((($serv-1)*5)+5);

where @mm_arr is an array ("Jan", "Feb", …)
and num2col is a function I use to translate column numbers to letters.

The line just below is the repeat_formula

$worksheet->repeat_formula(
     (($serv-1)*5)+4, 
     1+$mmindx, 
     $stored_formula, 
     undef, 
     'A1:A2', $formula_range
);

I expect to get:

=median(Feb!K3:AH3)

but instead I get:

=median(K3:AH3)

So the find/replace is somehow working, but I can’t really pin down how!

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:14:06+00:00Added an answer on June 3, 2026 at 6:14 am

    First, a few recommendations:

    There are way too many parentheses in your expressions which, coupled with the variables and functions whose definitions we can’t see make your code hard to parse visually. You also did not post a small, self-contained script that exhibits the problem, which means anyone who tries to help you has to set up a test script. Make it easy on others.

    For example, the line where you define $formula_range can be re-written as follows:

    my $formula_range = sprintf('%s!%s%d:%s%d',
        $mm_arr[$mmindx - 1],
        num2col(2 + $form_off),
        5 + 5 * ($serv - 1),
        num2col(2 + 31 + $form_off),
        5 + 5 * ($serv - 1),
    );
    

    Keep in mind you can use functions provided by Spreadsheet::WriteExcel::Utility to convert between cell notation and row/column indexes.

    The problem seems to be that if the stored formula is not in the form SheetName!Range, then the substitution chops off the sheet name in the replacement, and only puts in the range.

    The reason for this seems to have to do with the parser setup in Spreadsheet::WriteExcel::Formula. A plain range is parsed as a range2d token whereas a range with a sheet reference is parsed as a range3d token. Later, in repeat_formula, the replacement is done on tokens. From what I can gather, a range2d token looks like '_ref2dA1:A10'. So, '_ref2dA1:A2' gets transformed into '_ref2dFeb!A1:10' in repeat formula. However, when it is passed to Spreadsheet::WriteExcel::Formula::parse_tokens, the code still classifies it as a range2d token based on the prefix. I gather the final call $parse_str .= $self->_convert_ref2d($token, $class); then turns it back into '_ref2dA1:A10. I am not sure if this could be classified as a bug, but it is definitely unexpected from the user’s POV.

    So, the solution is to put in the name of a sheet that is guaranteed to exist.

    Here is a test script:

    #!/usr/bin/env perl
    
    use strict; use warnings;
    use Spreadsheet::WriteExcel;
    use Const::Fast;
    
    const my @MONTHS => qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
    const my $WORKBOOK => 'test.xls';
    
    my $book = Spreadsheet::WriteExcel->new($WORKBOOK)
        or die "Cannot open '$WORKBOOK': $!";
    
    my %sheets = map {$_ => $book->add_worksheet($_)} Summary => @MONTHS;
    
    for my $m (@MONTHS) {
        for my $n (1 .. 10) {
            $sheets{ $m }->write_number($n - 1, 0, $n);
        }
    }
    
    my $formula = $sheets{Summary}->store_formula('=MEDIAN(Summary!A1:A2)');
    
    for my $n (0 .. 1) {
        my $replacement = sprintf(q{'%s'!A1:A10}, $MONTHS[$n]);
    
        $sheets{Summary}->repeat_formula($n, 0, $formula, undef,
            'Summary!A1:A2' => $replacement
        );
    }
    
    $book->close
        or die "Error closing '$WORKBOOK': $!";
    

    And here is a screenshot:

    Screenshot showing correct formulas

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamic application that need to create a window interface with checkbox.
I am working with Excel 2010. I need to create a macro, that after
I'm working on an ajax google maps script and I need to create dynamic
i need to create a dynamic SQL statement that selects fields based on an
I need to create a dynamic array in Java, but the values type differ
i want to write head tag. i need create dynamic <head id=htmlHead runat=server> </head>
I need to create dynamic 'Pay Now' buttons on my site, and PayPal says
HI all, i need to create an dynamic array, every time i add object,it
i have create 7 dynamic buttons in a scroll box. Each row need to
I need to create dynamic radio button in my table.I have a table in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.