Skip to content

Commit

Permalink
Do not use string eval for conditional formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ruoso authored and jmcnamara committed Dec 29, 2023
1 parent 19ce0f7 commit bd31592
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/Spreadsheet/ParseExcel/Utility.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ sub ExcelFmt {
$format_str = '@' if uc($format_str) eq "GENERAL";

# Check for a conditional at the start of the format. See notes above.
my $conditional;
if ( $format_str =~ /^\[([<>=][^\]]+)\](.*)$/ ) {
$conditional = $1;
$format_str = $2;
my $conditional_op;
my $conditional_value;
if ( $format_str =~ /^\[([<>=]+)([^\]]+)\](.*)$/ ) {
$conditional_op = $1;
$conditional_value = $2;
$format_str = $3;
}

# Ignore the underscore token which is used to indicate a padding space.
Expand Down Expand Up @@ -166,12 +168,23 @@ sub ExcelFmt {
}

# Override the previous choice if the format is conditional.
if ($conditional) {

# TODO. Replace string eval with a function.
$section = eval "$number $conditional" ? 0 : 1;
if ($conditional_op) {
if ($conditional_op eq '>') {
$section = $number > $conditional_value ? 0 : 1;
} elsif ($conditional_op eq '>=') {
$section = $number >= $conditional_value ? 0 : 1;
} elsif ($conditional_op eq '<') {
$section = $number < $conditional_value ? 0 : 1;
} elsif ($conditional_op eq '<=') {
$section = $number <= $conditional_value ? 0 : 1;
} elsif ($conditional_op eq '=') {
$section = $number == $conditional_value ? 0 : 1;
} elsif ($conditional_op eq '==') {
$section = $number == $conditional_value ? 0 : 1;
} elsif ($conditional_op eq '<>') {
$section = $number != $conditional_value ? 0 : 1;
}
}

# We now have the required format.
$format = $formats[$section];

Expand Down

0 comments on commit bd31592

Please sign in to comment.