srivasva has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks , I need help where I want to copy formula from a cell and paste it in another cell. I am able to retrieve formula by :
$Formula1= $Sheet->Range('A499')->{Formula};
but I want to use this formula in next row with a row increment in formula.
eg. Formula -> "=MAX(B499:AR499)"
if I want to paste it in row 500 it should become
"=MAX(B500:AR500)"
as it happens with copy paste in Excel. Plz guide me. I am looking solution from Win32::OLE where I need not to do workaround of regex substitution coz I have around 50 formula's to substitute. I think it can be done by copy & paste but can someone plz guide me how to go for it in Win32::OLE?

Replies are listed 'Best First'.
Re: Win32::OLE Excel formula Copy
by Andrew Coolman (Hermit) on Nov 25, 2008 at 13:10 UTC
    Since you already can get the formula and you can figure out the new row somehow so you just need to change row then use of substitution might work. Similar for columns.
    $Formula1= $Sheet->Range('A499')->{Formula}; $new_row = 500; $Formula1 =~ s/\d+/$new_row/g; $Sheet->Range('A'.$new_row)->{Formula} = $Formula1;

    Regards,
    s++ą  ł˝ ął. Ş ş şą Żľ ľą˛ş ą ŻĽąş.}++y~-~?-{~/s**$_*ee
      Thanks for the solution. But I am looking for a more generic solution as I have around 50 different formulas to be substituted with new row and they are spread into different columns . eg.
      =IF(C489=0,0,(ABS(C492-C489)/C489))
      in the above formula above substitution $Formula1 =~ s/\d+/$new_row/g; will not work.
        If you want the behaviour of the copy & paste mechanism, you should try to use that: select the cells, copy them, move the cursor to another cell, paste there.

        I'm not familiar with OLE at all, but I'm quite sure that this has to work somehow, and I even suspect you'll find some code examples if you search hard enough.

Re: Win32::OLE Excel formula Copy
by oko1 (Deacon) on Nov 25, 2008 at 13:25 UTC

    All you need to do is increment the number for the row you want to copy to.

    my $Formula = $Sheet->Range("A499")->{Formula}; # The next 10 rows print "=MAX(B$_:AR$_)" for 500..510;

    Obviously, you'd change the print statement into whatever method you need to invoke to paste your formula.


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: Win32::OLE Excel formula Copy
by ww (Archbishop) on Nov 25, 2008 at 13:31 UTC
    ++ Andrew_Coolman and oko1.

    TIMTOWTDI (a very linear way without using the native abilities of a module (possibly use Win32::OLE::Const 'Microsoft Excel'; or similar):

    #!usr/perl/bin use strict; use warnings; my $Formula = "=MAX(B499:AR499)"; #desired: "=MAX(B500:AR500)" my ($formula_begin, $therest) = split /\({1}/, $Formula; # ESCAPE the + open paren my ($cell1, $cell2) = split /:/, $Formula; my ($cell1a, $cell1b) = split /\(/, $cell1; my ($cell2a, $closeparen) = split /\)/,$cell2; # One way to get rid of + the trailing ')' so we can increment the number, later. print "First cell referenced: $cell1b, second cell referenced: $cell2a +\n\n"; $cell1b =~ /([A-Z]+)(\d+)/; my $cell1balpha = $1; my $cell1bdigits = $2; $cell2a =~ /([A-Z]+)(\d+)/; my $cell2aalpha = $1; my $cell2adigits = $2; ++$cell1bdigits; ++$cell2adigits; print "AFTER INCREMENT, first cell referenced: $cell1balpha" . $cell1b +digits . "; second cell referenced: $cell2aalpha" ."$cell2adigits\n\n +"; print "\tRefs adjusted formula: $formula_begin" . "($cell1balpha" . $c +ell1bdigits. ":$cell2aalpha" ."$cell2adigits" . ")\n";

    Output: Refs adjusted formula: =MAX(B500:AR500)

    This is not the recommended way but may serve to illustrate one way of dealing with the likes of cell references in OO's scalc or Excel.

    Added plaudits to oko1, missing colon in last code item, output, and the missing "in" in last paragraph

Re: Win32::OLE Excel formula Copy
by Corion (Patriarch) on Nov 26, 2008 at 13:26 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.