Forum: Ruby on Rails How can I check if a cell is empty using RubyXL?

5a41028798f4e263c2d0692f5b1847ec?d=identicon&s=25 Monserrat Foster (Guest)
on 2013-10-17 21:00
(Received via mailing list)
I'm trying to check if a cell is empty because when the parser reaches
an
empty cell it throws NoMethodError (undefined methodvalue' for
nil:NilClass)`

This is what I was trying to do:

1.upto(inventory[0].sheet_data.size) do |line|
      @projectCode = inventory[0][line][1].value unless
inventory[0][line][1].value.nil?
      @ProjectName = inventory[0][line][2].value unless
inventory[0][line][2].value.nil?end

also tried

1.upto(inventory[0].sheet_data.size) do |line|
      @projectCode = inventory[0][line][1].value unless
inventory[0][line][1].value.empty?
      @ProjectName = inventory[0][line][2].value unless
inventory[0][line][2].value.empty?
end

The cells with values have no issues, but when it reaches 2 empty rows
below all data it throws this... Can anyone tell me how to do this?
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-10-17 22:17
(Received via mailing list)
On 17 October 2013 19:58, Monserrat Foster <monsefoster@gmail.com>
wrote:
> I'm trying to check if a cell is empty because when the parser reaches an
> empty cell it throws NoMethodError (undefined methodvalue' for
> nil:NilClass)`
>
> This is what I was trying to do:
>
> 1.upto(inventory[0].sheet_data.size) do |line|
>       @projectCode = inventory[0][line][1].value unless
> inventory[0][line][1].value.nil?

Is it not fairly obvious?  The error message says you are calling the
method value on something that is nil, so presumably
inventory[0][line][1] is nil.  So just test that for nil.  There are
nicer ways of coding this however, I might use
@projectCode = inventory[0][line][1].value if inventory[0][line][1]

However you seem to be in a loop that keeps overwriting @projectCode
but perhaps you have snipped out some of the code.

Colin
5a41028798f4e263c2d0692f5b1847ec?d=identicon&s=25 Monserrat Foster (Guest)
on 2013-10-18 18:39
(Received via mailing list)
tried
@projectCode = inventory[0][line][1].value if inventory[0][line][1].nil?

and even testing inventory[0][line].nil? and inventory[0].nil? and I
keep
getting the same error

undefined method `[]' for nil:NilClass

I am overwriting @projectCode with each iteration because there's a
comparison later.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-10-18 19:28
(Received via mailing list)
On 18 October 2013 17:38, Monserrat Foster <monsefoster@gmail.com>
wrote:

Please don't top post, it makes it difficult to follow the thread.
Insert your replies inline at appropriate points in previous message.
Thanks.

> tried
> @projectCode = inventory[0][line][1].value if inventory[0][line][1].nil?
>
> and even testing inventory[0][line].nil? and inventory[0].nil? and I keep
> getting the same error
>
> undefined method `[]' for nil:NilClass

if inventory[0] gives you that message then inventory must be nil.
Put some debug code in to check out what is happening.  The simplest
way is to insert puts statements to show the values of variables.  The
output will appear in the server terminal.  For more sophisticated
debug techniques see the Rails Guide on Debugging.

Colin
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.