I am trying to access in a macro the URL specified by a HYPERLINK
function in a formula of a spreadsheet. This should work regardless of how the formula is constructed.
Test case
The file Get_Hyperlink.ods (14.4 KB) has example formulæ in A1, A2 and A3, yielding links to https://duckduckgo.com, https://google.com and https://ask.libreoffice.org/ respectively. If the function Get_Hyperlink
worked as desired, positioning the cursor to one of those cells and running macro Tester__Get_Hyperlink
would produce a message saying “Hyperlink is ‘https://duckduckgo.com’”, or one of the other links as appropriate.
Similar questions
The question at Evaluate a HYPERLINK formula's target URL seems to be the same question, but the macro given by Zizi64 in a file GetURL.ods just seems to be about the URL of the document itself.
Proposed solutions
Apache OpenOffice: forum della comunità - [Risolto] copiare solo Link collegamento ipertestuale - (Leggi argomento) and the function CELL_URL
in the [Calc, Basic] Introspective cell functions at Apache OpenOffice Community Forum - [Calc, Basic] Introspective cell functions - (View topic)
access it as cell.getTextfields.getByIndex (0).URL
, by my cell has no Textfields (Count = 0).
ask283622extractURLdemo_1.ods supplied at extract hyperlink from a hyperlinked text to another cell - #4 by anon73440385
accesses it by scanning parts of cell.text, but my cell only has one l1Part, which has 1 l2Part, and both have as String the cell text, and the l2Part has TextPortionType = Text and TextField is (null).
It is obviously not a satisfactory solution to build a formula parser in basic! The only similar approach that would at a pinch be acceptable would be to use an API function that parses a formula much as the UI function Insert Function does, yielding a tree down which one could walk to find the HYPERLINK
call, and extract the value of its second argument.
Experiments
cell.getPropertyValue ("Hyperlink")
yields an empty string.
I also searched in various places such as https://forum.openoffice.org/, https://wiki.openoffice.org/, https://www.openoffice.org/api/docs/, but found nothing.
None of the fields of the cell shown by the debugger seemed to contain anything relevant.