-
Notifications
You must be signed in to change notification settings - Fork 0
/
XlsDataWriter.pm
123 lines (98 loc) · 4 KB
/
XlsDataWriter.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package XlsDataWriter;
use Carp;
use strict;
use YAML;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Utility;
use Excel::Writer::XLSX;
BEGIN {
our ($VERSION, @ISA);
$VERSION = 0.02;
}
sub new {
my $class = shift;
my $config_input = shift; #If hash ref, uses the hash. Otherwise, it assumes it is a filename
my $additional_arguments_ref = shift;
my %additional_arguments;
if( ref $additional_arguments_ref eq 'HASH' ){
%additional_arguments = %{ $additional_arguments_ref };
}
my %config_hash;
if( ref $config_input eq "HASH" ){
%config_hash = %{ $config_input };
}else{
open my $config_file, "< $config_input" or croak "Can't open configuration infile $!";
my $yaml_stream = do { local $/; <$config_file> };
%config_hash = %{Load($yaml_stream)};
}
my $self = { 'config_hash' => \%config_hash };
bless $self, $class;
}
sub write_data_to_xls {
my $self = shift;
my %data_hash = %{ shift @_ };
for my $current_file ( @{ $self->{'config_hash'}->{'files_to_modify'} } ){
$current_file->{'filename'} || croak "No filename specified";
#Delete old file if it exists
if( -e $current_file->{'filename'} ){
unlink $current_file->{'filename'};
}
my $workbook;
if( $current_file->{'filename'} =~ /xlsx$/ ){
$workbook = Excel::Writer::XLSX->new( $current_file->{'filename'} ) or croak "Unable to write excel file $current_file->{'filename'}";
}else{
$workbook = Spreadsheet::WriteExcel->new( $current_file->{'filename'} ) or croak "Unable to write excel file $current_file->{'filename'}";
}
for my $current_worksheet_info ( @{ $current_file->{'worksheets_to_modify'} } ){
$current_worksheet_info->{'worksheet_name'} || croak "No worksheet specified in one worksheet for $current_file->{'filename'}";
my $worksheet = $workbook->add_worksheet( $current_worksheet_info->{'worksheet_name'} );
for my $current_range_info ( @{ $current_worksheet_info->{'ranges_to_modify'} } ){
#Check that required values are available
$current_range_info->{'start_cell'} || croak "Start cell missing in one range for $current_worksheet_info->{'worksheet_name'}";
$current_range_info->{'data_list'} || croak "Data list missing in one range for $current_worksheet_info->{'worksheet_name'}";
ref $data_hash{ $current_range_info->{'data_list'} } eq 'ARRAY' || croak "Data hash does not contain an array of values for $current_range_info->{'data_list'} on worksheet $current_worksheet_info->{'worksheet_name'}";
my ($start_column, $start_row ) = &xl_cell_to_rowcol( $current_range_info->{'start_cell'} );
my @data_to_print = @{$data_hash{ $current_range_info->{'data_list'} }};
my $transpose_row_column;
if( exists $current_range_info->{'transpose_row_column'} ){
$transpose_row_column = 1;
}
for (my $i = 0; $i <= $#data_to_print; $i++ ){
#Use multiple columns if they exist
my @second_data_array;
if( ref $data_to_print[ $i ] eq 'ARRAY' ){
@second_data_array = @{ $data_to_print[ $i ] };
}else{
@second_data_array = ( $data_to_print[ $i ] );
}
for ( my $j; $j <= $#second_data_array; $j++){
my $current_row;
my $current_column;
if( $transpose_row_column ){
$current_row = $start_row + $j;
$current_column = $start_column + $i;
my $data_to_print = $second_data_array[ $j ];
if( $data_to_print =~ /^=/ ){
$data_to_print = "\'" . $data_to_print;
}
$worksheet->write( $current_column, $current_row, $data_to_print );
}else{
$current_row = $start_row + $i;
$current_column = $start_column + $j;
if( $current_row >= 0 && $current_column >= 0 ){
my $data_to_print = $second_data_array[ $j ];
if( $data_to_print =~ /^=/ ){
$data_to_print = "\'" . $data_to_print;
}
$worksheet->write( $current_column, $current_row, $data_to_print );
}
}
}
}
}
}
print "Exported $current_file->{'filename'}\n";
$workbook->close();
}
}
1;