HyperLink
Facade API | Has Paid Plan | Univer Server | Univer on Node.js | Preset |
---|---|---|---|---|
✅ | - | - | ✅ | UniverSheetsHyperLinkPreset |
- The hyperlink feature allows jumping to worksheets and cells, supporting adding, updating, parsing, and removing links.
- Supports inserting hyperlinks within rich text.
- Hyperlinks support sub-sheets, ranges/cells, defined names, and standard links (http, mailto, etc.).
This feature includes the following plugin packages:
@univerjs/sheets-hyper-link
- HyperLink Plugin@univerjs/sheets-hyper-link-ui
- HyperLink UI Plugin
Presets Installation
import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets';
import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core';
import UniverPresetSheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US';
import { UniverSheetsHyperLinkPreset } from '@univerjs/presets/preset-sheets-hyper-link';
import UniverPresetSheetsHyperLinkEnUS from '@univerjs/presets/preset-sheets-hyper-link/locales/en-US';
import '@univerjs/presets/lib/styles/preset-sheets-core.css'
import '@univerjs/presets/lib/styles/preset-sheets-hyper-link.css'
const { univerAPI } = createUniver({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsHyperLinkEnUS
),
},
theme: defaultTheme,
presets: [
UniverSheetsCorePreset(),
UniverSheetsHyperLinkPreset()
]
});
Piecemeal Installation
npm install @univerjs/sheets-hyper-link @univerjs/sheets-hyper-link-ui
import { LocaleType, merge, Univer } from '@univerjs/core';
import { defaultTheme } from "@univerjs/design";
import { UniverSheetsHyperLinkPlugin } from '@univerjs/sheets-hyper-link';
import { UniverSheetsHyperLinkUIPlugin } from '@univerjs/sheets-hyper-link-ui';
import SheetsHyperLinkUIEnUS from '@univerjs/sheets-hyper-link-ui/locale/en-US';
import '@univerjs/sheets-hyper-link-ui/lib/index.css';
import '@univerjs/sheets-hyper-link/facade';
import '@univerjs/sheets-hyper-link-ui/facade';
const univer = new Univer({
theme: defaultTheme,
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
SheetsHyperLinkUIEnUS
),
},
});
univer.registerPlugin(UniverSheetsHyperLinkPlugin);
univer.registerPlugin(UniverSheetsHyperLinkUIPlugin);
Univer on Node.js Piecemeal Installation
npm install @univerjs/sheets-hyper-link
import { LocaleType, Univer } from '@univerjs/core';
import { defaultTheme } from "@univerjs/design";
import { UniverSheetsHyperLinkPlugin } from '@univerjs/sheets-hyper-link';
import '@univerjs/sheets-hyper-link/facade';
const univer = new Univer({
theme: defaultTheme,
locale: LocaleType.EN_US,
});
univer.registerPlugin(UniverSheetsHyperLinkPlugin);
Facade API
To get full defination of facade api, please refer to @univerjs/sheets-hyper-link/facade
and @univerjs/sheets-hyper-link-ui/facade
Build/Parse/Jump HyperLink
const workbook = univerAPI.getActiveWorkbook();
const sheet = workbook.getActiveSheet();
const range = sheet.getRange(0, 0, 1, 1);
// Create a link to jump to a sheet
// The returned link: `#gid=sheetId`
const sheetLink = workbook.createSheetHyperlink(sheet.getSheetId());
// Create a link to jump to a range or cell
// The returned link: `#gid=sheetId&range=A1`
const rangeLink = workbook.createSheetHyperlink(sheet.getSheetId(), range.getRange());
// Or
const rangeLink = workbook.createSheetHyperlink(
sheet.getSheetId(),
{
startRow: 0,
endRow: 0,
startColumn: 0,
endColumn: 0
}
);
// Parse the link, returning information including the link's name, url, and type
const linkInfo = workbook.parseSheetHyperlink('#gid=sheet&range=A1');
console.log(linkInfo);
// 0.5.2+ support, Jump to the link
workbook.navigateToSheetHyperlink('#gid=sheet&range=A1');
Manipulating Hyperlinks in Cells
const workbook = univerAPI.getActiveWorkbook();
const sheet = workbook.getActiveSheet();
const range = sheet.getRange(0, 0, 1, 1);
// Add a link
const url = workbook.createSheetHyperlink(sheet.getSheetId());
let label = '123';
// If the current cell is not empty, the label can be omitted
await range.setHyperLink(url, label);
// Query the hyperlink in the current cell
let links = range.getHyperLinks();
console.log(links);
let linkId = links[0].id;
label = '456';
// 0.5.2+ support, Update the hyperlink, Note that after the update is successful, linkId will become the new id
await range.updateHyperLink(linkId, url, label);
// 0.5.2+ support, Remove the hyperlink
links = range.getHyperLinks();
linkId = links[0].id;
await range.cancelHyperLink(linkId);