Advertisement
techouse

import.pl

Nov 7th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.65 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. =pot
  4. VERSION 1.1
  5. MODIFIED TO WORK WITH SHITTY WINDOWS
  6.  
  7. REQUIRES:
  8.     Spreadsheet::XLSX
  9.     Text::CSV::Encoded
  10.     Email::Valid
  11.     Time::Piece
  12.  
  13. NOTE:
  14.     Even thouth the output encoding is iso-8859-1 Windows reads it as UTF-8 no BOM; I don't understand the world no more.
  15.  
  16. =cut
  17.  
  18. use warnings;
  19. use strict;
  20. use Cwd qw(abs_path);
  21. use File::Basename qw(dirname);
  22. use feature qw(switch);
  23. use Spreadsheet::XLSX;
  24. use Text::CSV::Encoded;
  25. use Email::Valid;
  26. use Time::Piece;
  27. use Encode qw(decode encode);
  28. use utf8;
  29. binmode(STDOUT, ":utf8");
  30.  
  31. sub trim {
  32.    (my $s = $_[0]) =~ s/^\s+|\s+$//g;
  33.    return $s;
  34. }
  35.  
  36. my $dirname = dirname(abs_path($0));
  37.  
  38. my $excel = Spreadsheet::XLSX->new($dirname . '/import.xlsx');
  39.  
  40. my $csv_file = $dirname . '/import.csv';
  41. open(my $csv_FH, '>:encoding(iso-8859-1)', $csv_file) or die $!;
  42. my $csv = Text::CSV::Encoded->new({
  43.    eol => $/,
  44.    sep_char => ';',
  45.    encoding_out => 'iso-8859-1'
  46. });
  47. my @column_names = qw(email datum_sodelovanja(YYYY-MM-DD) ime_podjetja ime priimek spol1=m,2=z,3=nedefiniran vir_pridobitve datum_rojstva(YYYY-MM-DD) telefon naslov kraj postna_stevilka opombe);
  48. $csv->column_names(@column_names);
  49. $csv->print($csv_FH, [@column_names]);
  50.  
  51. foreach my $sheet (@{$excel->{Worksheet}}) {
  52.    $sheet->{MaxRow} ||= $sheet->{MinRow};
  53.  
  54.    # the rows
  55.    foreach my $row ($sheet->{MinRow} .. $sheet->{MaxRow}) {
  56.     $sheet->{MaxCol} ||= $sheet->{MinCol};
  57.  
  58.     my %row_data;
  59.     my $email = 0;
  60.  
  61.     # the columns of a single row
  62.     foreach my $col ($sheet->{MinCol} ..  $sheet->{MaxCol}) {
  63.         my $cell = $sheet->{Cells}[$row][$col];
  64.  
  65.         # the cell in the column
  66.         if ($cell) {
  67.         given ($col) {
  68.             when (0) { $row_data{'ime'} = ucfirst(decode('iso-8859-1', $cell->{Val})); }
  69.             when (1) { $row_data{'priimek'} = ucfirst(decode('iso-8859-1', $cell->{Val})); }
  70.             when (2) {
  71.             if (Email::Valid->address($cell->{Val})) {
  72.                 $row_data{'email'} = $cell->{Val};
  73.                 $email = 1; # email checker variable
  74.             }
  75.             }
  76.             when (3) {
  77.                        if (trim($cell->{Val}) ne '') {
  78.                            my $post_num = $cell->{Val};
  79.                            my $post_name = $cell->{Val};
  80.                            $post_num =~ s/\D+//;
  81.                            $row_data{'postna_stevilka'} = $post_num;
  82.                            $post_name =~ s/\d+//;
  83.                            $row_data{'kraj'} = ucfirst(trim(decode('iso-8859-1', $post_name)));
  84.                            undef $post_num;
  85.                            undef $post_name;
  86.                        }
  87.             }
  88.             when (4) {
  89.             given ($cell->{Val}) {
  90.                 when ('m') { $row_data{'spol'} = 1; }
  91.                 when ('w') { $row_data{'spol'} = 2; }
  92.                 default { $row_data{'spol'} = 3; }
  93.             }
  94.             }
  95.             when (8) {
  96.                        if (trim($cell->{Val}) ne '') {
  97.                            my $participation_date = Time::Piece->strptime($cell->{Val}, '%Y-%m-%d %H:%M:%S');
  98.                            $row_data{'datum_sodelovanja'} = $participation_date->strftime('%Y-%m-%d');
  99.                            undef $participation_date;
  100.                        }
  101.             }
  102.         }
  103.         }
  104.     }
  105.  
  106.     if ($email != 0) {
  107.         # only write it if the email is valid
  108.         $csv->print($csv_FH, [
  109.         $row_data{'email'},
  110.         $row_data{'datum_sodelovanja'},
  111.         '', # ime podjetja
  112.         $row_data{'ime'},
  113.         $row_data{'priimek'},
  114.         $row_data{'spol'},
  115.         '', # vir vpišemo ročno!
  116.         '', # datum rojstva
  117.         '', # telefon
  118.         '', # naslov
  119.         $row_data{'kraj'},
  120.         $row_data{'postna_stevilka'},
  121.         '' # opombe
  122.         ]);
  123.     }
  124.  
  125.        undef %row_data;
  126.        undef $email;
  127.    }
  128. }
  129.  
  130. close($csv_FH);
  131. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement