Pergunta

In the following code, happy_path works, but not sad_path:

require "axlsx"

def happy_path
  package = Axlsx::Package.new
  workbook = package.workbook
  worksheet = workbook.add_worksheet
  worksheet.add_row(["Hot", "Summer", "Night"])
  cell = worksheet.name_to_cell("A1")
  cell.value = "Cold"
  package.serialize("Happy_path_20130104.xlsx")
end

def sad_path
  package = Axlsx::Package.new
  workbook = package.workbook
  worksheet = workbook.add_worksheet
  worksheet.add_row(["Hot", "Summer", "Night"])
  cell = worksheet.name_to_cell("E1")
  begin
    cell.value = "Meatloaf"
  rescue NoMethodError
    STDERR.puts $!
  end
  package.serialize("Sad_path_20130104.xlsx")
end

happy_path
sad_path

sad_path gives the error

undefined method `value=' for nil:NilClass

because cell is nil.

http://rubydoc.info/github/randym/axlsx/Axlsx/Cell and other parts of the documentation warn:

Note: The recommended way to generate cells is via Worksheet#add_row

The problem is that I want to be able to add data to A1, B1, and C1, and then later on add data to E1, F1 and G1.

Is there any way of doing that? Or should I store the data for A1, B1, C1 and E1, F1 and G1, and add it to the worksheet all at once?

Foi útil?

Solução

I'd like to see more discussion before adding a method like this to the library. Axlsx is intentionally sparse to minimize serialization time. if you could submit a pull request on github it would really help everyone. in the mean time, if you send in nil values cells will be created as per your requirements.

for example:

add_row ["foo", nil, nil]

will create A1...C1 cells and you can directly access the methods of those cells.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top