summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPaul Kmiec <[email protected]>2023-05-13 11:00:38 -0700
committerPaul Kmiec <[email protected]>2023-05-15 16:58:24 -0700
commit447b2522bc4a06c8436e708eae40cf7244dc0ec4 (patch)
tree764d08ac803bd6c1dc3065823da3f5b0cc39ef96
parentaf8360755fe21f4e9d30e943ba0b2be3c0128d28 (diff)
downloadcaxlsx-447b2522bc4a06c8436e708eae40cf7244dc0ec4.tar.gz
caxlsx-447b2522bc4a06c8436e708eae40cf7244dc0ec4.zip
Only define @escape_formulas in cell if it is different from worksheet
The benchmarks showed that validate_boolean is called 200_005 times and almost all of those are to validate escape_formulas passed into cell. In this commit the worksheet does not pass in its escape_formulas value, avoiding validate_boolean, and instead the cell asks the worksheet for value when needed. Now validate_boolean is called 5 times in benchmarks.
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb6
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb1
2 files changed, 4 insertions, 3 deletions
diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb
index 8afacee1..b44656e3 100644
--- a/lib/axlsx/workbook/worksheet/cell.rb
+++ b/lib/axlsx/workbook/worksheet/cell.rb
@@ -46,7 +46,7 @@ module Axlsx
val = options.delete(:formula_value)
self.formula_value = val unless val.nil?
val = options.delete(:escape_formulas)
- self.escape_formulas = val.nil? ? row.worksheet.escape_formulas : val
+ self.escape_formulas = val unless val.nil?
parse_options(options)
@@ -146,7 +146,9 @@ module Axlsx
# Allowing user-generated data to be interpreted as formulas is a security risk.
# See https://www.owasp.org/index.php/CSV_Injection for details.
# @return [Boolean]
- attr_reader :escape_formulas
+ def escape_formulas
+ defined?(@escape_formulas) ? @escape_formulas : row.worksheet.escape_formulas
+ end
# Sets whether to treat values starting with an equals sign as formulas or as literal strings.
# @param [Boolean] value The value to set.
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 960f33b3..21c53409 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -429,7 +429,6 @@ module Axlsx
# Allowing user generated data to be interpreted as formulas can be dangerous
# (see https://www.owasp.org/index.php/CSV_Injection for details).
def add_row(values = [], options = {})
- options[:escape_formulas] = escape_formulas if options[:escape_formulas].nil?
row = Row.new(self, values, options)
update_column_info row, options.delete(:widths)
yield row if block_given?