bigwinner
bigwinner

Reputation: 629

Ruby Spreadsheet Gem: extracting value from a formula cell in an Excel file

I'm using the spreadsheet gem for an RoR app.

I'm extracting data from a cell in the Excel sheet using the following line of code:

current_qsubnum = @mainsheet.cell(y_qsubnum, x_current)

I then do some processing based on this data.

However, what I've realized is that sometimes my users put a formula in that cell, which I can't process. I need to grab the value from the cell, rather than the formula.

However, whenever I apply the value method, it still is getting me the formula. I've tried this:

current_qsubnum = @mainsheet.cell(y_qsubnum, x_current).value

And this:

current_qsubnum = @mainsheet[y_qsubnum, x_current].value

Thank you for any suggestions.

Upvotes: 1

Views: 1990

Answers (1)

Veeresh Honnaraddi
Veeresh Honnaraddi

Reputation: 1051

while looping in to the Excel sheet you can check for class of cell.

worksheet.each do |row| row_value = row[n].value if row[n].is_a?(Spreadsheet::Formula)#get value from formula cell end

Upvotes: 2

Related Questions