|
|
(5 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
− | #!/usr/bin/perl
| + | {{lowercase}}{{svn|tools|osv2wiki.pl}} |
− | # Imports rows from an OpenOffice spreadsheet file (*.ods) into wiki record articles{{perl}}
| |
− | # - processes only the first sheet
| |
− | # - ignores empty rows
| |
− | use strict;
| |
− | require('xml.pl');
| |
| | | |
− | # Column names and regex to determine rows imported (case insensitive)
| + | '''ods2wiki''' is a way to import data from a Open Office spreadsheet files into a MediaWiki. |
− | my %filter = (
| |
− | 'type' => 'hours'
| |
− | );
| |
| | | |
− | # Names of columns to include from source and their target names
| + | [[Category:Tools]][[Category:PERL]] |
− | my @mapcols = (
| |
− | );
| |
− | | |
− | # Additional columns to add and a default value
| |
− | my @addcols = (
| |
− | );
| |
− | | |
− | # Extract the content.xml file from the .ods archive
| |
− | my $xml = $ARGV[0] or die "Please specify a .ods file to import rows from";
| |
− | | |
− | # Convert the XML text to a hash tree (see http://www.organicdesign.co.nz/xml.pl)
| |
− | $xml = xmlParse(join('', qx( unzip -p $xml content.xml )));
| |
− | | |
− | # Process the rows of the first sheet
| |
− | my %cols = ();
| |
− | my $first = 1;
| |
− | for (@{$$xml{-content}}) {
| |
− | for (@{$$_{-content}}) {
| |
− | if ($$_{-name} eq 'office:body') {
| |
− | for (@{$$_{-content}}) {
| |
− | my $done = 0;
| |
− | for (@{$$_{-content}}) {
| |
− |
| |
− | # Only process rows for the first sheet
| |
− | if ($$_{-name} eq 'table:table' && $done == 0) {
| |
− | $done++;
| |
− |
| |
− | # Loop through the rows of this sheet
| |
− | for (@{$$_{-content}}) {
| |
− | my @row = ();
| |
− | for (@{$$_{-content}}) {
| |
− |
| |
− | # Add this cell's content to the row
| |
− | my $cell = '';
| |
− | $cell = $$_{-content}[0] for @{$$_{-content}};
| |
− | push @row, $cell;
| |
− |
| |
− | # Handle the table:number-columns-repeated attribute
| |
− | if (defined $$_{'table:number-columns-repeated'} && $$_{'table:number-columns-repeated'} < 100) {
| |
− | push @row, $cell while --$$_{'table:number-columns-repeated'};
| |
− | }
| |
− | }
| |
− |
| |
− | # Process this row (unless empty)
| |
− | if (join('', @row)) {
| |
− |
| |
− | # Process this data row (or define cols if first row)
| |
− | if ($first) {
| |
− | for (0..$#row) { $cols{lc $row[$_]} = $_ }
| |
− | $first = 0;
| |
− | } else {
| |
− |
| |
− | # Check if it passes through the filter
| |
− | my $fail = 0;
| |
− | while (my ($col, $pat) = each(%filter)) {
| |
− | $fail = 1 unless $row[$cols{lc $col}] =~ /$pat/i;
| |
− | }
| |
− |
| |
− | # Process this row if it passed
| |
− | unless ($fail) {
| |
− |
| |
− | print join("\t", @row)."\n";
| |
− |
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |