source: trunk/LATMOS-Accounts/bin/la2xls @ 1806

Last change on this file since 1806 was 1653, checked in by nanardon, 8 years ago

add la2xls: generate xls file

File size: 2.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use Spreadsheet::WriteExcel;
9
10=head1 NAME
11
12    la2xls - Output data in XLS format
13
14=head1 SYNOPSIS
15
16    la-search [options] filter [filter2 [...]] [attributes [...]]
17
18=head1 OPTIONS
19
20=over 4
21
22=item --config configdir
23
24Use this configuration directory instead default
25
26=item --base basename
27
28Perform search in this specific base instead default
29
30=item --with-unexp
31
32Take into account all objects (even non propagated ones, with attribute
33'exported'=0) (default)
34
35=item --wo-unexp
36
37Take into account only propagated objects (attribute 'exported'=1)
38
39=item -f FILE
40
41The output filename. The default is la2xls.xls.
42
43=item FILTER
44
45Filter are in form "[ATTRIBUTE][OPERATOR][VALUE]"
46
47where attribute is a supported attribute.
48
49OPERATOR can be:
50
51=over 4
52
53=item C<=>
54
55Search object having attribute strictely equal to VALUE. If VALUE is C<*> match
56any non empty value.
57
58=item C<~>
59
60Search object having attribute containing VALUE.
61
62=back
63
64=back
65
66=cut
67
68GetOptions(
69    'c|config=s'        => \my $config,
70    'b|base=s'          => \my $base,
71    'o|object=s'        => \my $otype,
72    'no-unexp|wo-unexp' => \my $nounexp,
73    'with-unexp'        => \my $unexp,
74    'f|file=s'          => \my $file,
75    'help'              => sub { pod2usage(0) },
76) or pod2usage();
77
78if (!$ARGV[0]) {warn "You must specify 'filter', aborting\n"; pod2usage(); }
79
80$otype ||= 'user';
81$file  ||= 'la2xls.xls';
82
83my $LA = LATMOS::Accounts->new($config, noacl => 1);
84my $labase = $LA->base($base);
85$labase && $labase->load or die "Cannot load base";
86
87$labase->unexported($nounexp ? 0 : 1);
88
89my (@filters, @attr);
90
91foreach (@ARGV) {
92    if (/(~|=)/) {
93        push(@filters, $_);
94    } else {
95        push(@attr, $_);
96    }
97}
98
99my @result = $labase->search_objects($otype, @filters);
100
101my @objs = map { $labase->get_object($otype, $_) } @result;
102
103# Create a new Excel workbook
104my $workbook = Spreadsheet::WriteExcel->new($file);
105
106# Add a worksheet
107my $worksheet = $workbook->add_worksheet();
108
109#  Add and define a format
110my $format = $workbook->add_format(); # Add a format
111$format->set_bold();
112$format->set_align('center');
113
114$worksheet->write(0, 0, 'name', $format);
115my $row = 0;
116foreach my $obj (@objs) {
117    $row++;
118    $worksheet->write($row, 0, $obj->id);
119}
120
121
122
123foreach my $col (0 .. $#attr) {
124    $worksheet->write(0, $col+1, $attr[$col], $format);
125
126    my $row = 0;
127    foreach my $obj (@objs) {
128        $row++;
129        $worksheet->write($row, $col+1, $obj->get_attributes($attr[$col]));
130    }
131}
132
133$workbook->close();
Note: See TracBrowser for help on using the repository browser.