[Solved] Accessing current sheet in Calc via Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Hacker John
Posts: 25
Joined: Thu Dec 06, 2007 1:17 pm

[Solved] Accessing current sheet in Calc via Basic

Post by Hacker John »

The documentation shows me how to get a sheet by index or name but there doesn't seem to be a method for accessing the current sheet.

In MSOffice VB I would use something like ThisDocument.ActiveSheet. So I just wondered if there is a similar ThisComponent.CurrentThingummyjig in ooBasic.

I've trawled the Developer index but no joy. Can any kind soul help me?
Last edited by Hacker John on Tue Dec 30, 2008 7:30 pm, edited 1 time in total.
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Accessing current sheet in Calc via Basic

Post by Villeroy »

Search for "ActiveSheet"
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
Hacker John
Posts: 25
Joined: Thu Dec 06, 2007 1:17 pm

Re: Accessing current sheet in Calc via Basic

Post by Hacker John »

Thanks for the hint, Villeroy. I've manged to get the current sheet. My test macro, should anyone else find themselves similarly puzzled is

Code: Select all

sub Test
    Dim  oSheet As Object, oCell As Object
    oSheet=thiscomponent.getcurrentcontroller.activesheet
    oCell=oSheet.getCellByPosition(0,0) 'A1
    oCell.SetString("oops")
    0Cell.CellBackColor = RGB(255,141,56)
End Sub
which turns cell A1 of the current sheet into a fetching orange shade.
User avatar
Lupp
Volunteer
Posts: 3761
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] Accessing current sheet in Calc via Basic

Post by Lupp »

The BASIC code from the answer above

Code: Select all

...
    Dim  oSheet As Object, oCell As Object
    oSheet=thiscomponent.getcurrentcontroller.activesheet
...
gets the sheet which is in the foreground (tab selected with priority).
Does someone also know how to get the sheet (object or name or number) from wich a user defined FUNCTION containing the statements was called? I mean this in the same sense as the function call ISNUMBER(A1) does always reference the cell A1 OF THE SHEET IT'S PLACED ON, not of the active sheet.
On Windows 10: LibreOffice 25.8.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: [Solved] Accessing current sheet in Calc via Basic

Post by MTP »

I'm confused by the question. How would a user interact with any sheet (in order to trigger a function) other than the foreground sheet?

If you need to know the name contained in a cell's formula, you can oCell.getFormula() (which returns a string value) and parse that string to pull out the name.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
User avatar
Lupp
Volunteer
Posts: 3761
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] Accessing current sheet in Calc via Basic

Post by Lupp »

(I'm not completely sure about the terminology.)
A FORMULA will be recalculated if some part of it has changed and either 'AutoCalculate' is ON or a 'Recalculate' is ordered by some action (the user clicking the menu item e.g.). I don't know which object is actually responsible and by which chain of notifications the process is actually triggered, but the fact itself is essential for the working of spreadsheets.
The result (the recalculated cell value) may be made shown by a cell of the active (foreground) sheet that contains an appropriate formula. As I can force recalculation of a cell formula in a not active (not in the foreground or even hidden) sheet by "faking" a change I can see that a function using the above mentioned code to get the name (or number) of the 'ActiveSheet' does really return the ACTIVE (FOREGROUND) SHEET and not the sheet where the FORMULA IS PLACED IN. Thus I cannot get information about the OTHER sheet (Name e.g.) using such a function. But that was what I wanted.
Why should I mind?
Why use =ROW()? At design time I know the row number. But the formula will tell me the correct result (possibly way over a chain of dependencies by formulae) accessible from anywhere even if there were rows inserted / deleted ... and surely independet of which row of the active sheet may be 'active' at the time. In short: I'm looking to get information about "ThisSheet" by my function and not about the "CurrentlyActiveSheet".
Sorry about the verbousness. It seems, lacking the really fitting words I have to use many.
Shall I explain in more detail still what I mean by "faking a change" to trigger a recalculation?
On Windows 10: LibreOffice 25.8.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: [Solved] Accessing current sheet in Calc via Basic

Post by MTP »

So you have one cell that triggers one macro when the cell is recalculated?
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
Post Reply