Hi i need a bit of help with some ole automation specificaly with updating a chart in an excel spreadsheet using perl. What i am trying to do is update sourceData for "Chart 1" on the Requests sheet. The sourceData for that chart currently has the values of '=Data!$A$1:$A$291,Data!$G$1:$G$291' what i am trying to do change it to '=Data!$A$1:$A$295,Data!$G$1:$G$295' for example. This Excel spreadsheet is already prebuilt contains a number of charts on the request,performance and errors pages. Here is the code:
sub updateExcelCounters { my $excelToUpdate = shift; my @dataFiles = @_; ###print "Excel: $excelToUpdate files; @dataFiles .\n"; my $Excel = Win32::OLE->new("Excel.Application"); $Excel->{Visible} = 1; print "Excel: Opening Excel $excelToUpdate \n"; my $Book = $Excel->Workbooks->Open($excelToUpdate); # open Excel +file print "book : $Book\n"; my $DataSheet = $Book->Worksheets("Data"); my $RequestsSheet = $Book->Worksheets("Requests"); my $PerformanceSheet = $Book->Worksheets("Performance"); my $ErrorsSheet = $Book->Worksheets("Errors"); my $file; foreach $file (@dataFiles) { if($file =~ /counters\.csv/) { my $line; my $Range; my @items; my $valueRange; my $lastSourceData; my $i = 1; $DataSheet->Activate(); open(IN,"<$file") || die "$!\n"; while($line = <IN>) { chomp $line; @items = split/,/,$line; $valueRange = "A" .$i . ":". "I" .$i; $Range = $DataSheet->Range($valueRange); #print "Range $Range\n"; ### Create a refrence to @items and insert it into exce +l $Range->{Value} = [\@items]; $i++; } close(IN); ### Get the value that will be user to update all the grap +hs SourceData with; $lastSourceData = $i -1; print "lastSourceData: $lastSourceData \n"; $RequestsSheet->Select(); ### Here we update the graphs sourcedata my $Chart1 = $RequestsSheet->ChartObjects("Chart 1"); $Chart1->Activate(); my $name = $Chart1->Name(); print "Chart1 Name: ", $name ,"\n";\ $RequestsSheet->ChartObjects("Chart 1")->Activate(); $Chart1->SetSourceData({Source => '=Data!\$A\$2:\$A\$295'} +); } ### Closing if($file } ### Closing the foreach }

In reply to OLE - updating a chart in an excel spreadsheet by Rom399

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.