[SOLVED] URL for relative path to local file in hyperlink

Discuss the spreadsheet application
Post Reply
User avatar
bertram
Posts: 12
Joined: Sat Mar 17, 2012 4:35 am

[SOLVED] URL for relative path to local file in hyperlink

Post by bertram »

spreadsheet .ods is running from a folder that is two folders under program root
target txt file to open by hyperlink in .ods is 2 folders up and (x) folders down

I need to use =HYPERLINK() formula -- impractical to use Insert > Hyperlink dialog for 100s of links 1 by 1

Code: Select all

address with windows-\
folder .ods source       M:\3_patches\UpdateNotes\Windows7-x86
.ols fullpathname        M:\3_patches\UpdateNotes\Windows7-x86\Windows7-x86_setFiledates.ods

target file              M:\3_patches\Updates\Windows7-x86\Additional\_IE11\IE11-Windows6.1-Updates.txt

absolute address with URL-/)
works ok         file:///M:/3_patches/Updates/Windows7-x86/Additional/_IE11/IE11-Windows6.1-Updates.txt

relative address (two folders up from running .ods source)
fails                   file:///../../Updates/Windows7-x86/Additional/_IE11/IE11-Windows6.1-Updates.txt
where do I go wrong?

(I must be world's worst searcher -- search engines return file after file, none of which addressing this question ... URL to local file)
F1 Help is no help. It says file:/// url should be same as url string given in File Open. File Open opens file open dialog, which does not show relative URL string. I know the folder heirachy, but relative URL is failing ...
Last edited by robleyd on Mon Jun 18, 2018 4:54 am, edited 2 times in total.
Reason: Add green tick [robleyd, Moderator]
Platonic solids: constructed by modulation of rotational action -- the tetrahedron can be constructed by negentropic action of four bundles of rotational energy, where each bundle is quantized by modulation 3.
Windows XP pro SP3, LibreOffice 4.4.5.2
User avatar
RusselB
Moderator
Posts: 6646
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: URL for relative path to local file in hyperlink formula

Post by RusselB »

I don't see anything in the help file that states or implies that a relative address can be used.
All of the examples that I see use an absolute address
This might be worked around to use a generated absolute address by storing the address(es) in a helper column and then referencing the cell address in the helper column for the URL
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: URL for relative path to local file in hyperlink formula

Post by Villeroy »

All paths in AOO, including the hyperlink fields, are relative paths even though they are displayed as absolute paths.
Of course, a literal absolute URL in a formula is absolute. But you can calculate the current document's path:

Code: Select all

=MID(CELL("filename");2;SEARCH("/[^/]+$";CELL("filename"))-1)
with Tools>Options>Calc>Calculate: "Regular expressions in formulas" enabled.
Now you can store only the path/file.xyz portion and concatenate it with the calculated path.
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
User avatar
MrProgrammer
Moderator
Posts: 4903
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: URL for relative path to local file in hyperlink formula

Post by MrProgrammer »

bertram wrote:I need to use =HYPERLINK() formula -- impractical to use Insert > Hyperlink dialog for 100s of links 1 by 1
That's unfortunate. If you've specified option Load/Save → General → Save URLs relative to file system, the Insert → Hyperlink dialog would open the correct relative link after you provide the absolute link during link creation.

With the HYPERLINK function Calc opens the exact absolute link you've specified for the first parameter. However you do have control over what's opened because the first parameter can reference a cell with a formula. Let's arrange the work like this:
X2: Relative link you'd like to use
X3: =SUBSTITUTE(CELL("FILENAME");"'";"")  Current spreadsheet
X4: =LEN(X2)-LEN(SUBSTITUTE(X2;"../";"")) Count of relative level characters in X2 → ../../ is six characters
X5: =LEN(X3)-LEN(SUBSTITUTE(X3;"/";""))   Count of shashes in current spreadsheet file name
X6: =SUBSTITUTE(X3;"/";CHAR(9);X5-X4/3)   Mark end of what relative levels represent with special character
X7: =LEFT(X6;FIND(CHAR(9);X6)-1)          Determine represented levels for current spreadsheet
X8: =SUBSTITUTE(X2;LEFT(X2;7+X4);X7)      Replace relative level directories in X2 with X7
Screen Shot 2018-06-17 at 14.53.33 .png
Then your hyperlink function would be =HYPERLINK(X8). I can imagine this is more complex than you'd prefer. Perhaps you can use Villeroy's simpler implementation of the same basic idea. My picture above shows the cell values I think you'll see on your system.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.

[Tutorial] Ten concepts that every Calc user should know
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
bertram
Posts: 12
Joined: Sat Mar 17, 2012 4:35 am

Re: URL for relative path to local file in hyperlink formula

Post by bertram »

thanks Villeroy and MrProgrammer

first lesson for me was that every URL to a local file must begin with file:/// + drive letter + : + /

URLs in OO are not like in DOS where I can start with a command prompt location, and them go up 1 folder with ../, etc

thus for a URL with driveletter that goes up 2 folders from current location of .ods file

Code: Select all

=MID(CELL("filename"),2,SEARCH("/[^/]+/[^/]+/[^/]+$",CELL("filename"))-1)
thus for me
=CELL("filename")
produces: 'file:///M:/3_patches/UpdateNotes/Windows7-x86/Windows7-x86_setFiledates.ods'#$Sheet6

then expanding Villeroy's formula to find / two directories up
=MID(CELL("filename"),2,SEARCH("/[^/]+/[^/]+/[^/]+$",CELL("filename"))-1)
produces: file:///M:/3_patches/UpdateNotes/

This is just what I'm looking for ... THANKS!

=== edit ===
this might be a little cleaner -- to go up n directories from current folder for ods file,
use REPT("/[^/]+",n+1)&"$") for regex string
thus to go up 2 folders, repeat regex 2+1 = 3 etc.

Code: Select all

to start
=CELL("filename") >>
'file:///M:/3_patches/UpdateNotes/Windows7-x86/Windows7-x86_setFiledates.ods'#$Sheet6

then, to go up 2 directories, like ..\..\ at dos command prompt, then repeat regex 3 times
=MID(CELL("filename"),2,SEARCH(REPT("/[^/]+",3)&"$",CELL("filename"))-1) >>
file:///M:/3_patches/
Platonic solids: constructed by modulation of rotational action -- the tetrahedron can be constructed by negentropic action of four bundles of rotational energy, where each bundle is quantized by modulation 3.
Windows XP pro SP3, LibreOffice 4.4.5.2
Post Reply