Get Cell from inside of Custom formula

Is it possible to get the cell from inside of a custom formula. I can’t use CurrentSelection because that will not work when the formula is being re-calculated.

So, from inside of the formula I would like to find the cell that holds the formula.

class CustomFn(unohelper.Base, XPy):
    def __init__(self, ctx):
        self.ctx = ctx
        self._info_provider = CustomInfoProvider()

    def custom_fn(self) -> str:
        this_cell = # What goes here to get the cell that is being calculated?
        # I need to know the current cell address.
        # This next line is not exactly what I am doing but conveys the idea.
        info = self._info_provider.get_info_for_cell(this_cell)
        return info

def createInstance(ctx):
    return CustomFn(ctx)

It is not looking good from AI’s point of view. Copilot says it is not possible.

Copilot

In LibreOffice Calc, when creating a custom function (also known as a User-Defined Function or UDF), it’s not straightforward to get the reference to the cell that called the function directly within the function itself. This is because the function is designed to be stateless and not aware of the context in which it is called.

However, there is a workaround that involves passing the cell reference as an argument to the function. You can modify the function to accept additional parameters that represent the current cell’s coordinates. Here’s an example of how you might adjust your py function to accept the cell address as parameters:

Python

class Py(unohelper.Base, XPy):
    # ... other parts of the class ...

    def py(self, cell_address=None):
        if cell_address:
            # Process the cell_address which is a tuple (Sheet, Column, Row)
            sheet, column, row = cell_address
            # Now you can use the sheet, column, and row to get the cell value or perform other actions
            # ...
        else:
            # Handle the case where no cell address is provided
            # ...
        return # the value you want to return

AI-generated code. Review and use carefully. .

To use this function in Calc, you would enter the following formula in a cell:

=PY(CELL("address"))

The CELL("address") function returns the address of the current cell, which is then passed to your py function.

Please note that this approach requires the user to manually enter the cell reference when using the function. If you’re looking for a way to automatically detect the calling cell without user intervention, this might not be possible due to the limitations of the API and the way Calc functions are evaluated.

For more advanced manipulation and if you need to get the calling cell programmatically, you might need to consider creating a macro that runs on a specific event or trigger that can access the current selection or active cell. This would be outside the scope of a simple UDF and would require a different approach to interacting with the Calc document. Remember to always test your code thoroughly to ensure it behaves as expected in all scenarios.

You

Is there a way to get the calling cell without user input?

Copilot

Unfortunately, LibreOffice Calc does not natively support retrieving the calling cell’s address from within a custom function without user input. Custom functions in Calc are designed to be stateless and are not aware of the context in which they are called.

However, there are some advanced methods that involve using macros or scripting to interact with the document and possibly retrieve the active cell or selection. These methods are more complex and typically require a deeper understanding of the LibreOffice API and macro programming.

One approach could be to use a macro that runs on specific events or triggers, which can access the current selection or active cell. This macro could then potentially pass the cell reference to your custom function. But this would be outside the scope of a simple User-Defined Function (UDF) and would require a different approach to interacting with the Calc document.

For example, you could create a service that calls Calc functions and retrieves the active cell:

Python

def get_active_cell():
    # Obtain the current component
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    # Check if the current component is a Calc document
    if hasattr(model, "Sheets"):
        sheet = model.CurrentController.ActiveSheet
        cell = model.CurrentController.getSelection()
        # Check if the selection is a single cell
        if cell.supportsService("com.sun.star.sheet.SheetCell"):
            return cell
    return None

AI-generated code. Review and use carefully. .

This macro could be triggered by an event, such as selecting a cell or changing the content of a cell, and then it could interact with your py function.

Please note that implementing such a solution requires careful consideration of the document’s behavior and user experience, as it may not be as straightforward as using a built-in function. Always ensure to test your code thoroughly to make sure it behaves as expected in all scenarios. If you’re not familiar with macro programming in LibreOffice, it might be beneficial to consult the or seek assistance from the LibreOffice community forums or other experienced developers.

so far youre like boilerplate (in case its your own) ?!

ok here is a real reason to dive into!!

this site provides good example
https://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/AddIn.html
https://forum.openoffice.org/en/forum/viewtopic.php?t=56916&hilit=sort+addin
from german forum

I started with that article some time ago. I am not seeing how it helps answer my question.


Is is possible to get the cell that the formula is being executed for without using CurrentSelection?

may be you need to declare:

::com::sun::star::beans::XPropertySet
for a ::com::sun::star::beans::XPropertySet interface to the SpreadsheetDocument making the function call. Only one parameter of this type is allowed in each function. It can be used to query document settings like SpreadsheetDocumentSettings::NullDate.

as first Argument in the corresponding XPy.idl ???

Do you have more information on this? I am not clear on what you are pointing at here.

no, my searchengine is not better as yours!