in reply to modify perl 'excel to xml'
Hello cibien, and welcome to the Monastery!
I can’t really help you with your problem, since I don’t understand what you are trying to achieve. You ask:
is possible to do the same for propriety?
but I have no idea what this means. Some examples — input and desired output — would be useful for explaining what you want to do.
Also, you should supply some sample input for the existing code, together with the output it produces, so the monks can experiment on the code and verify that it still does what it should. See How do I post a question effectively?. Also specify the version of Perl you are using. (From the line system("cls"); it appears the script is running on Windows.)
I gather you inherited the code you posted from someone else. You should be aware there are a few issues with this code as it stands:
But the real problem with the code posted is that it’s just — well — too long and complicated to wade through. In other words, it’s in desperate need of refactoring.
To make a start: the main (i.e., top-level) code could usefully be re-written like this:
{ system("cls"); # Read command line arguments my ($materialmapping_file, $data_folder) = @ARGV; print "OPTIONS\nmaterialmapping_file: $materialmapping_file\n" . "data_folder: $data_folder\n"; if (length $materialmapping_file && length $data_folder) { opendir(my $dir, $data_folder) or die "Error in opening dir $data_folder\n"; my $materialmapping_table_xml = XML::LibXML->createDocument('1.0', 'UTF-8'); my $materialmapping_table_xml_root = $materialmapping_table_xml->createElement('masterdata'); $materialmapping_table_xml_root-> setAttribute('version', getSQLTimeStamp()); $materialmapping_table_xml-> setDocumentElement($materialmapping_table_xml_root); print "Parsing files...\n"; while (my $filename = readdir($dir)) { if ($filename =~ / \. xls $ /ix) { parse_xls_file($data_folder, $filename, $materialmapping_table_xml, $materialmapping_table_xml_root); } } $materialmapping_table_xml->toFile($materialmapping_file, 2); } }
which calls a new subroutine:
sub parse_xls_file { my ($data_folder, $filename, $materialmapping_table_xml, $materialmapping_table_xml_root) = @_; if (my $oBook = Spreadsheet::ParseExcel-> new()->parse($data_folder . $filename)) { for (0 .. $oBook->{SheetCount} - 1) { process_sheet($oBook, $_, $materialmapping_table_xml, $materialmapping_table_xml_root) } } print "Parsed $filename\n"; }
which in turn calls:
sub process_sheet { my ($oBook, $iSheet, $materialmapping_table_xml, $materialmapping_table_xml_root) = @_; my $oWkS = $oBook->{Worksheet}[$iSheet]; ... }
... and so on. This process should be continued until the code becomes manageable, at which point it will be much easier to add new features, etc.
Sorry I can’t provide the solution you wanted; but I hope the above at least gives you some useful pointers.
Update: There are a couple of other issues with the original code:
does not insert a path separator between the folder and the filename. So, unless the script is called like this:my $oBook = $parser->parse($data_folder.$filename);
(note the trailing backslash), the file will not be found and the script will fail silently.>perl script.pl output.xml Excel\
contains a condition which can never be fulfilled, as it requires $active_cell to be both defined yet also decodable to zero length. Fix: remove the ! (negation) operator.if($map_Cmin >= 0 and $map_Cmin >= 0 and $title_row >= 0){ for(my $iR = $title_row +1; defined $oWkS->{MaxRow} && $iR <= $oWk +S->{MaxRow} ; $iR++){ $internal_cell = $oWkS->{Cells}[$iR][$map_Cmin]; $active_cell = $oWkS->{Cells}[$iR][$active_col]; $family_cell = $oWkS->{Cells}[$iR][$family_col]; if(defined $internal_cell and defined $active_cell and defined + $family_cell and length(decode('cp1252',$internal_cell->{Val})) gt 0 + and !(length(decode('cp1252',$active_cell->{Val})) gt 0) and length( +decode('cp1252',$family_cell->{Val})) gt 0)
The input also requires a special format: see not only the post by Anonymous Monk below, but also the comments at the head of the code in the original post by cibien. The following format works when saved as an “Excel 97-2003 Workbook (*.xls)”:
| A | B | C | D | E | ===+========+==========+===========+=========+=========+ 1 | | Configurator mapping | ---+--------+----------+-----------+---------+---------+ 2 | active | internal | pr_family | pr1 | pr2 | ---+--------+----------+-----------+---------+---------+ 3 | yes | item 1 | 1,2 | value 1 | value 2 | ---+--------+----------+-----------+---------+---------+ 4 | yes | item 2 | 1,2 | value 3 | value 3 | ---+--------+----------+-----------+---------+---------+ 5 | yes | item 3 | 1,2 | value 5 | value 4 | ---+--------+----------+-----------+---------+---------+
When this spreadsheet is processed by the script with the fixes applied, it produces the following output:
<?xml version="1.0" encoding="UTF-8"?> <masterdata version="2012-07-27 01:51:57"> <item internal="item 1 " pr_family="1" pr1="value 1" pr2="value 2"/> <item internal="item 1 " pr_family="2" pr1="value 1" pr2="value 2"/> <item internal="item 2 " pr_family="1" pr1="value 3" pr2="value 3"/> <item internal="item 2 " pr_family="2" pr1="value 3" pr2="value 3"/> <item internal="item 3 " pr_family="1" pr1="value 5" pr2="value 4"/> <item internal="item 3 " pr_family="2" pr1="value 5" pr2="value 4"/> </masterdata>
Of course, none of this answers the OP’s question; but it may provide a base upon which an answer can be constructed.
Athanasius <°(((>< contra mundum
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: modify perl 'excel to xml'
by Anonymous Monk on Jul 26, 2012 at 06:34 UTC |