Skip to content

Commit 21053e4

Browse files
committed
Added hyperlink example.
1 parent 39d25a1 commit 21053e4

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

dev/docs/source/_images/hyperlink.png

70.9 KB
Loading

dev/docs/source/example_hyperlink.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.. _ex_hyperlink:
2+
3+
Example: Adding hyperlinks
4+
==========================
5+
6+
This program is an example of writing hyperlinks to a worksheet. See the
7+
:func:`write_url` method for more details.
8+
9+
.. image:: _images/hyperlink.png
10+
11+
.. literalinclude:: ../../../examples/hyperlink.lua
12+
:language: lua
13+

dev/docs/source/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ directory of the ``xlsxwriter`` distribution.
1212

1313
example_hello_world.rst
1414
example_demo.rst
15+
example_hyperlink.rst
1516
example_array_formula.rst
1617
example_merge1.rst
1718
example_defined_name.rst

dev/docs/source/worksheet.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,109 @@ otherwise it will appear as a number::
444444

445445
See :ref:`working_with_dates_and_time` for more details.
446446

447+
worksheet:write_url()
448+
---------------------
449+
450+
.. function:: write_url(row, col, url[, format[, string[, tip]]])
451+
452+
Write a hyperlink to a worksheet cell.
453+
454+
:param row: The cell row (zero indexed).
455+
:param col: The cell column (zero indexed).
456+
:param url: Hyperlink url.
457+
:param format: Optional :ref:`Format <format>` object.
458+
:param string: An optional display string for the hyperlink.
459+
:param tip: An optional tooltip.
460+
461+
The ``write_url()`` method is used to write a hyperlink in a worksheet cell.
462+
The url is comprised of two elements: the displayed string and the
463+
non-displayed link. The displayed string is the same as the link unless an
464+
alternative string is specified.
465+
466+
Both row-column and A1 style notation are supported. See :ref:`cell_notation`
467+
for more details.
468+
469+
The ``format`` parameter is used to apply formatting to the cell. This
470+
parameter is generally required since a hyperlink without a format doesn't look
471+
like a link the following :ref:`Format <format>` should be used::
472+
473+
workbook:add_format({color = "blue", underline = 1})
474+
475+
For example::
476+
477+
link_format = workbook:add_format({color = "blue", underline = 1})
478+
worksheet:write_url("A1", "http://www.lua.org/", link_format)
479+
480+
Four web style URI's are supported: ``http://``, ``https://``, ``ftp://`` and
481+
``mailto:``::
482+
483+
worksheet:write_url("A1", "ftp://www.lua.org/")
484+
worksheet:write_url("A2", "http://www.lua.org/")
485+
worksheet:write_url("A3", "https://www.lua.org/")
486+
worksheet:write_url("A4", "mailto:jmcnamaracpan.org")
487+
488+
You can display an alternative string using the ``string`` parameter::
489+
490+
worksheet:write_url("A1", "http://www.lua.org", link_format, "Lua")
491+
492+
.. Note::
493+
494+
If you wish to have some other cell data such as a number or a formula you
495+
can overwrite the cell using another call to ``write_*()``::
496+
497+
worksheet:write_url("A1", "http://www.lua.org/", link_format)
498+
499+
-- Overwrite the URL string with a formula. The cell is still a link.
500+
worksheet:write_formula("A1", "=1+1", link_format)
501+
502+
There are two local URIs supported: ``internal:`` and ``external:``. These are
503+
used for hyperlinks to internal worksheet references or external workbook and
504+
worksheet references::
505+
506+
worksheet:write_url("A1", "internal:Sheet2!A1")
507+
worksheet:write_url("A2", "internal:Sheet2!A1")
508+
worksheet:write_url("A3", "internal:Sheet2!A1:B2")
509+
worksheet:write_url("A4", "internal:'Sales Data'!A1")
510+
worksheet:write_url("A5", [[external:c:\temp\foo.xlsx]])
511+
worksheet:write_url("A6", [[external:c:\foo.xlsx#Sheet2!A1]])
512+
worksheet:write_url("A7", [[external:..\foo.xlsx]])
513+
worksheet:write_url("A8", [[external:..\foo.xlsx#Sheet2!A1]])
514+
worksheet:write_url("A9", [[external:\\NET\share\foo.xlsx]])
515+
516+
Worksheet references are typically of the form ``Sheet1!A1``. You can also link
517+
to a worksheet range using the standard Excel notation: ``Sheet1!A1:B2``.
518+
519+
In external links the workbook and worksheet name must be separated by the
520+
``#`` character: ``external:Workbook:xlsx#Sheet1!A1'``.
521+
522+
You can also link to a named range in the target worksheet: For example say you
523+
have a named range called ``my_name`` in the workbook ``c:\temp\foo.xlsx`` you
524+
could link to it as follows::
525+
526+
worksheet:write_url("A14", [[external:c:\temp\foo.xlsx#my_name]])
527+
528+
Excel requires that worksheet names containing spaces or non alphanumeric
529+
characters are single quoted as follows ``'Sales Data'!A1``.
530+
531+
Links to network files are also supported. Network files normally begin with
532+
two back slashes as follows ``\\NETWORK\etc``. In order to generate this in a
533+
single or double quoted string you will have to escape the backslashes,
534+
``'\\\\NETWORK\\etc'`` or use a block quoted string ``[[\\NETWORK\etc]]``.
535+
536+
Alternatively, you can avoid most of these quoting problems by using forward
537+
slashes. These are translated internally to backslashes::
538+
539+
worksheet:write_url("A14", "external:c:/temp/foo.xlsx")
540+
worksheet:write_url("A15", "external://NETWORK/share/foo.xlsx")
541+
542+
See also :ref:`ex_hyperlink`.
543+
544+
.. note::
545+
XlsxWriter will escape the following characters in URLs as required
546+
by Excel: ``\s " < > \ [ ] ` ^ { }`` unless the URL already contains ``%xx``
547+
style escapes. In which case it is assumed that the URL was escaped
548+
correctly by the user and will by passed directly to Excel.
549+
447550

448551
worksheet:set_row()
449552
-------------------

0 commit comments

Comments
 (0)