[Solved] Calling Calc Functions From Macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
bladeoflight16
Posts: 7
Joined: Wed Mar 04, 2009 10:14 pm

[Solved] Calling Calc Functions From Macro

Post by bladeoflight16 »

Hello.

I have a spreadsheet that will soon have about 20 columns of data, with 99 rows of data each. I'm trying to find a formula to predict the data, so each column of recorded data will have a corresponding column that gives a prediction based on my formula and a couple of parameters (which are located elsewhere in the spreadsheet and I can modify there if necessary) as well as another corresponding column calculating the difference between my formula's prediction and the actual data.

The problem is that I will be modifying the formula itself frequently in an attempt to find the right one. So changing it and copying it to roughly 2000 cells would be a huge pain. It would be far easier to define the function in one place and change it there. I have found out how to use a macro in Basic to define a function with parameters that can be called in the cells, but my formula is at present using some spreadsheet functions. Is there a way to call spreadsheet functions from inside a macro?

If not, is one of the macro languages better than the others for mathematical operations? Also, where can I find something about the mathematical API for whichever language is best?

I do have programming experience (primarily in Java and a tiny bit in C), but I've never worked with any of the languages listed for macro creation. Obviously, if there's a better way to define a spreadsheet function than using a macro, I'm open to that.

Also on a side note, I wasn't sure if this belonged here or in the Calc section, so sorry if it's in the wrong place.
Last edited by bladeoflight16 on Sat Jul 04, 2009 10:18 am, edited 1 time in total.
OOo 3.1.1 on MS Windows 7
User avatar
squenson
Volunteer
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: Calling Calc Functions From Macro

Post by squenson »

Here is an example that uses the MAX calc function:

Code: Select all

Function calc_MAX(vRange)

							' The first argument to callFunction() is the Calc Function Name
							' The 2nd argument is the parameters of that function as an array
	FuncAcc = createunoservice("com.sun.star.sheet.FunctionAccess")

	calc_MAX = FuncAcc.callFunction("MAX", array(vRange))
	
End Function
Put some numbers in A1:A5 and in B1 type =calc_MAX(A1:A5)
LibreOffice 4.2.3.3. on Ubuntu 14.04
bladeoflight16
Posts: 7
Joined: Wed Mar 04, 2009 10:14 pm

Re: Calling Calc Functions From Macro

Post by bladeoflight16 »

Thank you very much. After I fixed my security settings to allow documents in this file's location to execute their macros and discovered that I had to put my function in the document's Standard library to get it to load when I opened the file, that worked perfectly.

Now I have a related problem.

If I edit the function and recompile the macro, the function definition actually changes. I tested this by putting the function into a new cell, and it calculated according to the changed version. However, if the function is already in a cell, the spreadsheet does not realize the function has changed and does not recalculate. It also seems that using Recalculate (F9 or Tools --> Cell Contents --> Recalculate) does not force recalculation either, since the cell contents have not changed. I can close and reopen the document to force full recalculation, but I'd prefer to avoid that. Is there a way to force all cells to recalculate?
OOo 3.1.1 on MS Windows 7
User avatar
squenson
Volunteer
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: Calling Calc Functions From Macro

Post by squenson »

May be I am wrong but does not Shift-F9 or Ctrl-F9 force a full recalculation? Try and let us know!
LibreOffice 4.2.3.3. on Ubuntu 14.04
bladeoflight16
Posts: 7
Joined: Wed Mar 04, 2009 10:14 pm

Re: Calling Calc Functions From Macro

Post by bladeoflight16 »

Ah, thank you. It was both, actually: Ctrl+Shift+F9. Much appreciated!

Edit: It's really surprising that I wasn't able to find that by a simple Google search and a search on this forum.
OOo 3.1.1 on MS Windows 7
User avatar
TheGurkha
Volunteer
Posts: 6482
Joined: Thu Mar 13, 2008 12:13 pm
Location: North Wales, UK.

Re: [Solved] Calling Calc Functions From Macro

Post by TheGurkha »

The best way to search here is to use the advanced search, and sort the results by topic, not by post.
Ubuntu 14.10 Utopic Unicorn, LibreOffice Version: 4.3.3.2
Gurkha Welfare Trust
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Calling Calc Functions From Macro

Post by Villeroy »

The calculation results of the FunctionAccess service depend on several additional properties. Just like many results in a spreadsheet depend on the options in Tools>Options>Calc>Calculation.
http://api.openoffice.org/docs/common/r ... ccess.html
http://api.openoffice.org/docs/common/r ... tings.html [deprecated but without alternative]
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply