Skip to content

Commit

Permalink
utility: fix quote_sheetname() edge cases
Browse files Browse the repository at this point in the history
Closes #308
  • Loading branch information
jmcnamara committed Dec 28, 2024
1 parent f6de04a commit c16a14a
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions lib/Excel/Writer/XLSX/Utility.pm
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ sub quote_sheetname {
$needs_quoting = 1;
}

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

Expand All @@ -255,60 +256,50 @@ sub quote_sheetname {
}

# Rule 4. Sheet names must not *start* with a valid RC style cell
# reference. Valid means that the row and column values are within
# Excel limits.

# Rule 4a. Check for some single R/C references.
elsif ($uc_sheetname eq "R"
|| $uc_sheetname eq "C"
|| $uc_sheetname eq "RC" )
{
$needs_quoting = 1;

# reference. Other characters after the valid RC reference are ignored
# by Excel. Valid means that the row and column range values must also
# be within Excel row and column limits.
#
# Note: references without trailing characters like R12345 or C12345 are
# caught by Rule 3. Negative references like R-12345 are caught by Rule
# 1 due to the dash.

# Rule 4a. Check for sheet names that start with R1 style references.
elsif ( $uc_sheetname =~ /^R(\d+)/ ) {
my $row = $1;
if ( $row > 0 && $row <= $row_max ) {
$needs_quoting = 1;
}
}

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

# Rule 4c. Check for R1C1 style references where both the number
# ranges are optional. Note that only 1 of the number ranges is
# required to be valid.
elsif ( $uc_sheetname =~ /^R(\d+)?C(\d+)?/ ) {
if ( defined $1 ) {
my $row = $1;
if ( $row > 0 && $row <= $row_max ) {
$needs_quoting = 1;
}
}

if ( defined $2 ) {
my $col = $1;
if ( $col > 0 && $col <= $col_max ) {
$needs_quoting = 1;
}
}
# Rule 4c. Check for some single R/C references.
elsif ($uc_sheetname eq "R"
|| $uc_sheetname eq "C"
|| $uc_sheetname eq "RC" )
{
$needs_quoting = 1;
}
}


if ( $needs_quoting ) {

# Double quote any single quotes.
$sheetname =~ s/'/''/g;
$sheetname = q(') . $sheetname . q(');
}


return $sheetname;
}



###############################################################################
#
# xl_inc_row($string)
Expand Down

0 comments on commit c16a14a

Please sign in to comment.