Perl - Win32::OLE - Excel Formulas

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prasanna CRN
    New Member
    • Mar 2011
    • 11

    Perl - Win32::OLE - Excel Formulas

    I am a Perl Programmer & i am using WIN32::OLE for Excel operations.

    I wanted to use conditional formulas like 'if' in excel via Perl.

    Thanks in Advance.
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    I personally would use Spreadsheet::Wr iteExcel as Win32::OLE does not provide great documentation.

    At the very least, maybe you could create a test excel file with functions and examine it using Win32::OLE to determine how they are included.

    - Miller

    Comment

    • Prasanna CRN
      New Member
      • Mar 2011
      • 11

      #3
      Hi Miller,
      Thanks for your Interest & I found the answer.

      Code:
      "=IF(RC[$prev]>RC[$curr],RC[$prev]-RC[$curr],\"PASS\")"
      The reason why i am using Win32::OLE is, no need of additional Module Installation.
      Last edited by miller; Apr 1 '11, 08:01 PM.

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Of course, Just note that anytime you want to use quotes " instead of an interpolated string, you can use the qq operator instead.

        Code:
        qq{=IF(RC[$prev]>RC[$curr],RC[$prev]-RC[$curr],"PASS")}

        Comment

        • Prasanna CRN
          New Member
          • Mar 2011
          • 11

          #5
          Thanks Miller.

          But Folks, any idea on Arithmetic Operation with Strings?

          Ex : I can do auto increment of string : my $a = 'A';
          $a ++ ; #Will give 'B'

          Like that, i want to do : $a += 5 ; #I expect 'F', but perl throws error on this. Any idea on my thing?

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            When you do $a++, perl will increment the ascii value, but when you do $a += 5, you're attempting to add a numerical number which is not the same.

            To accomplish what you want, you'd need to do this.
            Code:
            my $letter = 'a';
            $letter = chr(ord($letter)+5);

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              As RonB pointed out, the fact that the increment ++ and decrement -- operators work on strings is a special feature. To get the same behavior out of += or any other operator would require overloading them.

              The best method is to just use the method that RonB demonstrated.

              - Miller

              Comment

              • Prasanna CRN
                New Member
                • Mar 2011
                • 11

                #8
                Thanks Guys for your Awesome Response!!!

                RonB,
                I accept that. But even with this option i have a problem:
                my $letter = 'A';
                my $test;

                for ($i = 1; $i <= 100; $i++){

                $test = chr(ord($letter ));
                print "Orig Char : $letter; Exp Char : $test\n";
                $letter++;
                }

                This will fine upto 26 (A to Z), After 26th Iteration, $letter will become 'AA' where the function will fail (Since it will take 1 char as argument).

                Any alternative way to address this problem.

                Comment

                • miller
                  Recognized Expert Top Contributor
                  • Oct 2006
                  • 1086

                  #9
                  Yes, if you increment a string it will go from A to Z and then to AA AB, etc. You say that's not what you want, but you haven't described what your ideal behavior actually is?

                  What are you wanting your string incrementor to do? In theory you're going through 100 values, what do you want them to be?

                  Comment

                  • Prasanna CRN
                    New Member
                    • Mar 2011
                    • 11

                    #10
                    Miller,
                    At the begining i mentioned that i am working in Excel.
                    The Excel Cells Starts from 'A' & it goes like AA - AZ, BA - BZ & so on.. This is my Use case!

                    Hope it will narrow your ideas.

                    Comment

                    • miller
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1086

                      #11
                      You don't need that incremental translation using chr and ord. Instead just rely on your original variable.

                      Code:
                      my $column = 'A';
                      
                      for my $i (1..100) {
                      	print "$column ";
                      	$column++;
                      }
                      Outputs

                      A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ
                      AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH BI BJ
                      BK BL BM BN BO BP BQ BR BS BT BU BV BW BX BY BZ CA CB CC CD CE CF CG CH CI CJ CK
                      CL CM CN CO CP CQ CR CS CT CU CV

                      Comment

                      • miller
                        Recognized Expert Top Contributor
                        • Oct 2006
                        • 1086

                        #12
                        Note, an even cleaner way of doing it is to use the .. operator.

                        Code:
                        for my $column ('A'..'CV') {
                            print "$column ";
                        }

                        Comment

                        • miller
                          Recognized Expert Top Contributor
                          • Oct 2006
                          • 1086

                          #13
                          It's also good to know a functional way to translate a column number into excel notation. Here's one way to do that

                          Code:
                          sub excel_col {
                          	my $val = shift;
                          
                          	my $str = '';
                          
                          	do {
                          		$val-- if $str ne '';
                          		my $r = $val % 26;
                          		$val = ($val - $r) / 26;
                          		$str = chr(ord('A') + $r) . $str;
                          	} while ($val > 0);
                          
                          	return $str;
                          }
                          
                          for my $i (0..1000) {
                          	print excel_col($i) . ' ';
                          }
                          Last edited by miller; Apr 4 '11, 07:52 PM.

                          Comment

                          • Prasanna CRN
                            New Member
                            • Mar 2011
                            • 11

                            #14
                            Thanks Miller. I had used alternative way as per my use case.(with some limitations)
                            But yours will be applied in broader manner.

                            Comment

                            Working...