diff options
| author | Zsolt Kozaroczy <[email protected]> | 2023-05-17 09:52:58 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-17 09:52:58 +0200 |
| commit | df919fffb9ccef3eb343f7aaf70206da8f3c8073 (patch) | |
| tree | c1c264d6e8f16904eaf6d7343faffb1ce24aafc5 /lib/axlsx | |
| parent | 6eb2fc56d3ab658edc1477d138b1cf0b3021ab29 (diff) | |
| parent | 72d6411b9365df0bccbe1ac43f793fc7d7ab369f (diff) | |
| download | caxlsx-df919fffb9ccef3eb343f7aaf70206da8f3c8073.tar.gz caxlsx-df919fffb9ccef3eb343f7aaf70206da8f3c8073.zip | |
Merge branch 'master' into chore/use-delete-prefix-suffix
Diffstat (limited to 'lib/axlsx')
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/util/constants.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/util/validators.rb | 4 | ||||
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 4 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell.rb | 29 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell_serializer.rb | 4 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/row.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 1 |
8 files changed, 25 insertions, 23 deletions
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index 77a051c3..fa08bd83 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -239,7 +239,7 @@ module Axlsx def swap_anchor(new_anchor) new_anchor.drawing.anchors.delete(new_anchor) @anchor.drawing.anchors[@anchor.drawing.anchors.index(@anchor)] = new_anchor - new_anchor.instance_variable_set "@object", @anchor.object + new_anchor.instance_variable_set :@object, @anchor.object @anchor = new_anchor end end diff --git a/lib/axlsx/util/constants.rb b/lib/axlsx/util/constants.rb index 42a7683c..5ef3ece1 100644 --- a/lib/axlsx/util/constants.rb +++ b/lib/axlsx/util/constants.rb @@ -423,4 +423,6 @@ module Axlsx # Trailing character that indicates an array formula. ARRAY_FORMULA_SUFFIX = '}' + + BOOLEAN_VALUES = [true, false].freeze end diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb index 0004f51a..e9cf13b6 100644 --- a/lib/axlsx/util/validators.rb +++ b/lib/axlsx/util/validators.rb @@ -106,8 +106,8 @@ module Axlsx DataTypeValidator.validate :signed_int, Integer, v end - VALID_BOOLEAN_CLASSES = [String, Integer, Symbol, TrueClass, FalseClass].freeze - VALID_BOOLEAN_VALUES = [0, 1, 'true', 'false', :true, :false, true, false, '0', '1'].freeze + VALID_BOOLEAN_CLASSES = [TrueClass, FalseClass, Integer, String, Symbol].freeze + VALID_BOOLEAN_VALUES = [true, false, 1, 0, '1', '0', 'true', 'false', :true, :false].freeze BOOLEAN_VALIDATOR = lambda { |arg| VALID_BOOLEAN_VALUES.include?(arg) } # Requires that the value is a form that can be evaluated as a boolean in an xml document. diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 3e4927aa..1a2f0488 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -395,8 +395,8 @@ module Axlsx # retrieve the cells from. e.g. range('Sheet1!A1:B2') will return an array of four cells [A1, A2, B1, B2] while range('Sheet1!A1') will return a single Cell. # @return [Cell, Array] def [](cell_def) - sheet_name = cell_def.split('!')[0] if cell_def.match('!') - worksheet = self.worksheets.select { |s| s.name == sheet_name }.first + sheet_name = cell_def.split('!')[0] if cell_def.include?('!') + worksheet = self.worksheets.find { |s| s.name == sheet_name } raise ArgumentError, 'Unknown Sheet' unless sheet_name && worksheet.is_a?(Worksheet) worksheet[cell_def.gsub(/.+!/, "")] diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index f47781f0..4d1bae63 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -42,12 +42,13 @@ module Axlsx self.type = type unless type == :string val = options.delete(:style) - self.style = val unless val.nil? || val == 0 + self.style = val unless val.nil? || val.zero? val = options.delete(:formula_value) self.formula_value = val unless val.nil? + val = options.delete(:escape_formulas) + self.escape_formulas = val unless val.nil? parse_options(options) - self.escape_formulas = row.worksheet.escape_formulas if escape_formulas.nil? self.value = value value.cell = self if contains_rich_text? @@ -135,7 +136,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. @@ -272,15 +275,13 @@ module Axlsx def extend=(v) set_run_style :validate_boolean, :extend, v; end # The inline underline property for the cell. - # It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true + # It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting # @return [Boolean] # @return [String] - # @note true is for backwards compatability and is reassigned to :single attr_reader :u # @see u def u=(v) - v = :single if (v == true || v == 1 || v == :true || v == 'true') set_run_style :validate_cell_u, :u, v end @@ -343,7 +344,7 @@ module Axlsx # @example Absolute Cell Reference # ws.rows.first.cells.first.r #=> "$A$1" def r_abs - "$#{r.match(%r{([A-Z]+)([0-9]+)})[1, 2].join('$')}" + "$#{r.match(/([A-Z]+)([0-9]+)/)[1, 2].join('$')}" end # @return [Integer] The cellXfs item index applied to this cell. @@ -368,7 +369,7 @@ module Axlsx start, stop = if target.is_a?(String) [self.r, target] elsif target.is_a?(Cell) - Axlsx.sort_cells([self, target]).map { |c| c.r } + Axlsx.sort_cells([self, target]).map(&:r) end self.row.worksheet.merge_cells "#{start}:#{stop}" unless stop.nil? end @@ -498,13 +499,11 @@ module Axlsx :time elsif v.is_a?(TrueClass) || v.is_a?(FalseClass) :boolean - elsif v.to_s =~ Axlsx::NUMERIC_REGEX && v.respond_to?(:to_i) + elsif v.respond_to?(:to_i) && v.to_s =~ Axlsx::NUMERIC_REGEX :integer - elsif v.to_s =~ Axlsx::SAFE_FLOAT_REGEX && v.respond_to?(:to_f) - :float - elsif (matchdata = v.to_s.match(MAYBE_FLOAT_REGEX)) && (Float::MIN_10_EXP..Float::MAX_10_EXP).cover?(matchdata[:exp].to_i) && v.respond_to?(:to_f) + elsif v.respond_to?(:to_f) && (v.to_s =~ Axlsx::SAFE_FLOAT_REGEX || ((matchdata = v.to_s.match(MAYBE_FLOAT_REGEX)) && matchdata[:exp].to_i.between?(Float::MIN_10_EXP, Float::MAX_10_EXP))) :float - elsif v.to_s =~ Axlsx::ISO_8601_REGEX + elsif Axlsx::ISO_8601_REGEX.match?(v.to_s) :iso_8601 elsif v.is_a? RichText :richtext @@ -522,14 +521,14 @@ module Axlsx case type when :date - self.style = STYLE_DATE if self.style == 0 + self.style = STYLE_DATE if self.style.zero? if !v.is_a?(Date) && v.respond_to?(:to_date) v.to_date else v end when :time - self.style = STYLE_DATE if self.style == 0 + self.style = STYLE_DATE if self.style.zero? if !v.is_a?(Time) && v.respond_to?(:to_time) v.to_time else diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index 9d39add3..f45d3c73 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -10,7 +10,9 @@ module Axlsx # @param [String] str The string to apend serialization to. # @return [String] def to_xml_string(row_index, column_index, cell, str = +'') - str << '<c r="' << Axlsx::cell_r(column_index, row_index) << '" s="' << cell.style.to_s << '" ' + str << '<c r="' + str << Axlsx::col_ref(column_index) << Axlsx::row_ref(row_index) + str << '" s="' << cell.style.to_s << '" ' return str << '/>' if cell.value.nil? method = cell.type diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb index 16116835..dc0320b7 100644 --- a/lib/axlsx/workbook/worksheet/row.rb +++ b/lib/axlsx/workbook/worksheet/row.rb @@ -89,7 +89,7 @@ module Axlsx # @param [String] str The string this rows xml will be appended to. # @return [String] def to_xml_string(r_index, str = +'') - serialized_tag('row', str, :r => r_index + 1) do + serialized_tag('row', str, :r => Axlsx.row_ref(r_index)) do each_with_index { |cell, c_index| cell.to_xml_string(r_index, c_index, str) } end end 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? |
