Hi,
=HYPERLINK is a formula, same as if you had "=A1 + A2" in the cell. If you read the cell, you will get a TFormula struct which has:
The url is one of the parameters of the formula, but not the formula result. I mean, in the spreadsheet below:
The formula result for the cell is "hello", not the hyperlink. You could also have more complex formulas, like
=HYPERLINK("https://tmssoftware.com","hello") & HYPERLINK("https://tmssoftware.com","bye")
And what would be the hyperlink there? (just for the record, if you click the cell in Excel it will go to https://tmssoftware.comhttps/tmssoftware.com
). You might even have a =if() there that only shows the hyperlink in some cases.
The solutions I see are:
- Quick and dirty: Look at the formula text, search for =Hyperlink and get the url after that. It will break in a lot of corner cases, but the common one will work. Also note that there must be a second parameter to =HYPERLINK for this to be a problem: To read a hyperlink like
=HYPERLINK("https://test.com")
you can get it from the formula result.
- If you want to do it more professionally, you can use Tokens (yes, we got you covered
)
Note that while the example uses GetFormulaTokens, you can use GetTokens if you already have the text you want to parse.
As mentioned in the link above, formulas are stored in RPN notation. So you don't have a TTokenFunction where you have a property .Arguments
, but instead, in the stack, you have the Arguments and apply the Function to that.
To play with tokens, the simplest way will be to play with them: use the code in the example above to read a formula with the hyperlink and see the tokens it has.
For example, if you try the following code:
var xls = new XlsFile(1, true); var Tokens = xls.GetTokens(1, "=HYPERLINK(\"https://store.icrealtime.com/IPEG-B20F-IRW4\",\"ICR | Store_IPEG-B20F-IRW4\")");
You'll see the following result.
The important thing to notice is that the arguments go "in reverse", that is, the first argument is the more far away from the function token. The stack is:
Argument1 -> The one you want
Argument2
FunctionToken with name = "HYPERLINK", and 2 arguments.
But you can also have Hyperlink with 1 argument, in which case the argument most near the function would be the one you want. Basically, you have to scan Tokens for a HYPERLINK function, and when found at position i, look at Tokens[i - ArgumentCount]