Last active
December 26, 2015 10:59
-
-
Save briandfoy/7140446 to your computer and use it in GitHub Desktop.
A demonstration of Excel::Writer::XLSX applied to an exercise from Learning Perl.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use v5.10; | |
use strict; | |
use warnings; | |
die "Directory does not exist!" unless -d $ARGV[0]; | |
my @files = glob( $ARGV[0] ); | |
# make the name column at least as wide as the longest filename | |
my( $workbook, $worksheet ) = get_book_and_sheet( 'attributes.xlsx', 'attributes' ); | |
make_header_row( 'Name', 'Exists', 'Readable', 'Writeable', 'Executable' ); | |
my $max_length = 0; | |
foreach my $file ( @ARGV ) { | |
make_row( $file ); | |
$max_length = length $file if length $file > $max_length; | |
} | |
# make the name column at least as wide as the longest filename | |
$worksheet->set_column( 'A:A', $max_length ); | |
# write everything to the file | |
$workbook->close; | |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
sub make_header_row { | |
my( @headings ) = @_; | |
my( $workbook, $worksheet ) = get_book_and_sheet(); | |
my $row = 0; | |
my $column = 0; | |
my $format = header_format(); | |
$worksheet->write( $row, $column++, $_, $format ) | |
foreach @headings; | |
} | |
sub make_row { | |
local( $_ ) = @_; | |
my( $workbook, $worksheet ) = get_book_and_sheet(); | |
state $row = 1; | |
my $column = 0; | |
my @attributes = ( -e, -r, -w, -x ); | |
$worksheet->write( $row, $column++, $_ ); # filename | |
foreach my $attribute ( @attributes ) { | |
$worksheet->write( $row, $column++, $attribute ? 'x' : '' ); | |
} | |
$row++; | |
} | |
sub header_format { | |
my( $workbook, $worksheet ) = get_book_and_sheet(); | |
my $format = $workbook->add_format(); | |
$format->set_bold(); | |
$format->set_bg_color('yellow'); | |
$format->set_align('center'); | |
$format; | |
} | |
sub get_book_and_sheet { | |
my( $file, $label ) = @_; # only first run! | |
require Excel::Writer::XLSX; | |
state $workbook = Excel::Writer::XLSX->new( $file ); | |
state $worksheet = $workbook->add_worksheet( $label ); | |
( $workbook, $worksheet ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Switch to Excel::Writer::XLSX