Bug 19716: Add option to send header line for CSV output
authorKyle M Hall <kyle@bywatersolutions.com>
Wed, 29 Nov 2017 20:20:19 +0000 (15:20 -0500)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 23 Mar 2018 14:45:38 +0000 (11:45 -0300)
Currently, if outputting a CSV file using runreport.pl, you need to look at the report used to know what each column means. It would be nice if we could include column headers.

Test Plan:
1) Apply this patch
2) Try using runreport.pl with --format csv --csv-header

Signed-off-by: David Bourgault <david.bourgault@inlibro.com>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
misc/cronjobs/runreport.pl

index 1648f4d..cb10125 100755 (executable)
@@ -66,6 +66,7 @@ runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
    --from=s        e-mail address to send report from
    --subject=s     subject for the e-mail
    --store-results store the result of the report
+   --csv-header    add column names as first line of csv output
 
 
  Arguments:
@@ -175,6 +176,7 @@ my $subject = "";
 my $separator = ',';
 my $quote = '"';
 my $store_results = 0;
+my $csv_header = 0;
 
 my $username = undef;
 my $password = undef;
@@ -194,6 +196,7 @@ GetOptions(
     'password:s'        => \$password,
     'method:s'          => \$method,
     'store-results'     => \$store_results,
+    'csv-header'        => \$csv_header,
 
 ) or pod2usage(2);
 pod2usage( -verbose => 2 ) if ($man);
@@ -273,6 +276,14 @@ foreach my $report_id (@ARGV) {
             quote_char  => $quote,
             sep_char    => $separator,
             });
+
+        if ( $csv_header ) {
+            my $fields = $sth->{NAME};
+            $csv->combine( @$fields );
+            $message .= $csv->string() . "\n";
+            push @rows_to_store, [@$fields] if $store_results;
+        }
+
         while (my $line = $sth->fetchrow_arrayref) {
             $csv->combine(@$line);
             $message .= $csv->string() . "\n";