开发者

Calling an Excel formula from server-side Ruby web application

开发者 https://www.devze.com 2023-03-10 18:21 出处:网络
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.

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 for开发者_开发知识库mula?

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


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


No answers, so for now I will guess that re-implementing the formula in Ruby is less difficult than calling Excel/OpenOffice.

Feel free to provide a better answer!

0

精彩评论

暂无评论...
验证码 换一张
取 消