weshatheleopard / rubyXL

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

`<main>': undefined method `[]' for nil:NilClass (NoMethodError) #371

Closed amurlikowski closed 4 years ago

amurlikowski commented 4 years ago

The code:

require 'rubyXL'

require 'rubyXL/convenience_methods'

require 'rubyXL/convenience_methods/cell'
require 'rubyXL/convenience_methods/color'
require 'rubyXL/convenience_methods/font'
require 'rubyXL/convenience_methods/workbook'
require 'rubyXL/convenience_methods/worksheet'

workbook = RubyXL::Workbook.new

cell = workbook[0][0][0]

workbook.write("./file.xlsx")

The error:

D:\RubyXl>test.rb
Traceback (most recent call last):
D:/RubyXl/test.rb:13:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

Version: rubyXL (3.4.14)

mattiamalonni commented 4 years ago

By default the cell does not exist. You have to initialize it first with:

worksheet.add_cell(0, 0, '')