Necessity is the a mother… Wanted to build a prototype Rails application that could interact with complicated Excel spreadsheets Store “input” data in plain old AR models Pass that data to the spreadsheets Recalculate and extract the values of complicated formulas Allow user to download a “live” spreadsheet filled with their data
The Easy Way…not Use the ruby spreadsheet gem! “Reading is easy!” “Writing is easy!” “You should be able to fill in Data to be evaluated by predefined Formulas” Yes, yes I should…but alas, I cannot “A ‘To Do’ in source code != functionality!”
Whoa…spreadscript? Need to manipulate or dynamically generate Excel spreadsheet files? YES! Need to perform complex spreadsheet calculations on data stored in a database? YES!! Tired of being restricted to providing users with CSV files when you really want formulas, formatting, and multiple sheets in a workbook? YES!! YES!! YES!! Want your Perl, Python, or Tcl scripts to have access to the power…? WTF? Perl, Python, Tcl? NOOOOOOOOO!
A different angle Build a formula calculation engine in Ruby Pass the model inputs to the engine Calculate the formula values Populate a spreadsheet with the value… …and while I’m at it write the next Googlebook
Desperation sets in… Use Microsoft Excel Web Services API in Sharepoint to expose a web service that allows me to programmatically access and interact with Excel 2010 spreadsheets stored in a Sharepoint document repository Microsoft? Sharepoint? Web service?
Ask people smarter than me Pragmatic Programmer’s “Rails Studio” Alumni mailing list “Why not use JRuby – there’s probably a really nice Excel spreadsheet library out there in Java” Hmmm…I know Java, corporations love Java, and corporations love Excel…maybe he’s on to something!
JRuby - What? jruby.org - “100% Pure-Java implementation of…Ruby” Instead of the C-based Ruby we all know and love
And the Google says… Search for “java excel spreadsheet”
Does JRuby do Rails? https://github.com/jruby/jruby/wiki/JRubyOnRails You can use JRuby with Ruby on Rails. JRuby gives Rails the power and functionality of the Java Platform, providing it with: Excellent garbage collection for endless uptimes Hotspot profiled dynamic optimizations for great performance Access to the Java ecosphere for additional technology options Deployment to Java application servers for ubiquity
How to get it? rvm install jruby By default it loads as ruby 1.8.7 To get ruby 1.9: export JRUBY_OPTS="--1.9” add an after_use hook (see rvm docs)
One gotcha Mac OS X 10.5.8 – has java 1.5, wasn’t working Download Java 1.6 update from Apple Still not working, java –version = 1.5.0_26 Applications > Utilities > Java Preferences Move Java SE 6 to the top of “Java application versions” Reloads the new version automagically (1.6.0_22 in my case)
Key features for using Java include Java # provides bridge to JVM # and shortcut for core classes require 'poi-3.7-20101029.jar’ # bring in the JARs require 'poi-ooxml-3.7-20101029.jar’ class AnalysisAdapter include_classjava.io.FileInputStream # to use core Java classes include_classjava.io.FileOutputStream include_classJava::org.apache.poi.xssf.usermodel.XSSFWorkbook # to explicitly use classes from JARs
Ruby syntax translation Use Ruby syntax on Java objects: wb_path= Rails.root.join('public', 'spreadsheets', sheet_name) fis= FileInputStream.new(wb_path.to_s); workbook = XSSFWorkbook.new(fis) Instead of the Java style: FileInputStreamfis= new FileInputStream(wb_path.to_s); XSSFWorkbookworkbook = new XSSFWorkbook(fis)