weshatheleopard / rubyXL

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

NoMethodError: undefined method `[]' for nil:NilClass ...rubyXL/convenience_methods/worksheet.rb:125:in `block in insert_column' #329

Closed gremito closed 5 years ago

gremito commented 5 years ago

It's a continuation of https://github.com/weshatheleopard/rubyXL/issues/328

desc "add_cell"
task :add_cell do
  $:.unshift File.dirname(__FILE__) + '/lib'
  require './lib/rubyXL'
  require './lib/rubyXL/convenience_methods/worksheet'

  excel_data = RubyXL::Parser.parse(ENV["PATH"])
  puts "excel_data[#{ENV["SHEET_NAME"]}].sheet_name: #{excel_data[ENV["SHEET_NAME"]].sheet_name}"
  puts "excel_data[#{ENV["SHEET_NAME"]}][0][0].value: #{excel_data[ENV["SHEET_NAME"]][0][0].value}"

  excel_data[ENV["SHEET_NAME"]].insert_column(0)
  excel_data[ENV["SHEET_NAME"]].add_cell(0, 0, "【Test Data】")

  puts "excel_data[#{ENV["SHEET_NAME"]}][0][0].value: #{excel_data[ENV["SHEET_NAME"]][0][0].value}"
end
$ bundle exec rake add_cell PATH=/Users/gremito/Desktop/Book1.xlsx SHEET_NAME=CopySheet
excel_data[CopySheet].sheet_name: CopySheet
excel_data[CopySheet][0][0].value: int
excel_data[CopySheet][0][0].value: ?Test Data?

$ bundle exec rake add_cell PATH=/Users/gremito/Desktop/Book1.xlsx SHEET_NAME=ProblemSheet
excel_data[ProblemSheet].sheet_name: ProblemSheet
excel_data[ProblemSheet][0][0].value: int
rake aborted!
NoMethodError: undefined method `[]' for nil:NilClass
/Users/gremito/ruby/rubyXL/lib/rubyXL/convenience_methods/worksheet.rb:125:in `block in insert_column'
/Users/gremito/ruby/rubyXL/lib/rubyXL/convenience_methods/worksheet.rb:124:in `each'
/Users/gremito/ruby/rubyXL/lib/rubyXL/convenience_methods/worksheet.rb:124:in `each_with_index'
/Users/gremito/ruby/rubyXL/lib/rubyXL/convenience_methods/worksheet.rb:124:in `insert_column'
/Users/gremito/ruby/rubyXL/Rakefile:116:in `block in <top (required)>'
/Users/gremito/.rbenv/versions/2.3.1/bin/bundle:23:in `load'
/Users/gremito/.rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
Tasks: TOP => add_cell
(See full trace by running task with --trace)

I'm attaching a problematic Excel data to it. Book1.xlsx

weshatheleopard commented 5 years ago

I added support for sparse data. Try 3.4.3.