在以下代码中,happy_path
有效,但无效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
给出错误
undefined method `value=' for nil:NilClass
因为cell
是零。
http://rubydoc.info/github/randym/axlsx/Axlsx/Cell和文档的其他部分警告:
注意:生成单元格的推荐方法是通过 Worksheet#add_row
问题是我希望能够将数据添加到 A1、B1 和 C1,然后再将数据添加到 E1、F1 和 G1。
有没有办法做到这一点?或者我应该存储 A1、B1、C1 和 E1、F1 和 G1 的数据,并一次将其全部添加到工作表中?