Search code examples
libreoffice-calc

LibreOffice Calc: How to get file path of the current ods document into the cell?


I would like to get current document folder path as a cell value. I need it for proper relative reference creation used in a function call OFFSET(INDIRECT(<generated filepath#sheet.cell>))

I am out of luck with google. I found basic function ThisComponent.getURL() that could help.

The nearest solution is via the reference to other file in the same folder with use of function FORMULA() over that cell an then using string operations over the result.

I would expect some simple function like INFO() but I could not find any.

Thanx


Solution

  • Add this function to the macro library:

    Function GetCurrentFolder
        sUrl = ThisComponent.getURL()
        sParts = Split(sUrl, "/")
        ReDim Preserve sParts(0 to UBound(sParts) - 1)
        GetCurrentFolder = Join(sParts, "/")
    End Function
    

    Then put this in the spreadsheet formula:

    =GETCURRENTFOLDER()