lib/rubyXL/convenience_methods/worksheet.rb in rubyXL-3.4.17 vs lib/rubyXL/convenience_methods/worksheet.rb in rubyXL-3.4.18

- old
+ new

@@ -105,22 +105,23 @@ cell.r = RubyXL::Reference.new(r, c) } } # Update merged cells for all rows below - self.merged_cells ||= RubyXL::MergedCells.new - merged_cells.each { |mc| - next if mc.ref.row_range.last < row_index + if self.merged_cells then + merged_cells.each { |mc| + next if mc.ref.row_range.last < row_index - in_merged_cell = mc.ref.row_range.first < row_index - mc.ref = RubyXL::Reference.new( - mc.ref.row_range.first + (in_merged_cell ? 0 : 1), - mc.ref.row_range.last + 1, - mc.ref.col_range.first, - mc.ref.col_range.last, - ) - } + in_merged_cell = mc.ref.row_range.first < row_index + mc.ref = RubyXL::Reference.new( + mc.ref.row_range.first + (in_merged_cell ? 0 : 1), + mc.ref.row_range.last + 1, + mc.ref.col_range.first, + mc.ref.col_range.last, + ) + } + end return new_row end def delete_row(row_index=0) @@ -134,24 +135,25 @@ row = sheet_data[index] row && row.cells.each{ |c| c.row -= 1 unless c.nil? } } # Update row number of merged cells - self.merged_cells ||= RubyXL::MergedCells.new - merged_cells.delete_if { |mc| mc.ref.row_range == (row_index..row_index) } - merged_cells.each { |mc| - next if mc.ref.row_range.last < row_index + if self.merged_cells then + merged_cells.delete_if { |mc| mc.ref.row_range == (row_index..row_index) } + merged_cells.each { |mc| + next if mc.ref.row_range.last < row_index - in_merged_cell = mc.ref.row_range.first <= row_index - mc.ref = RubyXL::Reference.new( - mc.ref.row_range.first - (in_merged_cell ? 0 : 1), - mc.ref.row_range.last - 1, - mc.ref.col_range.first, - mc.ref.col_range.last, - ) - } - merged_cells.delete_if { |mc| mc.ref.single_cell? } + in_merged_cell = mc.ref.row_range.first <= row_index + mc.ref = RubyXL::Reference.new( + mc.ref.row_range.first - (in_merged_cell ? 0 : 1), + mc.ref.row_range.last - 1, + mc.ref.col_range.first, + mc.ref.col_range.last, + ) + } + merged_cells.delete_if { |mc| mc.ref.single_cell? } + end return deleted end # Inserts column at +column_index+, pushes everything right, takes styles from column to left @@ -181,22 +183,23 @@ } cols.insert_column(column_index) # Update merged cells for all rows below - self.merged_cells ||= RubyXL::MergedCells.new - merged_cells.each { |mc| - next if mc.ref.col_range.last < column_index + if self.merged_cells then + merged_cells.each { |mc| + next if mc.ref.col_range.last < column_index - in_merged_cell = mc.ref.row_range.first < column_index - mc.ref = RubyXL::Reference.new( - mc.ref.row_range.first, - mc.ref.row_range.last, - mc.ref.col_range.first + (in_merged_cell ? 0 : 1), - mc.ref.col_range.last + 1, - ) - } + in_merged_cell = mc.ref.row_range.first < column_index + mc.ref = RubyXL::Reference.new( + mc.ref.row_range.first, + mc.ref.row_range.last, + mc.ref.col_range.first + (in_merged_cell ? 0 : 1), + mc.ref.col_range.last + 1, + ) + } + end # TODO: update column numbers end def delete_column(column_index = 0) @@ -215,11 +218,12 @@ } cols.each { |range| range.delete_column(column_index) } # Update row number of merged cells - self.merged_cells ||= RubyXL::MergedCells.new + return unless self.merged_cells + merged_cells.delete_if { |mc| mc.ref.col_range == (column_index..column_index) } merged_cells.each { |mc| next if mc.ref.col_range.last < column_index in_merged_cell = mc.ref.col_range.first <= column_index @@ -228,9 +232,10 @@ mc.ref.row_range.last, mc.ref.col_range.first - (in_merged_cell ? 0 : 1), mc.ref.col_range.last - 1, ) } + merged_cells.delete_if { |mc| mc.ref.single_cell? } end def get_row_style(row_index) row = sheet_data.rows[row_index]