summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx
diff options
context:
space:
mode:
authorZsolt Kozaroczy <[email protected]>2023-05-17 10:08:04 +0200
committerGitHub <[email protected]>2023-05-17 10:08:04 +0200
commit3e6badd102edd9f4ec54b12ead2e769474e4a045 (patch)
treec35b58dca864ad0f4800445e58e2ca2eca01d31b /lib/axlsx
parent7d70e17cd818958eac09f68c8886b9664aa4ad8c (diff)
parent7899578ad12fb710cbdf74e1aa87e2190c5832ed (diff)
downloadcaxlsx-3e6badd102edd9f4ec54b12ead2e769474e4a045.tar.gz
caxlsx-3e6badd102edd9f4ec54b12ead2e769474e4a045.zip
Merge branch 'master' into serializedAttributes
Diffstat (limited to 'lib/axlsx')
-rw-r--r--lib/axlsx/drawing/pic.rb2
-rw-r--r--lib/axlsx/util/constants.rb12
-rw-r--r--lib/axlsx/util/validators.rb4
-rw-r--r--lib/axlsx/workbook/workbook.rb4
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb43
-rw-r--r--lib/axlsx/workbook/worksheet/cell_serializer.rb8
-rw-r--r--lib/axlsx/workbook/worksheet/row.rb2
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb1
8 files changed, 39 insertions, 37 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 4697bb3a..5ef3ece1 100644
--- a/lib/axlsx/util/constants.rb
+++ b/lib/axlsx/util/constants.rb
@@ -413,4 +413,16 @@ module Axlsx
# Numeric recognition
NUMERIC_REGEX = /\A[+-]?\d+?\Z/.freeze
+
+ # Leading characters that indicate a formula.
+ # See: https://owasp.org/www-community/attacks/CSV_Injection
+ FORMULA_PREFIX = '='
+
+ # Leading characters that indicate an array formula.
+ ARRAY_FORMULA_PREFIX = '{='
+
+ # 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 929a7c28..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?
@@ -72,16 +73,6 @@ module Axlsx
CELL_TYPES = [:date, :time, :float, :integer, :richtext,
:string, :boolean, :iso_8601, :text].freeze
- # Leading characters that indicate a formula.
- # See: https://owasp.org/www-community/attacks/CSV_Injection
- FORMULA_PREFIXES = ['='].freeze
-
- # Leading characters that indicate an array formula.
- ARRAY_FORMULA_PREFIXES = ['{='].freeze
-
- # Trailing character that indicates an array formula.
- ARRAY_FORMULA_SUFFIX = '}'
-
# The index of the cellXfs item to be applied to this cell.
# @return [Integer]
# @see Axlsx::Styles
@@ -145,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.
@@ -282,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
@@ -353,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.
@@ -378,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
@@ -395,14 +386,14 @@ module Axlsx
def is_formula?
return false if escape_formulas
- type == :string && @value.to_s.start_with?(*FORMULA_PREFIXES)
+ type == :string && @value.to_s.start_with?(FORMULA_PREFIX)
end
def is_array_formula?
return false if escape_formulas
type == :string &&
- @value.to_s.start_with?(*ARRAY_FORMULA_PREFIXES) &&
+ @value.to_s.start_with?(ARRAY_FORMULA_PREFIX) &&
@value.to_s.end_with?(ARRAY_FORMULA_SUFFIX)
end
@@ -508,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
@@ -532,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 e1bdf728..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
@@ -88,7 +90,7 @@ module Axlsx
# @param [String] str The string the serialized content will be appended to.
# @return [String]
def formula_serialization(cell, str = +'')
- str << 't="str"><f>' << cell.clean_value.to_s.sub('=', '') << '</f>'
+ str << 't="str"><f>' << cell.clean_value.to_s.delete_prefix(FORMULA_PREFIX) << '</f>'
str << '<v>' << cell.formula_value.to_s << '</v>' unless cell.formula_value.nil?
end
@@ -97,7 +99,7 @@ module Axlsx
# @param [String] str The string the serialized content will be appended to.
# @return [String]
def array_formula_serialization(cell, str = +'')
- str << 't="str">' << '<f t="array" ref="' << cell.r << '">' << cell.clean_value.to_s.sub('{=', '').sub(/}$/, '') << '</f>'
+ str << 't="str">' << '<f t="array" ref="' << cell.r << '">' << cell.clean_value.to_s.delete_prefix(ARRAY_FORMULA_PREFIX).delete_suffix(ARRAY_FORMULA_SUFFIX) << '</f>'
str << '<v>' << cell.formula_value.to_s << '</v>' unless cell.formula_value.nil?
end
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?