Spreadsheet::Read - Read the data from a spreadsheet
use Spreadsheet::Read; my $ref = ReadData ("test.csv", sep => ";"); my $ref = ReadData ("test.sxc"); my $ref = ReadData ("test.ods"); my $ref = ReadData ("test.xls"); my $a3 = $ref->[1]{A3}, "\n"; # content of field A3 of sheet 1
Spreadsheet::Read tries to transparantly read *any* spreadsheet and return its content in a universal manner independent of the parsing module that does the actual spreadsheet scanning.
For OpenOffice this module uses Spreadsheet::ReadSXC
For Excel this module uses Spreadsheet::ParseExcel
For CSV this module uses Text::CSV_XS
For SquirrelCalc there is a very simplistic built-in parser
The data is returned as an array reference:
$ref = [ # Entry 0 is the overall control hash { sheets => 2, sheet => { "Sheet 1" => 1, "Sheet 2" => 2, }, type => "xls", version => 0.26, }, # Entry 1 is the first sheet { label => "Sheet 1", maxrow => 2, maxcol => 4, cell => [ undef, [ undef, 1 ], [ undef, undef, undef, undef, undef, "Nugget" ], ], A1 => 1, B4 => "Nugget", }, # Entry 2 is the second sheet { label => "Sheet 2", : :
To keep as close contact to spreadsheet users, row and column 1 have index 1 too in the cell element of the sheet hash, so cell "A1" is the same as cell [1, 1] (column first). To switch between the two, there are two helper functions available: cell2cr () and cr2cell ().
cell
cell2cr ()
cr2cell ()
The cell hash entry contains unformatted data, while the hash entries with the traditional labels contain the formatted values (if applicable).
The control hash (the first entry in the returned array ref), contains some spreadsheet metadata. The entry sheet is there to be able to find the sheets when accessing them by name:
sheet
my %sheet2 = %{$ref->[$ref->[0]{sheet}{"Sheet 2"}]};
my $ref = ReadData ($source [, option =
my $ref = ReadData ("file.csv", sep => ',', quote =
my $ref = ReadData ("file.xls");
my $ref = ReadData ("file.ods");
my $ref = ReadData ("file.sxc");
my $ref = ReadData ("content.xml");
my $ref = ReadData ($content);
Tries to convert the given file, string, or stream to the data structure described above.
Precessing data from a stream or content is supported for Excel (through a File::Temp temporary file), or for XML (OpenOffice), but not for CSV.
ReadSXC does preserve sheet order as of version 0.20.
Currently supported options are:
Control the generation of named cells ("A1" etc). Default is true.
Control the generation of the {cell}[c][r] entries. Default is true.
Control the generation of the {attr}[c][r] entries. Default is false.
If set, ReadData () will remove all trailing lines and columns per sheet that have no visual data. This option is only valid if cells is true. The default value is true if cells is true, and false otherwise.
ReadData ()
cells
Set separator for CSV. Default is comma ,.
,
Set quote character for CSV. Default is ".
"
Enable some diagnostic messages to STDERR.
The value determines how much diagnostics are dumped (using Data::Dumper). A value of 9 and higher will dump the entire structure from the backend parser.
my $cell = cr2cell (col, row)
cr2cell () converts a (column, row) pair (1 based) to the traditional cell notation:
(column, row)
my $cell = cr2cell ( 4, 14); # $cell now "D14" my $cell = cr2cell (28, 4); # $cell now "AB4"
my ($col, $row) = cell2cr ($cell)
cell2cr () converts traditional cell notation to a (column, row) pair (1 based):
my ($col, $row) = cell2cr ("D14"); # returns ( 4, 14) my ($col, $row) = cell2cr ("AB4"); # returns (28, 4)
my @rows = Spreadsheet::Read::rows ($ss->[1])
Convert {cell}'s [column][row] to a [row][column] list.
{cell}
[column][row]
[row][column]
Note that the indexes in the returned list are 0-based, where the index in the {cell} entry is 1-based.
rows () is not imported by default, so either specify it in the use argument list, or call it fully qualified.
rows ()
Spreadsheet::Read::parses ("CSV")
parses () returns Spreadsheet::Read's capability to parse the required format.
parses ()
parses () is not imported by default, so either specify it in the use argument list, or call it fully qualified.
my $v = Spreadsheet::Read::Version ()
Returns the current version of Spreadsheet::Read.
Version () is not imported by default, so either specify it in the use argument list, or call it fully qualified.
Version ()
Future plans include cell attributes, available as for example:
{ label => "Sheet 1", maxrow => 2, maxcol => 4, cell => [ undef, [ undef, 1 ], [ undef, undef, undef, undef, undef, "Nugget" ], ], attr => [ undef, [ undef, { type => "numeric", color => "Red", font => "Arial", enc => "iso10646-1", size => "12", format => "## ###.##", halign => "right", }, ] [ undef, undef, undef, undef, undef, { type => "text", color => "#e2e2e2", font => "LetterGothic", enc => "iso8859-1", size => "15", format => undef, halign => "left", }, ] A1 => 1, B4 => "Nugget", },
New Spreadsheet::Read options are bound to happen. I'm thinking of an option that disables the reading of the data entirely to speed up an index request (how many sheets/fields/columns). See xlscat -i.
xlscat -i
Try to transparently support as many options as the encapsulated modules support regarding (un)formatted values, (date) formats, hidden columns rows or fields etc. These could be implemented like attr above but names meta, or just be new values in the attr hashes.
attr
meta
I consider adding any spreadsheet interface that offers a usable API.
Now that the different parsers are only activated if the module can be loaded, we need more flexibility is switching from Text::CSV_XS to Text::CSV in the parser part.
Consider making the ref an object, though I currently don't see the big advantage (yet). Maybe I'll make it so that it is a hybrid functional / OO interface.
http://search.cpan.org/~jwied/
A pure perl version is available on http://search.cpan.org/~makamaka/
http://search.cpan.org/~kwitknr/
http://search.cpan.org/~terhechte/
http://search.cpan.org/~jwied/ http://search.cpan.org/~alancitt/
http://search.cpan.org/~gng/ for xlscat likewise functionality (Excel only)
http://search.cpan.org/~nkh/ for an alternative set of cell2cr () / cr2cell () pair
http://search.cpan.org/~nkh/ offers a Pure Perl implementation of a spreadsheet engine. Users that want this format to be supported in Spreadsheet::Read are hereby motivated to offer patches. It's not high on my todo-list.
http://search.cpan.org/~ken/ offers an alternative for my xlscat -c, in the xls2csv tool, but this tool focusses on character encoding transparency, and requires some other modules.
xlscat -c
H.Merijn Brand, <h.m.brand@xs4all.nl>
Copyright (C) 2005-2006 H.Merijn Brand
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Spreadsheet::Read, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Spreadsheet::Read
CPAN shell
perl -MCPAN -e shell install Spreadsheet::Read
For more information on module installation, please visit the detailed CPAN module installation guide.