weshatheleopard / rubyXL

Ruby lib for reading/writing/modifying .xlsx and .xlsm files
MIT License
1.28k stars 255 forks source link

changing certain cell attributes changes them globally #315

Closed yskarpathiotis closed 5 years ago

yskarpathiotis commented 5 years ago

I have noticed this behavior for several other attributes as well (horizontal_alignment for example). I wasnt sure if this is expected functionality. The README implies that this should be able to get set at a cell level.

workbook[0][0][2].change_text_wrap(true)
workbook[0][0][4].change_text_wrap(true)
workbook[0][0][4].change_text_wrap(false)
dummy.workbook[0][0][2].text_wrap // returns false
Yuji-Kuroko commented 5 years ago

I also encountered that. About alignment, it seems that you can avoid the phenomenon by writing the following monkey patch.

https://github.com/weshatheleopard/rubyXL/blob/74b4717c298a9eb19710d402eee00b210edcff05/lib/rubyXL/convenience_methods.rb#L152-L159

module RubyXL
  module WorkbookConvenienceMethods
    def modify_alignment(style_index, &block)
      xf = cell_xfs[style_index || 0].dup
      xf.alignment = RubyXL::Alignment.new
      yield(xf.alignment)
      xf.apply_alignment = true

      register_new_xf(xf)
    end
  end
end