React Spreadsheet Hyperlinks
The React Spreadsheet component allows display of pre-existing hyperlinks in your Excel workbook as well as insertion of new ones that can link to websites, file directories, and even other worksheets in the workbook.
React Spreadsheet Hyperlinks Example
import { saveAs } from "file-saver" ;
import { Workbook } from "@infragistics/igniteui-react-excel" ;
import { WorkbookFormat } from "@infragistics/igniteui-react-excel" ;
import { WorkbookSaveOptions } from "@infragistics/igniteui-react-excel" ;
import { WorkbookLoadOptions } from "@infragistics/igniteui-react-excel" ;
import { IgrExcelXlsxModule } from "@infragistics/igniteui-react-excel" ;
import { IgrExcelCoreModule } from "@infragistics/igniteui-react-excel" ;
import { IgrExcelModule } from "@infragistics/igniteui-react-excel" ;
IgrExcelCoreModule.register();
IgrExcelModule.register();
IgrExcelXlsxModule.register();
export class ExcelUtility {
public static getExtension(format: WorkbookFormat): string {
switch (format) {
case WorkbookFormat.StrictOpenXml:
case WorkbookFormat.Excel2007:
return ".xlsx" ;
case WorkbookFormat.Excel2007MacroEnabled:
return ".xlsm" ;
case WorkbookFormat.Excel2007MacroEnabledTemplate:
return ".xltm" ;
case WorkbookFormat.Excel2007Template:
return ".xltx" ;
case WorkbookFormat.Excel97To2003:
return ".xls" ;
case WorkbookFormat.Excel97To2003Template:
return ".xlt" ;
}
}
public static load(file: File): Promise <Workbook> {
return new Promise <Workbook>((resolve, reject ) => {
ExcelUtility.readFileAsUint8Array(file).then((a ) => {
Workbook.load(a, new WorkbookLoadOptions(), (w ) => {
resolve(w);
}, (e ) => {
reject(e);
});
}, (e ) => {
reject(e);
});
});
}
public static loadFromUrl(url: string ): Promise <Workbook> {
return new Promise <Workbook>((resolve, reject ) => {
const req = new XMLHttpRequest();
req.open("GET" , url, true );
req.responseType = "arraybuffer" ;
req.onload = (d): void => {
const data = new Uint8Array (req.response);
Workbook.load(data, new WorkbookLoadOptions(), (w ) => {
resolve(w);
}, (e ) => {
reject(e);
});
};
req.send();
});
}
public static save(workbook: Workbook, fileNameWithoutExtension : string ): Promise <string > {
return new Promise <string >((resolve, reject ) => {
const opt = new WorkbookSaveOptions();
opt.type = "blob" ;
workbook.save(opt, (d ) => {
const fileExt = ExcelUtility.getExtension(workbook.currentFormat);
const fileName = fileNameWithoutExtension + fileExt;
saveAs(d as Blob, fileName);
resolve(fileName);
}, (e ) => {
reject(e);
});
});
}
private static readFileAsUint8Array(file: File): Promise <Uint8Array > {
return new Promise <Uint8Array >((resolve, reject ) => {
const fr = new FileReader();
fr.onerror = (e): void => {
reject(fr.error);
};
if (fr.readAsBinaryString) {
fr.onload = (e): void => {
const rs = (fr as any ).resultString;
const str: string = rs != null ? rs : fr.result;
const result = new Uint8Array (str.length);
for (let i = 0 ; i < str.length; i++) {
result[i] = str.charCodeAt(i);
}
resolve(result);
};
fr.readAsBinaryString(file);
} else {
fr.onload = (e): void => {
resolve(new Uint8Array (fr.result as ArrayBuffer ));
};
fr.readAsArrayBuffer(file);
}
});
}
}
ts コピー import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { ExcelUtility } from './ExcelUtility' ;
import { IgrExcelXlsxModule } from "@infragistics/igniteui-react-excel" ;
import { IgrExcelCoreModule } from "@infragistics/igniteui-react-excel" ;
import { IgrExcelModule } from "@infragistics/igniteui-react-excel" ;
import { IgrSpreadsheetModule } from "@infragistics/igniteui-react-spreadsheet" ;
import { IgrSpreadsheet } from "@infragistics/igniteui-react-spreadsheet" ;
IgrExcelCoreModule.register();
IgrExcelModule.register();
IgrExcelXlsxModule.register();
IgrSpreadsheetModule.register();
export default class SpreadsheetHyperlinks extends React.Component {
public spreadsheet: IgrSpreadsheet;
constructor (props: any ) {
super (props);
this .onSpreadsheetRef = this .onSpreadsheetRef.bind(this );
}
public render (): JSX .Element {
return (
<div className ="container sample" >
<IgrSpreadsheet ref ={this.onSpreadsheetRef} height ="100%" width ="100%" />
</div >
);
}
public onSpreadsheetRef(spreadsheet: IgrSpreadsheet) {
if (!spreadsheet) { return ; }
this .spreadsheet = spreadsheet;
const url = "https://static.infragistics.com/xplatform/excel/Hyperlinks.xlsx" ;
ExcelUtility.loadFromUrl(url).then((w) => {
this .spreadsheet.workbook = w;
});
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<SpreadsheetHyperlinks /> );
tsx コピー
Like this sample? Get access to our complete Ignite UI for React toolkit and start building your own apps in minutes. Download it for free.
Hyperlinks Overview
Hyperlinks are added to the IgrSpreadsheet
control by accessing the Hyperlinks
collection on the worksheet that you want to place the hyperlink on. This collection has an Add
method that takes a WorksheetHyperlink
object, where you can define the cell address, the hyperlink URL to be navigated to, the display text, and a tooltip to optionally be displayed on hover.
Dependencies
When setting up your React spreadsheet control to use hyperlinks, you will need to import the WorksheetHyperlink
class like so:
import { WorksheetHyperlink } from 'igniteui-react-excel' ;
ts
Code Snippet
The following code snippet demonstrates how to add a hyperlink to the currently viewed worksheet in the React IgrSpreadsheet
control:
this .spreadsheet.activeWorksheet.hyperlinks().add(new WorksheetHyperlink("A1" , "http://ko.infragistics.com" , "Infragistics" , "Infragistics Home Page" ));
ts
API References