diff options
| author | Zsolt Kozaroczy <[email protected]> | 2023-05-17 09:46:26 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-17 09:46:26 +0200 |
| commit | 5cda9e7f226a6d0f9ca38ba501c024f3bb564244 (patch) | |
| tree | 8e172b42e8ee104a0b2dca2283b2d8ac0076e846 /lib | |
| parent | a0bef85fc877afe91e22558bac5e14e2f7f88dbe (diff) | |
| parent | b099c0195f19458c1484f389955a2a0cf085a7ca (diff) | |
| download | caxlsx-5cda9e7f226a6d0f9ca38ba501c024f3bb564244.tar.gz caxlsx-5cda9e7f226a6d0f9ca38ba501c024f3bb564244.zip | |
Merge branch 'master' into chore/use-detect-and-include
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx.rb | 76 | ||||
| -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/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, 68 insertions, 52 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 4f15ba23..bf16afbd 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -1,29 +1,29 @@ # frozen_string_literal: true require 'htmlentities' -require 'axlsx/version.rb' +require 'axlsx/version' require 'marcel' -require 'axlsx/util/simple_typed_list.rb' -require 'axlsx/util/constants.rb' -require 'axlsx/util/validators.rb' -require 'axlsx/util/accessors.rb' +require 'axlsx/util/simple_typed_list' +require 'axlsx/util/constants' +require 'axlsx/util/validators' +require 'axlsx/util/accessors' require 'axlsx/util/serialized_attributes' require 'axlsx/util/options_parser' require 'axlsx/util/mime_type_utils' require 'axlsx/util/buffered_zip_output_stream' require 'axlsx/util/zip_command' -require 'axlsx/stylesheet/styles.rb' +require 'axlsx/stylesheet/styles' -require 'axlsx/doc_props/app.rb' -require 'axlsx/doc_props/core.rb' -require 'axlsx/content_type/content_type.rb' -require 'axlsx/rels/relationships.rb' +require 'axlsx/doc_props/app' +require 'axlsx/doc_props/core' +require 'axlsx/content_type/content_type' +require 'axlsx/rels/relationships' -require 'axlsx/drawing/drawing.rb' -require 'axlsx/workbook/workbook.rb' -require 'axlsx/package.rb' +require 'axlsx/drawing/drawing' +require 'axlsx/workbook/workbook' +require 'axlsx/package' # required gems require 'nokogiri' require 'zip' @@ -35,9 +35,9 @@ require 'time' begin if Gem.loaded_specs.has_key?("axlsx_styler") - raise StandardError.new("Please remove `axlsx_styler` from your Gemfile, the associated functionality is now built-in to `caxlsx` directly.") + raise StandardError, "Please remove `axlsx_styler` from your Gemfile, the associated functionality is now built-in to `caxlsx` directly." end -rescue +rescue StandardError # Do nothing end @@ -53,7 +53,7 @@ module Axlsx # # Defining as a class method on Axlsx to refrain from monkeypatching Object for all users of this gem. def self.instance_values_for(object) - Hash[object.instance_variables.map { |name| [name.to_s[1..-1], object.instance_variable_get(name)] }] + object.instance_variables.to_h { |name| [name.to_s[1..], object.instance_variable_get(name)] } end # determines the cell range for the items provided @@ -105,37 +105,51 @@ module Axlsx row_index = (numbers_str.to_i - 1) - return [col_index, row_index] + [col_index, row_index] end # converts the column index into alphabetical values. # @note This follows the standard spreadsheet convention of naming columns A to Z, followed by AA to AZ etc. # @return [String] def self.col_ref(index) - chars = +'' - while index >= 26 do - index, char = index.divmod(26) - chars.prepend((char + 65).chr) - index -= 1 + # Every row will call this for each column / cell and so we can cache result and avoid lots of small object + # allocations. + @col_ref ||= {} + @col_ref[index] ||= begin + i = index + chars = +'' + while i >= 26 + i, char = i.divmod(26) + chars.prepend((char + 65).chr) + i -= 1 + end + chars.prepend((i + 65).chr) + chars.freeze end - chars.prepend((index + 65).chr) - chars + end + + # converts the row index into string values. + # @note The spreadsheet rows are 1-based and the passed in index is 0-based, so we add 1. + # @return [String] + def self.row_ref(index) + @row_ref ||= {} + @row_ref[index] ||= (index + 1).to_s.freeze end # @return [String] The alpha(column)numeric(row) reference for this sell. # @example Relative Cell Reference # ws.rows.first.cells.first.r #=> "A1" def self.cell_r(c_index, r_index) - col_ref(c_index) << (r_index + 1).to_s + col_ref(c_index) + row_ref(r_index) end # Creates an array of individual cell references based on an excel reference range. # @param [String] range A cell range, for example A1:D5 # @return [Array] def self.range_to_a(range) - range.match(/^(\w+?\d+)\:(\w+?\d+)$/) - start_col, start_row = name_to_indices($1) - end_col, end_row = name_to_indices($2) + range =~ /^(\w+?\d+):(\w+?\d+)$/ + start_col, start_row = name_to_indices(::Regexp.last_match(1)) + end_col, end_row = name_to_indices(::Regexp.last_match(2)) (start_row..end_row).to_a.map do |row_num| (start_col..end_col).to_a.map do |col_num| cell_r(col_num, row_num) @@ -149,7 +163,7 @@ module Axlsx def self.camel(s = "", all_caps = true) s = s.to_s s = s.capitalize if all_caps - s.gsub(/_(.)/) { $1.upcase } + s.gsub(/_(.)/) { ::Regexp.last_match(1).upcase } end # returns the provided string with all invalid control charaters @@ -170,7 +184,7 @@ module Axlsx # @param [Object] value The value to process # @return [Object] def self.booleanize(value) - if value == true || value == false + if BOOLEAN_VALUES.include?(value) value ? 1 : 0 else value @@ -181,7 +195,7 @@ module Axlsx # @param [Hash] Hash to merge into # @param [Hash] Hash to be added def self.hash_deep_merge(first_hash, second_hash) - first_hash.merge(second_hash) do |key, this_val, other_val| + first_hash.merge(second_hash) do |_key, this_val, other_val| if this_val.is_a?(Hash) && other_val.is_a?(Hash) Axlsx.hash_deep_merge(this_val, other_val) else 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 4697bb3a..af8d9702 100644 --- a/lib/axlsx/util/constants.rb +++ b/lib/axlsx/util/constants.rb @@ -413,4 +413,6 @@ module Axlsx # Numeric recognition NUMERIC_REGEX = /\A[+-]?\d+?\Z/.freeze + + 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/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 929a7c28..cbf55482 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? @@ -145,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. @@ -282,15 +285,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 @@ -353,7 +354,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. @@ -378,7 +379,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 @@ -508,13 +509,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 @@ -532,14 +531,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 e1bdf728..2baa4271 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? |
