Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an Excel formula from server-side Ruby web application

My customer has a coded a function that takes 20 boolean values and comes up with an integer result.

They did it with Excel, as a complex formula.

In a new ROR web application, am I better off recoding the whole thing in Ruby, or somehow calling the Excel formula?

I could try to contact a server-side instance of Excel or headless OpenOffice and communicate with it using JACOB or the OpenOffice API?

like image 432
Nicolas Raoul Avatar asked May 17 '26 08:05

Nicolas Raoul


1 Answers

You can use the spreadsheet gem to read from the Excel file. Try the following code, using a sample file in the same directory called ss.xls which has 1 in A1, 2 in B1, and =A1+B1 in C1.

#gem install spreadsheet

require 'spreadsheet'    
book = Spreadsheet.open('ss.xls')
sheet = book.worksheet('Sheet1')
puts sheet.row(0)[2].value

The value call returns the last value for the cell that was calculated by Excel, with the above example it will output 3.0

Edit: Read more about the gem here: http://spreadsheet.rubyforge.org/GUIDE_txt.html

Edit 2: Of course, if you happen to be on Windows you will not need a gem; you just can use the win32ole library as described here: http://www.ruby-doc.org/docs/ProgrammingRuby/html/win32.html

like image 104
brentvatne Avatar answered May 20 '26 02:05

brentvatne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!