RobustExcelOle

RobustExcelOle helps controlling Excel. This obviously includes standard tasks like reading and writing Excel workbooks. The gem is designed to manage simultaneously running Excel instances, even with simultanously happening user interactions.

RobustExcelOle deals with various cases of Excel (and user) behaviour, and supplies workarounds for some Excel and JRuby bugs.

It supports handling workbooks across Excel instances by keeping track of workbooks and instances. Conflicts are managed before Excel notices.

Library references are supported.

RobustExcelOle works by sending VBA methods via Win32OlE. Therefore, it runs on Windows only.

Features

RobustExcelOle supports

  • opening and processing workbooks across various Excel instances

  • standard tasks like opening, reading, writing, saving, closing workbooks

  • dealing with simultanously happening user interactions

  • dealing with various cases of Excel and user behaviour

  • managing conflicts when opening workbooks (e.g. blocking or unsaved workbooks)

  • workarounds for Excel bugs and JRuby bugs

  • availability of all VBA methods

  • availability of the Excel constants (in form if Ruby constants: Excel constant.capitalize)

  • all standard Excel file formats (.xlsx, .xls, .xlsm)

  • list objects

  • reopening workbooks after closing them

  • unobtrusively opening workbooks, i.e. opening and processing workbooks while preserving their status, e.g. saved, readonly

  • reading and writing workbooks, worksheets, list objects, ranges, rows, columns, cells

  • naming, adding, and copying ranges and worksheets

  • references to other Excel libraries

  • console for interactive mode

Requirements

Ruby 2.1 or higher.

Installation

You can install the gem by running the command

gem install robust_excel_ole

or by using bundler and putting the following line in your Gemfile.

gem 'robust_excel_ole'

Usage

RobustExcelOle can be used either for scripts

require 'robust_excel_ole'
include RobustExcelOle

or as a console. You can start the console via the command

reo

If you want to start the console under jruby, and if you don’t want to use a version manager to switch between ruby and jruby, then use the command

jreo

The call of the console will include RobustExcelOle for you.

The following examples can be used for both scripts and console. If you have started the console in the gem path, you can just put these examples.

Description

In the following example, we want to open a workbook, modify a cell, save and close the workbook.

Let’s open a workbook.

workbook = Workbook.open 'spec/data/workbook.xls'

Now we have a Workbook object that wraps a WIN32OLE object. That is, you can send any WIN32OLE (VBA) method to it. See docs.microsoft.com/en-us/office/vba/api/excel.workbook#methods.

For example, you can determine the name of the workbook.

workbook.Name 
# => "workbook.xls"

For some common tasks and for considering various complex cases of Excel and user behaviour, more convenient methods are implemented. For example, RobustExcelOle provides methods for reading and writing the contents of ranges, for opening, saving, closing, reopening and unobtrusively opening workbooks, and for setting options.

First we want to make the workbook visible.

workbook.visible = true

This method makes both the Excel instance and the window of the Workbook WIN32OLE object visible.

Let’s read the value of a named cell.

value = workbook['firstcell']
# => "foo"

Now we want to write a new value into this cell.

workbook['firstcell'] = "new"

Then we’ll save the workbook.

workbook.save

More features when opening, modifying, creating, saving and closing workbooks

RobustExcelOle allows unobtrusively reading and modifying workbooks, i.e. accessing workbooks without changing their “status”. The status comprises whether the workbook is open or closed, saved or unsaved, read-only or writable, visible or invisible, whether the calculation mode is manual or automatic, and checking compatibility is done or not done.

Workbook.unobtrusively('spec/data/workbook.xls') do |workbook|
  # do something
end
Workbook.for_modifying('spec/data/workbook.xls') do |workbook|
  # do something
end
Workbook.for_reading('spec/data/workbook.xls') do |workbook|
  # do something
end

You can also create a new, empty workbook.

Workbook.create('spec/data/new_workbook.xls', :visible => true)

Moreover, you can open the workbook using a block, similar to, e.g., File.open.

Workbook.open('spec/data/workbook.xls') do |workbook|
  # do something
end

Once we have a workbook, we can set some options, e.g.

workbook.for_this_workbook(:visible => true, :read_only => false)

We can also open the workbook and provide the options in one step, e.g.

workbook = Workbook.open('spec/data/workbook.xls', :visible => true)

or, using abbreviations,

workbook = Workbook.open('spec/data/workbook.xls', :v => true)

Note that Workbook.open can also connect to workbooks which were not opened via RobustExcelOle (but had been opened before by some user).

For more details about opening workbooks see README_open

We can do a simple save

workbook.save

or save the workbook under a different name.

workbook.save_as('spec/data/new_workbook.xls')

Finally we can close the workbook

workbook.save

or save and close the workbook in one step.

workbook.close(:if_unsaved => :save)

For more details about saving and closing workbooks see README_save_close

Using Excel instances

We can start a new Excel instance with

excel1 = Excel.create

or

excel1 = Excel.new(:reuse => false)

We can also obtain an Excel object by connecting to the already running Excel instance.

excel2 = Excel.current

or

excel2 = Excel.new(:reuse => true)

We close the Excel instance using

excel1.close

Closed Excel instances can be reopened.

excel1.recreate(:reopen_workbooks => true, :visible => true)

Closing all Excel instances is done by

Excel.close_all(:if_unsaved => :forget)

For hard terminating all Excel processes we can use

Excel.kill_all

For more details about opening Excel instances see README_excel

Opening workbooks in several Excel instances

RobustExcelOle enables opening and processing workbooks in several Excel instances. Using more than one Excel process allows, e.g., running a script that operates in one Excel instance, while a user (or another script) modifies workbooks in another Excel instance.

For example, suppose we want to open a workbook.

workbook1 = Workbook.open('spec/data/workbook.xls')

Now we want to open another workbook in a different Excel instance.

workbook2 = Workbook.open('spec/data/different_workbook.xls', :excel => :new)

We can also create a third Excel instance and open another workbook in this instance.

excel1 = Excel.create
workbook3 = Workbook.open('spec/data/another_workbook.xls', :excel => excel1)

A workbook is opened by default in the Excel instance where it was open before most recently.

workbook1.close
workbook1 = Workbook.open('spec/data/workbook.xls')

If this Excel instance is damaged or closed, then options control whether the workbook shall be opened in the current (active), a new or a given Excel instance.

workbook1 = Workbook.open('spec/data/workbook.xls', :default => {:excel => :new})

Without the option :default, the workbook can be forced to be opened in the current, new or given Excel instance, no matter if and where it was opened before, e.g.

workbook2 = Workbook.open('spec/data/workbook.xls', :excel => excel1)

Managing conflicts with unsaved and blocking workbooks

RobustExcelOle enables processing workbooks, while still supporting user’s interactions: The commands enable to open, close, reopen, read, modify, write and save Excel workbooks, without the need of the user’s interaction, and even without the user noticing. Thus, while running a script containing RobustExcelOle commands, the user can open and process workbooks in any Excel instances at any time. RobustExcelOle manages the complex cases of conflicts that might occur such that the user does not need to interfere and the script can continue.

For example, suppose we want to process a list of workbooks. RobustExcelOle allows to rapidly open, manipulate, close and save these workbooks. Now assume, the workbook “workbook.xls” is being processed, while the user has already opened this workbook, has modified but not saved it yet. Excel would prompt a message and ask the user what to do. RobustExcelOle solves this conflict by using an option that states whether the changes of the user should be saved (accepted) or discarded (forgotten) before opening the workbook, e.g.

workbook = Workbook.open('workbook.xls', :if_unsaved => :accept)

Similarly, if the user has opened a workbook that has the same name but a different path, the conflict can be solved via an option.

workbook1 = Workbook.open('spec/data/workbook.xls')
# do something
workbook2 = Workbook.open('spec/data/more/workbook.xls', :if_blocked => :forget)

For more details about opening and closing workbooks in Excel instances see README_open

Operating on worksheets

Assume we have opened a workbook

workbook = Workbook.open('spec/data/workbook.xls')

We access the first worksheet by

worksheet = workbook.sheet(1)

or

worksheet = workbook.sheet('Sheet1')

or

worksheet = workbook.first_sheet

We can read and change the worksheet name.

worksheet.name
# => "Sheet1"
worksheet.name = "new_sheet"

We can copy the first worksheet, name it and add it before the third worksheet.

workbook.add_or_copy_sheet(sheet, :as => "copied_name, :before => workbook.last_sheet)

For more details about processing worksheets see README_sheet

Reading and writing ranges in worksheets

We can define a rectangular range by providing the top left and the bottum down cell.

worksheet.range([1..3,1..4])

We can read the first three cells of the first row

worksheet.row_range(1, 1..3).values   # => ["foo","workbook","sheet1"]

and the third column

worksheet.col_range(3).values   # => ["sheet1", 2.0, 4.0]

We can read the first cell, using

worksheet[1,1].value    # => "foo"

or

worksheet.row_range(1)[0].value    # => "foo"

Then we modify it.

worksheet[1,1] = "hello"

We get the value of a named range

worksheet["firstcell"]    # => "hello"

and set another value to that range.

worksheet["firstcell"] = "new_value"

For more details about reading and writing contents of cells and ranges see README_ranges

Reading and writing list objects

We can define a list object (or table) from scratch.

table = ListObject.new(worksheet, "table 1", [1,1], 3,["Person","Amount"])

This command creates a list object in worksheet named “table 1”, with upper left corner at position [1,1] (first cell), with 3 rows and the columns “Person” and “Amount”.

A row in this table can be accessed with help of [], e.g.

table[1]

Now we can set and get the value of a cell of the table, e.g.

table[1].person = "John"
table[1].person
# => "John"

Likewise we can get a table with help of an existing Win32OlE table.

ole_listobject = worksheet.ListObjects.Item("Table 1")
table = ListObject.new(ole_listobject)
 r4 = t4[1]

r4.person # => r.person = “John”

More things

You can convert some Win32Ole objects into a RobustExcelOle object.

range = sheet.Names.Item("firstcell").to_reo

Examples

You can run the examples included in the directory examples, e.g.

ruby examples\open_save_close\example_unobtrusively.rb

Development

This project RobustExcelOle is work in progress. We are happy to implement further features. So we invite you to send your pull requests. We then strive to realize them as soon as possible. If you have any feedback, or you find use cases that RobustExcelOle does not satisfy, please let us know.

RobustExcelOle is being tested for Excel 2010 and Excel 2013. It can be used for any recent Excel Office version. Most functions should run on Excel 2007 as well.

RobustExcelOle has been optimised with help of the rubocop and the rcov tool.

Support

Please contact us and to report issues and feature requests to github Issues. github.com/Thomas008/robust_excel_ole/issues

Collaborate

Please pull request on github.

Author

thomas [email protected]

License

MIT License. For more imformation, please see LICENSE.