Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Serious error: Row and column labeling is opposite #555. #565

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions examples/read_excel.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,46 @@

use Spreadsheet::Read qw(ReadData);

# May require installing Spreadsheet::XLSX.
my $book = ReadData('simple.xlsx');

say 'A1: ' . $book->[1]{A1};

my @row = Spreadsheet::Read::row($book->[1], 1);
say "";
say "row 1:";
for my $i (0 .. $#row) {
say 'A' . ($i+1) . ' ' . ($row[$i] // '');
say $row[$i] // '';
}

my @rows = Spreadsheet::Read::rows($book->[1]);
foreach my $i (1 .. scalar @rows) {
foreach my $j (1 .. scalar @{$rows[$i-1]}) {
say chr(64+$i) . " $j " . ($rows[$i-1][$j-1] // '');
say "";
foreach my $i (0 .. $#rows) {
foreach my $j (0 .. $#{$rows[$i]}) {
say chr(ord('A')+$j) . ($i+1) . ": " . ($rows[$i][$j] // '');
}
}

# Or just dump the data:
use Data::Dumper;
say "";
say Dumper \@rows;

# Output:
# $VAR1 = [
# [
# 'A1',
# 'B1',
# 'C1'
# ],
# [
# 'A2',
# 'B2',
# 'C2'
# ],
# [
# 'A3',
# 'B3',
# 'C3'
# ]
# ];
Binary file added examples/simple.xlsx
Binary file not shown.
57 changes: 17 additions & 40 deletions sites/en/pages/read-an-excel-file-in-perl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ or even a plain CSV file. It will use the file-extension to guess which format t
load the appropriate back-end module and use that module to load and parse the file. In the end it will create an array reference representing the whole file:

<code lang="perl">
# May require installing Spreadsheet::XLSX.
use Spreadsheet::Read qw(ReadData);
my $book = ReadData ('simple.xlsx');
</code>
Expand Down Expand Up @@ -102,16 +103,14 @@ and then it will loop over the indexes and display the content of each field. (d
<code lang="perl">
my @row = Spreadsheet::Read::row($book->[1], 1);
for my $i (0 .. $#row) {
say 'A' . ($i+1) . ' ' . ($row[$i] // '');
say $row[$i] // '';
}
</code>

<code>
A1 Hi Excel!
A2
A3
A4
A5 10
A1
B1
C1
</code>


Expand All @@ -125,47 +124,25 @@ represents one row in the spreadsheet.
This is how we iterate over all the elements:

<code lang="perl">
my @rows = Spreadsheet::Read::rows($book->[1]);
foreach my $i (1 .. scalar @rows) {
foreach my $j (1 .. scalar @{$rows[$i-1]}) {
say chr(64+$i) . " $j " . ($rows[$i-1][$j-1] // '');
foreach my $i (0 .. $#rows) {
foreach my $j (0 .. $#{$rows[$i]}) {
say chr(ord('A')+$j) . ($i+1) . ": " . ($rows[$i][$j] // '');
}
}
</code>

The result is

<code>
A 1 Hi Excel!
A 2
A 3
A 4
A 5 10
B 1 second row
B 2
B 3
B 4
B 5 11
C 1 1
C 2 2
C 3 3
C 4
C 5 12
D 1
D 2
D 3
D 4
D 5
E 1 4
E 2 6
E 3
E 4
E 5
F 1 5
F 2 7
F 3
F 4
F 5
A1: A1
B1: B1
C1: C1
A2: A2
B2: B2
C2: C2
A3: A3
B3: B3
C3: C3
</code>

With this we can already work quite well.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ say index $str, "The"; # 0
say index $str, "the"; # 26
</code>

Первый вызов <hl>index</hl> вернул 10, потому что строка "cat" начинается на 10 символе. Второй
Первый вызов <hl>index</hl> вернул 10, потому что строка "cat" начинается на индекс 10. Второй
вызов <hl>index</hl> вернул -1, указывая на то, что в этом предложении нет "dog".

Третий вызов показывает, что <hl>index</hl> возвращает 0, когда вторая строка стоит в начале первой.
Expand Down