weshatheleopard / rubyXL

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

NoMethodError: undefined method `insert_column' for #<RubyXL::Worksheet: 0x007fc2e7a35a00 > #328

Closed gremito closed 5 years ago

gremito commented 5 years ago

The Code(sample) following fails:

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

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

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

Console:

$ bundle exec rake add_cell PATH=/Users/gremito/Desktop/Book1.xlsx
excel_data[0].sheet_name: Sheet1
excel_data[0][0][0].value: type
rake aborted!
NoMethodError: undefined method `insert_column' for #<RubyXL::Worksheet:0x007fc2e7a35a00>
/Users/gremito/ruby/rubyXL/Rakefile:115: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)

The details of Excel are as follows.

Attach a problematic Excel data to it. Book1.xlsx

weshatheleopard commented 5 years ago

Did you read the documentation for 3.4.0? Especially "Convenience methods" section?

gremito commented 5 years ago

@weshatheleopard I am looking at the document of 3.4.2. The source code is the same version.

weshatheleopard commented 5 years ago

@gremito Namely:

Starting with version 3.4.0, the main data structure has been separated from the convenience methods that provide access to individual features of the xlsx format, in order to decrease the memory footprint. If you intend to use these features, you will need to additionally include the respective files:

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'

If you do not care about your RAM usage, just include them all at once by adding the following line to your code so it can continue operating just as before:

require 'rubyXL/convenience_methods'

gremito commented 5 years ago

@weshatheleopard Ohh I see! Was it convenience-methods on the README.

I applied it again and confirmed it.

Code:

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[0].sheet_name: #{excel_data[0].sheet_name}"
  puts "excel_data[0][0][0].value: #{excel_data[0][0][0].value}"

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

  puts "excel_data[0][0][0].value: #{excel_data[0][0][0].value}"
end

Console:

$ bundle exec rake add_cell PATH=/Users/iwamoto_takuya/Desktop/Book1.xlsx
excel_data[0].sheet_name: Sheet1
excel_data[0][0][0].value: type
excel_data[0][0][0].value: 【Test Data】

Thanks!!