summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorAgustin Gomez <[email protected]>2021-03-19 10:50:03 -0300
committerGitHub <[email protected]>2021-03-19 14:50:03 +0100
commit37acfa2e3692fbc278e3a06df457fde8d88a5f01 (patch)
treeec72ed0fff861ff6df224868abb7702a18b33c63 /lib
parented329dae5d9a69a11726a0ceba27e77aaf8adaa0 (diff)
downloadcaxlsx-37acfa2e3692fbc278e3a06df457fde8d88a5f01.tar.gz
caxlsx-37acfa2e3692fbc278e3a06df457fde8d88a5f01.zip
Implement :offset option for worksheet#add_row (#87)
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx/workbook/worksheet/row.rb8
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb4
2 files changed, 9 insertions, 3 deletions
diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb
index decd27c7..80937cf2 100644
--- a/lib/axlsx/workbook/worksheet/row.rb
+++ b/lib/axlsx/workbook/worksheet/row.rb
@@ -25,11 +25,12 @@ module Axlsx
# @option options [Array, Symbol] types
# @option options [Array, Integer] style
# @option options [Float] height the row's height (in points)
+ # @option options [Integer] offset - add empty columns before values
# @see Row#array_to_cells
# @see Cell
def initialize(worksheet, values=[], options={})
self.worksheet = worksheet
- super(Cell, nil, values.size)
+ super(Cell, nil, values.size + options[:offset].to_i)
self.height = options.delete(:height)
worksheet.rows << self
array_to_cells(values, options)
@@ -147,14 +148,15 @@ module Axlsx
# @option options [Array, Integer] style
def array_to_cells(values, options={})
DataTypeValidator.validate :array_to_cells, Array, values
- types, style, formula_values, escape_formulas = options.delete(:types), options.delete(:style), options.delete(:formula_values), options.delete(:escape_formulas)
+ types, style, formula_values, escape_formulas, offset = options.delete(:types), options.delete(:style), options.delete(:formula_values), options.delete(:escape_formulas), options.delete(:offset)
+ offset.to_i.times { |index| self[index] = Cell.new(self) } if offset
values.each_with_index do |value, index|
options[:style] = style.is_a?(Array) ? style[index] : style if style
options[:type] = types.is_a?(Array) ? types[index] : types if types
options[:escape_formulas] = escape_formulas.is_a?(Array) ? escape_formulas[index] : escape_formulas if escape_formulas
options[:formula_value] = formula_values[index] if formula_values.is_a?(Array)
- self[index] = Cell.new(self, value, options)
+ self[index + offset.to_i] = Cell.new(self, value, options)
end
end
end
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 9f3966f8..06a5e7f1 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -393,6 +393,9 @@ module Axlsx
# @example - specify whether a certain cells in a row should escape formulas or not
# ws.add_row ['=IF(2+2=4,4,5)', '=IF(13+13=4,4,5)'], :escape_formulas=>[true, false]
#
+ # @example - add a column offset when adding a row (inserts 'n' blank, unstyled columns before data)
+ # ws.add_row ['I wish', 'for a fish', 'on my fish wish dish'], offset: 3
+ #
# @see Worksheet#column_widths
# @return [Row]
# @option options [Array] values
@@ -400,6 +403,7 @@ module Axlsx
# @option options [Array, Integer] style
# @option options [Array] widths each member of the widths array will affect how auto_fit behavies.
# @option options [Float] height the row's height (in points)
+ # @option options [Integer] offset - add empty columns before values
# @option options [Array, Boolean] escape_formulas - Whether to treat a value starting with an equal
# sign as formula (default) or as simple string.
# Allowing user generated data to be interpreted as formulas can be dangerous