Writing formulas to excel spreadsheet

Hi, everyone. I've just started using ruby a couple of days ago, and
I've been using it to read data from text files and write to excel
spreadsheets. I also need to be able to write formulas to spreadsheets,
but when I open the excel file, the formula is in there without having
been evaluated - for example, the cell will appear as "=A1+A58+A114"
instead of whatever the value of that sum happens to be. If I click on
the cell and hit "enter," the formula will evaluate, but it does not do
so automatically.

Is there any way to get ruby to force excel to evaluate? Thanks!

···

--
Posted via http://www.ruby-forum.com/.

Have a look at :rubyonwindows.blogspot.com/search/label/excel

and look for 'formula'

···

--
Posted via http://www.ruby-forum.com/.

Will

I'm still fascinated why you are doing sophisticated things with Excel
(not Open Office) but steadfastly refuse to load it on your computer.
Windows and Excel can be purchased for the price of a monitor. You gain
ownership of software that costs hundreds and hundreds of millions to
develop.

Why fanny around with some Micky Mouse spreadsheet gem when you can have
the Full Monty?

···

--
Posted via http://www.ruby-forum.com/.

Mike Stephens wrote in post #999754:

Have a look at :rubyonwindows.blogspot.com/search/label/excel

and look for 'formula'

...which shows this:

Adding Formulae

emptyRow = 15
worksheet.Range("t#{emptyRow}")['Formula'] =
"=(Q#{emptyRow}+L#{emptyRow}+I#{emptyRow}+S#{emptyRow})"

···

--
Posted via http://www.ruby-forum.com/\.

Even if he had it installed locally, I'm guessing that he would want
to generate the document in code since generating it by hand would be
cumbersome. In addition, the spreadsheet gem works on any platform
(last I checked).

Regards,

Dan

···

On May 20, 3:36 pm, Mike Stephens <rub...@recitel.net> wrote:

Will

I'm still fascinated why you are doing sophisticated things with Excel
(not Open Office) but steadfastly refuse to load it on your computer.
Windows and Excel can be purchased for the price of a monitor. You gain
ownership of software that costs hundreds and hundreds of millions to
develop.

Why fanny around with some Micky Mouse spreadsheet gem when you can have
the Full Monty?

7stud -- wrote in post #999761:

Mike Stephens wrote in post #999754:

Have a look at :rubyonwindows.blogspot.com/search/label/excel

and look for 'formula'

...which shows this:

Adding Formulae

emptyRow = 15
worksheet.Range("t#{emptyRow}")['Formula'] =
"=(Q#{emptyRow}+L#{emptyRow}+I#{emptyRow}+S#{emptyRow})"

Thanks, guys. However, the above seems to require win32ole, for which,
if I'm not mistaken, you need office to be installed on the system.

Is there a way to do this with just the spreadsheet gem (i.e. just
require spreadsheet)?

···

--
Posted via http://www.ruby-forum.com/\.

Daniel Berger wrote in post #999984:

Will

I'm still fascinated why you are doing sophisticated things with Excel
(not Open Office) but steadfastly refuse to load it on your computer.
Windows and Excel can be purchased for the price of a monitor. You gain
ownership of software that costs hundreds and hundreds of millions to
develop.

Why fanny around with some Micky Mouse spreadsheet gem when you can have
the Full Monty?

Even if he had it installed locally, I'm guessing that he would want
to generate the document in code since generating it by hand would be
cumbersome. In addition, the spreadsheet gem works on any platform
(last I checked).

Regards,

Dan

Yep, that's correct - I want the scripts to work across a variety of
platforms, with as little dependence on outside applications and
libraries as possible. This is partly because the scripts I'm writing
may be used by a few others; I don't know about what software they will
or won't have on their systems, and they will likely not be too willing
to resolve too many dependency issues. I do have office installed on one
of my systems, but don't have access to that one right now, and will not
buy another copy just for this task.

The spreadsheet gem meets the above requirements, and it was working
brilliantly until I got to writing formulas to spreadsheets.

···

On May 20, 3:36pm, Mike Stephens <rub...@recitel.net> wrote:

--
Posted via http://www.ruby-forum.com/\.

I looked through the code in the spreadsheet gem a few months ago (2?) looking for this functionality. As far as I could tell, it was not yet possible to *write* formulas to a spreadsheet cell. It looks like that functionality is on the roadmap. Look at the "roadmap" section on the homepage: http://spreadsheet.rubyforge.org/

Looks like formula support isn't slated until version 0.8.0 (and we're on 0.6.x right now).

cr

···

On May 19, 2011, at 11:42 PM, Will James wrote:

7stud -- wrote in post #999761:

Mike Stephens wrote in post #999754:

Have a look at :rubyonwindows.blogspot.com/search/label/excel

and look for 'formula'

...which shows this:

Adding Formulae

emptyRow = 15
worksheet.Range("t#{emptyRow}")['Formula'] =
"=(Q#{emptyRow}+L#{emptyRow}+I#{emptyRow}+S#{emptyRow})"

Thanks, guys. However, the above seems to require win32ole, for which,
if I'm not mistaken, you need office to be installed on the system.

Is there a way to do this with just the spreadsheet gem (i.e. just
require spreadsheet)?

Will James wrote in post #999789:

the above seems to require win32ole, for which,
if I'm not mistaken, you need office to be installed on the system.

I'm intrigued - how are you running Excel to get it to calculate
formulae if it's not on your computer?

As it happens, I don't think win32ole has got anything to do with Office
anyway. It's to do with OLE, which is a Windows feature. Excel happens
to present an OLE object model.

···

--
Posted via http://www.ruby-forum.com/\.

. . . and frankly, you shouldn't have to explain why you're writing code
for something like this, unless it's actually relevant to the problem you
want help solving.

···

On Sat, May 21, 2011 at 11:28:26AM +0900, Will James wrote:

Yep, that's correct - I want the scripts to work across a variety of
platforms, with as little dependence on outside applications and
libraries as possible. This is partly because the scripts I'm writing
may be used by a few others; I don't know about what software they will
or won't have on their systems, and they will likely not be too willing
to resolve too many dependency issues. I do have office installed on one
of my systems, but don't have access to that one right now, and will not
buy another copy just for this task.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

There are other use cases for what you want to do, which is why I'm
interested in any problems you come across and any ways you solve
them. For example: I distrust spreadsheets for making important
calculations (too easy to make errors in obscure cells without
noticing), but they are very useful for displaying data. So I make
some calculations using Ruby (or whatever), and then display the
results on a worksheet page, using formulas to generate some of the
displayed results for the same reasons you give in a later post "it's
good to have the formulas in there, in part so that whoever is viewing
the formulas can follow the process of
how something is derived (without too much effort)". An important part
of what I'm doing is that the worksheets will be usable in even quite
old versions of Excel, so I only want to use elementary features of
Excel.

Actually, the original version of this used Microsoft Excel
VisualBasic for Applications, with all the calculations being done in
VBA, and using VBA to generate the worksheet pages. But I'd rather use
Ruby for the calculations, so I'm currently rewriting it, hence my
interest in what you're doing.

I'm assuming you've looked at things like this
  Optimize VBA
which has a section on using VBA to force calculations: I haven't
tried adapting the VBA code to run from Ruby accessing Excel, but it
should be possible?

As a very orthogonal suggestion: one thing that was worrying me about
my approach was what if I couldn't manage to get Ruby to write
anything directly into Excel. (You haven't got that problem.) But a
possible solution occurred to me: use Ruby (or whatever) to generate a
text file which has a list of cells and values or formulas (and
formatting) to be entered into (or used by) each cell. Then write a
VBA function to read such text files and generate the worksheet(s): a
little messy, but fairly easy to do, and I was much happier once I had
a backup plan if directly accessing Excel through Ruby didn't work.

···

On Sat, May 21, 2011 at 3:28 AM, Will James <ampclj9@hotmail.com> wrote:

Daniel Berger wrote in post #999984:

On May 20, 3:36pm, Mike Stephens <rub...@recitel.net> wrote:

I'm still fascinated why you are doing sophisticated things with Excel
(not Open Office) but steadfastly refuse to load it on your computer.
Windows and Excel can be purchased for the price of a monitor. You gain
ownership of software that costs hundreds and hundreds of millions to
develop.

Even if he had it installed locally, I'm guessing that he would want
to generate the document in code since generating it by hand would be
cumbersome.

Yep, that's correct - I want the scripts to work across a variety of
platforms, with as little dependence on outside applications and
libraries as possible. This is partly because the scripts I'm writing
may be used by a few others; I don't know about what software they will
or won't have on their systems, and they will likely not be too willing
to resolve too many dependency issues. I do have office installed on one
of my systems, but don't have access to that one right now, and will not
buy another copy just for this task.

Mike Stephens wrote in post #999926:

Will James wrote in post #999789:

the above seems to require win32ole, for which,
if I'm not mistaken, you need office to be installed on the system.

I'm intrigued - how are you running Excel to get it to calculate
formulae if it's not on your computer?

As it happens, I don't think win32ole has got anything to do with Office
anyway. It's to do with OLE, which is a Windows feature. Excel happens
to present an OLE object model.

There are alternatives to office (i.e. openoffice) which allow you to
work with excel spreadsheets but don't provide the necessary COM objects
or whatever it is that's needed to use some features of certain
libraries or modules in certain languages. I think to do:

class ExcelConst
end
WIN32OLE.const_load(excel, ExcelConst)

or

excel = WIN32OLE::new('excel.Application')

you do need to have excel installed. I remember in perl, to do stuff
like:

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

you need to have excel on the system. I'm not a professional programmer
(these days, I mostly program to automate a lot of painful data
crunching tasks that would take ages to do by hand), so some of this is
a bit beyond me...

Chuck - thanks. It's too bad that the formula stuff isn't implemented
yet, but could there be some clever workarounds to force or trick excel
into evaluating?

···

--
Posted via http://www.ruby-forum.com/\.

Chad Perrin wrote in post #1000098:

and frankly, you shouldn't have to explain why you're writing code
for something like this, unless it's actually relevant to the problem
you want help solving.

Chad, if you ever want to learn anything,and my experience of you -like
others before me - is you don't, learn this: it is always vital to
understand why you are doing something.

···

--
Posted via http://www.ruby-forum.com/\.

Colin Bartlett wrote in post #1000942:

So I make
some calculations using Ruby (or whatever), and then display the
results on a worksheet page, using formulas to generate some of the
displayed results for the same reasons you give in a later post "it's
good to have the formulas in there, in part so that whoever is viewing
the formulas can follow the process of
how something is derived (without too much effort)".

You are using formulae. Will wanted to change the formulae. (Apologies,
Will, if I came across as criticising you - that wasn't intended. I
fully
understand your problem constraints, which were not evident to start
with).

I'm curious to know why Will would change formulae. If parameters are
changing then he could factor those out as values passed in ( a bit like
your text file approach). If the data structures are changing
(eg variable range sizes) he could let Excel sort that out at run time.
Maybe you could tell us more, Will?

As regards to VBA, personally - for purity - I would aim to avoid it as
it shouldn't add any functionality you couldn't achieve by using a
combination of Ruby and Excel. To avoid being flamed yet again, I do
appreciate that this assumes you are running Ruby in the presence of
Windows and Excel. If you want to create an Excel sheet on a non-Windows
platform, your options are reduced.

···

--
Posted via http://www.ruby-forum.com/\.

Actually, the original version of this used Microsoft Excel
VisualBasic for Applications, with all the calculations being done in
VBA, and using VBA to generate the worksheet pages. But I'd rather use
Ruby for the calculations, so I'm currently rewriting it, hence my
interest in what you're doing.

I'm assuming you've looked at things like this
  Optimize VBA
which has a section on using VBA to force calculations: I haven't
tried adapting the VBA code to run from Ruby accessing Excel, but it
should be possible?

As a very orthogonal suggestion: one thing that was worrying me about
my approach was what if I couldn't manage to get Ruby to write
anything directly into Excel. (You haven't got that problem.) But a
possible solution occurred to me: use Ruby (or whatever) to generate a
text file which has a list of cells and values or formulas (and
formatting) to be entered into (or used by) each cell. Then write a
VBA function to read such text files and generate the worksheet(s): a
little messy, but fairly easy to do, and I was much happier once I had
a backup plan if directly accessing Excel through Ruby didn't work.

Thanks for the good tips and suggestions, Colin. I've never really
looked at VB, and like yourself, would rather stick with ruby, but will
take a look at the method to which you pointed in the link. I guess that
if the VB script forces an after-the-fact evaluation, one dirty solution
would be to have the ruby script (after it has done its part) call the
VB script just to force calculations, but not knowing anything about VB,
I'm not sure about what sort of new issues this would introduce, aside
from having to have some sort of vb interpreter.

I've compared the excel xml for evaluated vs. non-evaluated cells, and
it appears that the spreadsheet gem writes whatever strings you tell it
to write (and the cell is tagged accordingly), whereas excel formulas
have a particular format, such as:
    <Cell ss:Index="14"
     ss:Formula="=(RC[-11]+R[57]C[-11]+R[114]C[-11]+R[171]C[-11]+R[228]C[-11])/5"><Data
      ss:Type="Number">1.5812633999999999E-2</Data></Cell>
    <Cell
which may take a while to implement if one were to, for example, dig
into the underlying module and try to add formula functionality. I don't
think I can fool around with this particular approach, because it risks
dedicating too much time to the tools used to solve the primary problem,
and cuts into the time allocated to the problem.

I'm glad to hear you're working on a ruby solution to this issue, and
hope you let us know when you've got it ready. It's kind of funny - as I
said earlier, I've never used ruby prior to this task, but I'm starting
to get into it to the point that I may end up using it quite a bit. But
I guess at this point, this particular issue has become one of
intellectual curiousity more than necessity.

I'm curious to know why Will would change formulae. If parameters are
changing then he could factor those out as values passed in ( a bit like
your text file approach). If the data structures are changing
(eg variable range sizes) he could let Excel sort that out at run time.
Maybe you could tell us more, Will?

No prob - the task at hand doesn't involve changing formulas, but
dynamically generating them. Specifically, I'm reading in large,
cumbersome text files of output from another program, putting the data
into excel (in a more human-readable format), and dynamically generating
formulas in accordance with the nature of the input.

···

--
Posted via http://www.ruby-forum.com/\.

I do want to learn things, and I agree that it is vital to understand why
you are doing something, but it is *not* vital to explain to some
Microsoft obsessed shitbird why you are using Ruby to interact with an
XLS file rather than doing it "by hand" in Excel.

···

On Sun, May 22, 2011 at 06:14:10AM +0900, Mike Stephens wrote:

Chad Perrin wrote in post #1000098:
> and frankly, you shouldn't have to explain why you're writing code
> for something like this, unless it's actually relevant to the problem
> you want help solving.

Chad, if you ever want to learn anything,and my experience of you -like
others before me - is you don't, learn this: it is always vital to
understand why you are doing something.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Note that the reference was to VBA, which is not *strictly* the same as
VB. Knowing the difference between Visual Basic (or Visual Basic .NET)
and Visual Basic for Applications might help you navigate through search
results to find relevant information.

Both of them are different from VBScript, in case it matters.

···

On Thu, May 26, 2011 at 03:53:48AM +0900, Will James wrote:

Thanks for the good tips and suggestions, Colin. I've never really
looked at VB, and like yourself, would rather stick with ruby, but will
take a look at the method to which you pointed in the link. I guess that
if the VB script forces an after-the-fact evaluation, one dirty solution
would be to have the ruby script (after it has done its part) call the
VB script just to force calculations, but not knowing anything about VB,
I'm not sure about what sort of new issues this would introduce, aside
from having to have some sort of vb interpreter.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Just my 5 cents. What do you need formulas for. Is your data going to be
changed by hand in the future?

If not, why not calculate it by your program.

by
TheR

···

--
Posted via http://www.ruby-forum.com/.

Chad Perrin wrote in post #1000266:

but it is *not* vital to explain to some
Microsoft obsessed shitbird why you are using Ruby to interact with an
XLS file rather than doing it "by hand" in Excel.

The trouble with you Chad is you always manage to miss the point.

No-one on this thread has mentioned doing it by hand. The issue was
whether or not to use Windows OLE rather than a Linux environment, given
that the end-product is for Windows users, and that it is does what Will
wants - unlike his Linux solution.

I'm not Microsoft obsessed. The simple fact is it is the Linga Franca of
the computing world. Linux is only on a tiny minority of desktops. So
it's natural to question why someone would choose to go down a such a
route when Windows is what most other people would be using.

Will has said his colleagues just happen to be avid Linux fans so that's
fair enough.
That answers the question.

I know on this channel I will get the anti-Microsoft lobby. Fortunately
I don't get it at work. Professional programmers grudgingly accept that
.NET has bugs and the usual MS baggage but probably is a better bet than
say Java for mainstream e-business. Sadly people rarely have an opinion
on Ruby.

···

--
Posted via http://www.ruby-forum.com/\.

Chad Perrin wrote in post #1001016:

Note that the reference was to VBA, which is not *strictly* the same as
VB. Knowing the difference between Visual Basic (or Visual Basic .NET)
and Visual Basic for Applications might help you navigate through search
results to find relevant information.

Both of them are different from VBScript, in case it matters.

Thanks, Chad!

···

--
Posted via http://www.ruby-forum.com/\.