Skip to content

Commit c16a14a

Browse files
committed
utility: fix quote_sheetname() edge cases
Closes #308
1 parent f6de04a commit c16a14a

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

lib/Excel/Writer/XLSX/Utility.pm

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ sub quote_sheetname {
243243
$needs_quoting = 1;
244244
}
245245

246-
# Rule 3. Sheet names must not be a valid A1 style cell reference.
247-
# Valid means that the row and column values are within Excel limits.
246+
# Rule 3. Sheet names must not be a valid A1 style cell reference. Valid
247+
# means that the row and column range values must also be within Excel
248+
# row and column limits.
248249
elsif ( $uc_sheetname =~ /^([A-Z]{1,3}\d+)$/ ) {
249250
my ( $row, $col ) = xl_cell_to_rowcol( $1 );
250251

@@ -255,60 +256,50 @@ sub quote_sheetname {
255256
}
256257

257258
# Rule 4. Sheet names must not *start* with a valid RC style cell
258-
# reference. Valid means that the row and column values are within
259-
# Excel limits.
260-
261-
# Rule 4a. Check for some single R/C references.
262-
elsif ($uc_sheetname eq "R"
263-
|| $uc_sheetname eq "C"
264-
|| $uc_sheetname eq "RC" )
265-
{
266-
$needs_quoting = 1;
267-
259+
# reference. Other characters after the valid RC reference are ignored
260+
# by Excel. Valid means that the row and column range values must also
261+
# be within Excel row and column limits.
262+
#
263+
# Note: references without trailing characters like R12345 or C12345 are
264+
# caught by Rule 3. Negative references like R-12345 are caught by Rule
265+
# 1 due to the dash.
266+
267+
# Rule 4a. Check for sheet names that start with R1 style references.
268+
elsif ( $uc_sheetname =~ /^R(\d+)/ ) {
269+
my $row = $1;
270+
if ( $row > 0 && $row <= $row_max ) {
271+
$needs_quoting = 1;
272+
}
268273
}
269274

270-
# Rule 4b. Check for C1 or RC1 style references. References without
271-
# trailing characters (like C12345) are caught by Rule 3.
275+
# Rule 4b. Check for sheet names that start with C1 or RC1 style
272276
elsif ( $uc_sheetname =~ /^R?C(\d+)/ ) {
273277
my $col = $1;
274278
if ( $col > 0 && $col <= $col_max ) {
275279
$needs_quoting = 1;
276280
}
277281
}
278282

279-
# Rule 4c. Check for R1C1 style references where both the number
280-
# ranges are optional. Note that only 1 of the number ranges is
281-
# required to be valid.
282-
elsif ( $uc_sheetname =~ /^R(\d+)?C(\d+)?/ ) {
283-
if ( defined $1 ) {
284-
my $row = $1;
285-
if ( $row > 0 && $row <= $row_max ) {
286-
$needs_quoting = 1;
287-
}
288-
}
289-
290-
if ( defined $2 ) {
291-
my $col = $1;
292-
if ( $col > 0 && $col <= $col_max ) {
293-
$needs_quoting = 1;
294-
}
295-
}
283+
# Rule 4c. Check for some single R/C references.
284+
elsif ($uc_sheetname eq "R"
285+
|| $uc_sheetname eq "C"
286+
|| $uc_sheetname eq "RC" )
287+
{
288+
$needs_quoting = 1;
296289
}
297290
}
298291

299-
300292
if ( $needs_quoting ) {
293+
301294
# Double quote any single quotes.
302295
$sheetname =~ s/'/''/g;
303296
$sheetname = q(') . $sheetname . q(');
304297
}
305298

306-
307299
return $sheetname;
308300
}
309301

310302

311-
312303
###############################################################################
313304
#
314305
# xl_inc_row($string)

0 commit comments

Comments
 (0)