Hi Grygonos, thanks for the help. I tried to use your code to generate a chart instead, but had no luck. I find this OLE stuff very tricky!

I am trying to graph some data. The easiest way to show you what I'm trying to do is to show you the spreadsheet, have a look at the picture on my home node.

To make life easier, I created a template spreadsheet, which I open and insert the data into. (Although I would prefer not to use a template, and create the entire spreadsheet on the fly.) The problem occurs when the data range does not match the range in the template. Say I designed the graph to cope with 31 days worth of data, and the user only wants to plot 7 days worth of data. What happens is that the 1st 6 days of data occupy the same area as the fisrt 6 days of 31 days worth of data.

So, I started a macro, and changed the data range of the chart to match the data, and it cured the problem. However, when I tried to convert the macro so I could do this from perl, it didn't work.

The macro looked like this:

Sub Macro1() Sheets("Graph").Select ActiveSheet.ChartObjects("Chart 1").Activate ActiveChart.SeriesCollection(1).Select ActiveChart.SetSourceData Source:=Sheets("Data").Range("A1:C49"), +PlotBy:= _ xlColumns End Sub
From this macro, I produced this script. It doesn't work as expected though. If you un-comment the '$chart -> delete();' line, the chart does get deleted, so I know I've got the chart, but the marked line causes an error, and the data range doesn't get changed.
use strict; use warnings; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); $Win32::OLE::Warn = 0; my $template = "D:/My Scripts/test_chart.xls"; my $outexcel = "D:/My Scripts/test_chart1.xls"; my $excel; eval {$excel = Win32::OLE->GetActiveObject('Excel.Application')}; die "WARNING! Excel not installed" if $@; unless (defined $excel) { $excel = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) +or die "ERROR, cannot start Excel"; } $excel -> {Visible} = 1; $excel->{DisplayAlerts} = 0; die "Cannot find template $template\n" unless (-e $template); my $workbook = $excel -> Workbooks -> Open($template); my $range = "A1:C49"; my $sheet = $workbook -> Worksheets("Graph"); $sheet -> Activate(); my $chart = $sheet -> ChartObjects("Chart 1"); $chart -> Activate(); my $collection = $chart -> SeriesCollection(1); $collection -> Select(); # Can't call method "Select" on an undefined +value $chart->SetSourceData({Source => "$sheet", Range => "$range", PlotBy = +> "xlColumns"}); # $chart -> delete(); #This will work! # save the spreadsheet $workbook->SaveAs($outexcel); undef $workbook; undef $excel; exit();
The data is on sheet2 (named "Data") and occupies a range of A2:C745, if there is a full month's worth. The headings for the columns are in A1:C1.

On sheet1, called "Chart" is a chart of this data. I have put a picture of these sheets on my home node.


In reply to Re^2: Excel macro to perl conversion. by spikey_wan
in thread Excel macro to perl conversion. by spikey_wan

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.