Hyperlinks and Tooltips超链接和工具提示
File Format Support (click to show)
Traditional spreadsheet software, including Excel, support "Cell Links". The entire cell text is clickable.包括Excel在内的传统电子表格软件支持“单元格链接”。整个单元格文本都是可单击的。
Modern spreadsheet software, including Numbers, support "Span Links". Links are applied to text fragments within the cell content. This mirrors HTML semantics.包括数字在内的现代电子表格软件支持“跨度链接”。链接应用于单元格内容中的文本片段。这反映了HTML语义。
Formats | Link | Tooltip | Link Type |
---|---|---|---|
XLSX / XLSM | ✔ | ✔ | Cell Link |
XLSB | ✔ | ✔ | Cell Link |
XLS (BIFF8) | ✔ | ✔ | Cell Link |
XLML | ✔ | ✔ | Cell Link |
ODS / FODS / UOS | ✔ | Span Link | |
HTML | ✔ | ✕ | Span Link |
NUMBERS | ✔ | ✕ | Span Link |
X (✕) marks features that are not supported by the file formats. For example, the NUMBERS file format does not support custom tooltips.标记文件格式不支持的功能。例如,NUMBERS文件格式不支持自定义工具提示。
For "Span Link" formats, parsers apply the first hyperlink to the entire cell and writers apply the hyperlink to the entire cell text.对于“跨度链接”格式,解析器将第一个超链接应用到整个单元格,编写器将超链接应用于整个单元格文本。
Hyperlinks are stored in the 超链接存储在单元格对象的l
key of cell objects. l
键中。The 超链接对象的Target
field of the hyperlink object is the target of the link, including the URI fragment. Tooltips are stored in the Tooltip
field and are displayed when hovering over the text.Target
字段是链接的目标,包括URI片段。工具提示存储在Tooltip
字段中,并在悬停在文本上时显示。
For example, the following snippet creates a link from cell 例如,以下代码片段创建了一个从单元格A1
to https://sheetjs.com with the tip "Find us @ SheetJS.com!"
:A1
到https://sheetjs.com的链接,并提示"Find us @ SheetJS.com!"
:
ws["A1"].l = { Target: "https://sheetjs.com", Tooltip: "Find us @ SheetJS.com!" };
Following traditional software, hyperlinks are applied to entire cell objects. Some formats (including HTML) attach links to text spans. The parsers apply the first link to the entire cell. Writers apply links to the entire cell text.按照传统的软件,超链接应用于整个单元格对象。某些格式(包括HTML)将链接附加到文本跨度。解析器将第一个链接应用于整个单元格。编写器将链接应用于整个单元格文本。
Excel does not automatically style hyperlinks. They will be displayed using the default cell style.Excel不会自动设置超链接的样式。它们将使用默认的单元格样式显示。
Live Example (click to hide)
Extract all links from a file (click to show)
The following example iterates through each worksheet and each cell to find all links. The table shows sheet name, cell address, and target for each link.以下示例遍历每个工作表和每个单元格以查找所有链接。该表显示了每个链接的工作表名称、单元格地址和目标。
Remote Links远程链接
HTTP and HTTPS links can be used directly:HTTP和HTTPS链接可以直接使用:
ws["A2"].l = { Target: "https://docs.sheetjs.com/docs/csf/features/hyperlinks" };
ws["A3"].l = { Target: "http://localhost:7262/yes_localhost_works" };
Excel also supports Excel还支持mailto
email links with subject line:mailto
电子邮件链接,带有主题行:
ws["A4"].l = { Target: "mailto:[email protected]" };
ws["A5"].l = { Target: "mailto:[email protected]?subject=Test Subject" };
Live Example (click to show)
This demo creates a XLSX spreadsheet with a 此演示创建了一个带有mailto
email link. The email address input in the form never leaves your machine.mailto
电子邮件链接的XLSX电子表格。表单中输入的电子邮件地址永远不会离开您的机器。
Local Links本地链接
Links to absolute paths should use the 指向绝对路径的链接应使用file://
URI scheme:file://
URI方案:
ws["B1"].l = { Target: "file:///SheetJS/t.xlsx" }; /* Link to /SheetJS/t.xlsx */
ws["B2"].l = { Target: "file:///c:/SheetJS.xlsx" }; /* Link to c:\SheetJS.xlsx */
Links to relative paths can be specified without a scheme:可以在没有方案的情况下指定到相对路径的链接:
ws["B3"].l = { Target: "SheetJS.xlsb" }; /* Link to SheetJS.xlsb */
ws["B4"].l = { Target: "../SheetJS.xlsm" }; /* Link to ../SheetJS.xlsm */
Relative Paths have undefined behavior in the SpreadsheetML 2003 format. Excel 2019 will treat a 在SpreadsheetML 2003格式中,相对路径具有未定义的行为。Excel 2019将处理..\
parent mark as two levels up...\
父标记为向上两级。
Internal Links内部链接
Links where the target is a cell or range or defined name in the same workbook ("Internal Links") are marked with a leading hash character:目标是同一工作簿中的单元格、区域或定义名称的链接(“内部链接”)用前导哈希字符标记:
ws["C1"].l = { Target: "#E2" }; /* Link to cell E2 */
ws["C2"].l = { Target: "#Sheet2!E2" }; /* Link to cell E2 in sheet Sheet2 */
ws["C3"].l = { Target: "#SheetJSDName" }; /* Link to Defined Name */
Live Example (click to show)
This demo creates a workbook with two worksheets. In the first worksheet:此演示创建了一个包含两个工作表的工作簿。在第一个工作表中:
Cell单元格A1
("Same") will link to the rangeB2:D4
in the first sheetA1
(“相同”)将链接到第一个工作表中的范围B2:D4
Cell单元格B1
("Cross") will link to the rangeB2:D4
in the second sheetB1
(“十字”)将链接到第二个工作表中的范围B2:D4
Cell单元格C1
("Name") will link to the range in the defined nameSheetJSDN
C1
(“名称”)将链接到定义名称SheetJSDN
中的范围
The defined name 定义的名称SheetJSDN
points to the range A1:B2
in the second sheet.SheetJSDN
指向第二张图纸中的范围A1:B2
。
Some third-party tools like Google Sheets do not correctly parse hyperlinks in XLSX documents. A workaround was added in library version 0.18.12.一些第三方工具(如Google Sheets)无法正确解析XLSX文档中的超链接。在库版本0.18.12中添加了一个变通方法。
Tooltips工具提示
Tooltips are attached to hyperlink information. There is no way to specify a tooltip without assigning a cell link.工具提示附加到超链接信息。如果不指定单元链接,就无法指定工具提示。
Excel has an undocumented tooltip length limit of 255 characters.Excel的未记录工具提示长度限制为255个字符。
Writing longer tooltips is currently permitted by the library but the generated files will not open in Excel.库当前允许编写更长的工具提示,但生成的文件不会在Excel中打开。
HTML
The HTML DOM parser1 will process HTML DOM解析器1将处理表中的<a>
links in the table.<a>
链接。
Live Example (click to hide)
This example uses 此示例使用table_to_book
to generate a SheetJS workbook object from a HTML table. The hyperlink in the second row will be parsed as a cell-level link.table_to_book
从HTML表生成SheetJS工作簿对象。第二行中的超链接将被解析为单元格级链接。
The HTML writerHTML编写器2 will generate 将生成<a>
links.<a>
链接。
Live Example (click to hide)
This example creates a worksheet where 此示例创建一个工作表,其中A1
has a link and B1
does not. A1
有链接,而B1
没有。The sheet_to_html
function generates an HTML table where the topleft table cell has a standard HTML link.sheet_to_html
函数生成一个html表,其中左上方的表单元格有一个标准的html链接。
The primary SheetJS DOM parsing methods are主要的SheetJS DOM解析方法是table_to_book
,table_to_sheet
, andsheet_add_dom
table_to_book
、table_to_sheet
和sheet_add_dom
↩HTML strings can be written usingHTML字符串可以使用bookType: "html"
in thewrite
orwriteFile
methods or by using the dedicatedsheet_to_html
utility functionwrite
或writeFile
方法中的bookType: "html"
编写,也可以使用专用的sheet_to_html
实用程序函数编写↩