From 447b2522bc4a06c8436e708eae40cf7244dc0ec4 Mon Sep 17 00:00:00 2001 From: Paul Kmiec Date: Sat, 13 May 2023 11:00:38 -0700 Subject: 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. --- lib/axlsx/workbook/worksheet/cell.rb | 6 ++++-- lib/axlsx/workbook/worksheet/worksheet.rb | 1 - 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? -- cgit v1.2.3