in reply to How to control (in a generic way) population of Excel file from another Excel file

I would put the mapping rules in another spreadsheet something like this

#!perl use strict; use Win32::OLE::Const 'Microsoft Excel'; Win32::OLE->Option(Warn => 3); my $ex = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $wb1 = $ex->Workbooks->Open("c:\\temp\\Book1.xlsx") ; my $wb2 = $ex->Workbooks->Add ; my $wb3 = $ex->Workbooks->Open("c:\\temp\\Mapping.xlsx"); my $sht1 = $wb1->Worksheets("Sheet1"); my $sht2 = $wb2->Worksheets("Sheet1"); my $sht3 = $wb3->Worksheets("Sheet1"); my $lastRow = $sht3->UsedRange->Find({What=>"*", SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row}; # Copy columns for my $n (1..$lastRow){ my $in = $sht3->Range("A$n")->Value; my $out = $sht3->Range("B$n")->Value; print "Mapping rule $n : $in => $out\n"; $sht1->Range("$in:$in")->Copy( $sht2->Range("$out:$out") ); } $wb2->SaveAs("c:\\temp\\Book2.xlsx"); undef $ex;
poj
  • Comment on Re: How to control (in a generic way) population of Excel file from another Excel file
  • Download Code

Replies are listed 'Best First'.
Re^2: How to control (in a generic way) population of Excel file from another Excel file
by woland99 (Beadle) on Nov 10, 2015 at 20:11 UTC
    Thank you! That is interesting idea. Having rules in a spreadsheet would allow for much easier organization of rule definition. I could have rules that would require multiple steps (each step would be a row - with say 1st column specifying grouping). I could easily make them as complex as I want by adding data to a rule in as many columns as needed and interpreting that data based on header. I guess XML would be another way of defining rules but spreadsheet is more visual.