Search code examples
rubyruby-roo

Access to merged cells using Ruby-Roo


According to example below: Value is stored only in A1, other cells return nil. How is possible to get the A1'a value from the others merged cells, or simply check range of the A1 cell?

example


Solution

  • This is not possible without first assigning the value to all the cells of the range, even in Excel VBA this is the case.

    See this sample

    require 'axlsx'
    
    p = Axlsx::Package.new
    wb = p.workbook
    
    wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
      sheet.add_row ["Val", nil]
      sheet.add_row [nil, nil]
      merged = sheet.merge_cells('A1:B2')
      p sheet.rows[0].cells[0].value # "Val"
      p sheet.rows[0].cells[1].value # nil
      sheet[*merged].each{|cell|cell.value = sheet[*merged].first.value}
      p sheet.rows[0].cells[0].value # "Val"
      p sheet.rows[0].cells[1].value # "Val"
    end
    
    p.serialize('./simple.xlsx')
    

    Please add a sample yourself next time so that we see which gem you used, which code, error etc.