[Solved] Create new function with recorded macro?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
robbyn
Posts: 62
Joined: Sat Jun 26, 2021 10:01 pm

[Solved] Create new function with recorded macro?

Post by robbyn »

I find it very difficult to follow some of the guidance on basic macros. I end up being very muddled and adverse to continuing to read the advice.

I wondered if there is a template that is available for me to copy and paste a recorded macro into a draft function so that it could be called by other functions in a spreadsheet.

If you ask me to give an example of the macro. any recorded macro will do. I just want to be able to run the recorded macro via a function. Reason: other functions can be easily set to trigger the macro if it is wrapped in a (new) function eg

Rem new function = runontime()
=IF( a>b, runontime(), 0)
Last edited by robbyn on Thu Jul 15, 2021 1:06 pm, edited 1 time in total.
Libreoffice v7.2.0.4 on Ubuntu 20.04 Locale en-GB
FJCC
Moderator
Posts: 9270
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: create new function with recorded macro?

Post by FJCC »

Functions cannot modify cells other than the one from which they are called. I think this will prevent using a function in the way you want. I ran a quick test to confirm this but may be someone knows a way around that problem.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: create new function with recorded macro?

Post by Villeroy »

runontime.ods
(12.3 KiB) Downloaded 184 times
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
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Create new function with recorded macro?

Post by Lupp »

I also can hardly imagine a case where it might be reasonable to call a recorded macro from a user function.
However I reworked the example by @Villeroy (not quite slightly) to exemplify real-world problems occurring in the context.

Explanation: From my point of view using functions for calling user SubRoutines mainly (exclusively?) lies in the convenient way to pass parameters. The relevant limitation is that cells of the calling sheet can't be manipiulated this way.
Some versions of LibreOffice had very much relieved the prohibition of such action, but recent versions again are strict concerning this rule. I don't know for sure what caused this change of mind.
aoo105682CallSubByFunction.ods
(17.76 KiB) Downloaded 156 times
Last edited by Lupp on Sun Jul 11, 2021 7:27 pm, edited 1 time in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create new function with recorded macro?

Post by Villeroy »

He wants to run a macro (which performs a paste-special) once a day at a given time of day (in order to save a single click per day). Much trouble about nothing.
 Edit: If I remember correctly, the sheet content is locked until the entire calculation cascade is finished, so the whole thing will not work with his recorded paste-special macro. 
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
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Create new function with recorded macro?

Post by Lupp »

Villeroy wrote:He wants to run a macro (which performs a paste-special) once a day at a given time of day...
How do you know? Where would the clipboard content come from?

Anyway, autodidactic studies might better start with some reading. Realistically it is more likely to be done the example-based way, and studying the fundamentals comes failure-induced.

Remains the question if attempts to learn that way should be supported here.

As far as I am concerned, It's a strange thing, because I feel a kind of "need to write a little program" now and then nearly independent of any intention (not to speak of need) to use it. A mental disorder probably.
Last edited by Lupp on Sun Jul 11, 2021 10:53 pm, edited 1 time in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create new function with recorded macro?

Post by Villeroy »

The drama started here: viewtopic.php?f=9&t=105554

It's all my fault. I should not mention macros when there is no need to run some.
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
robbyn
Posts: 62
Joined: Sat Jun 26, 2021 10:01 pm

Re: Create new function with recorded macro?

Post by robbyn »

Villeroy:
It took me some time to understand how to bring in a recorded macro, but I have it working well.

The logic in your sample spreadsheet is really important for many people who can not program in openoffice basic but need actions to take place in response to a change in their spreadsheet. It avoids the need to understand how to put a macro together in detail, and also can easily make a macro conditional.

Thank you, and all who have commented on this thread.
Libreoffice v7.2.0.4 on Ubuntu 20.04 Locale en-GB
Post Reply