rubyXL icon indicating copy to clipboard operation
rubyXL copied to clipboard

It doesn't clear the formula while setting a value in a cell with 'change_contents(value)' method

Open ershad opened this issue 9 years ago • 1 comments

I was trying to change the value in a cell which has a formula. Ideally it should clear the formula and write the value, instead in some cases it's setting the value without clearing the cell.

Here's a sample snippet I used:

workbook = RubyXL::Parser.parse(xlsx_file_path)
worksheet = workbook.worksheets[0]
worksheet[1][1].change_contents(120)

While inspecting the cell after writing, it shows like this:

> puts worksheet[1][1].inspect
#<RubyXL::Cell(1,1): 120.0 =G98*(1+$E$98), datatype=nil, style_index=54>

Is this a known issue? Do we have a method to clear the cell and write the value? I tried deleting/inserting that specific cell, but that cleared the values in other cells in the same row (which had values computed with formula).

Thanks in advance.

ershad avatar Oct 05 '16 15:10 ershad

Got same issue. I put workaround method for my project to prevent this issue.

module RubyXL
  module CellConvenienceMethods
    def change_value(data)
      self.change_contents(data)
      self.formula = nil
      self.datatype = nil
      workbook.calculation_chain.cells.select! { |c|
        !(c.ref.col_range.begin == self.column && c.ref.row_range.begin == self.row)
      }
    end
  end
end

woremacx avatar Nov 02 '16 11:11 woremacx