Hyperlinks, Comments, and Images
The Kendo UI for Vue Spreadsheet lets users enrich the cells with hyperlinks, comments, and images. This article describes how users insert them and how developers set, read, and save them from code.
Inserting Content
Users can add hyperlinks, comments, and images from the Insert tab of the toolbar and, for hyperlinks and comments, from the context menu of a cell.
| Content | Toolbar tool | Context menu | Behavior |
|---|---|---|---|
| Hyperlink | Insert link | Hyperlink | Opens a dialog for the address. The cell shows the link, and clicking it opens the address in a new tab. Clearing the address removes it. |
| Comment | Insert comment | Insert comment | Opens a dialog for the comment text. Clearing the text removes the comment. |
| Image | Insert image | — | Opens a dialog with a drop zone. Drag a JPEG, PNG, or GIF file onto it or use the Select files button, then click OK. The image is placed at the selected cell. |
To delete an image or to change its order, right-click the image and use the context menu. If the selected file is not a valid image, the dialog displays an error message and OK stays disabled.
Allowed Hyperlink Addresses
The Spreadsheet hyperlinks are web addresses. The hyperlink dialog accepts:
- Absolute
httpandhttpsaddresses, such ashttps://www.telerik.com. - Host names without a scheme, such as
www.telerik.comorlocalhost:3000/app. The dialog stores them ashttps://addresses.
Any other address, for example mailto:, tel:, an anchor, a relative path, or a javascript:, data:, vbscript:, or file: address, is rejected by the dialog with the Enter a valid web address. message. The Spreadsheet adds http:// to every address that does not contain ://, so mailto: and tel: links, anchors, and relative paths cannot work as hyperlinks. Links that are set from JSON data or from code are not validated when they are set, but the Spreadsheet does not follow a link with an unsafe scheme when the user clicks it.
To change the text of the validation messages, use the
spreadsheet.invalidLinkAddressandspreadsheet.invalidImagekeys. For more information, see Globalization.
Setting Content from JSON
When you define the sheets through JSON data, set the hyperlinks and comments through the link and comment fields of a cell:
const sheets = [
{
name: 'Contacts',
rows: [
{
cells: [
{
value: 'Telerik',
link: 'https://www.telerik.com',
comment: 'The company website.'
}
]
}
]
}
];
Images are stored separately from the cells. Add the image data to the images field of the workbook, and place each image in a sheet through its drawings field:
const workbook = {
sheets: [
{
name: 'Contacts',
rows: [],
drawings: [
{
topLeftCell: 'D2', // The cell where the top left corner of the image is placed.
offsetX: 10, // The horizontal offset from the cell, in pixels.
offsetY: 5, // The vertical offset from the cell, in pixels.
width: 120,
height: 60,
image: 'logo' // The key of the image in the images field.
}
]
}
],
images: {
logo: 'data:image/png;base64,...' // A data URL or a URL of the image.
}
};
spreadsheetRef.value.fromJSON(workbook);
The Spreadsheet reads the images field only when the workbook is loaded by the fromJSON method, as shown in the demo above. The sheets of the defaultProps do not carry the image data.
Setting Content from Code
Use the link() and comment() methods of a range to read, set, or remove a hyperlink or a comment:
const range = spreadsheetRef.value.activeSheet().range('A1');
range.link('https://www.telerik.com'); // Sets a hyperlink.
range.link(); // Returns the hyperlink.
range.link(null); // Removes the hyperlink.
range.comment('Reviewed by the team.'); // Sets a comment.
range.comment(); // Returns the comment.
range.comment(null); // Removes the comment.
Saving Content
The toJSON() method of the Spreadsheet does not include the image data. To save the images together with the hyperlinks, comments, and the rest of the workbook, use the asynchronous saveJSON() method:
const json = await spreadsheetRef.value.saveJSON();
json.sheets[0].hyperlinks; // [{ ref: 'A1', target: 'https://www.telerik.com' }]
json.sheets[0].drawings; // The images that are placed in the sheet.
json.images; // { logo: 'data:image/png;base64,...' }
// Restore the workbook later.
spreadsheetRef.value.fromJSON(json);
The comments are saved in the comment field of the corresponding cells.