SpreadJS v13.1 includes enhanced hyperlink support. In previous versions of SpreadJS, hyperlinks only opened URL links, and could not be exported to Excel. Our latest update addresses these issues and more.
In addition to Excel compatibility, we've added options to create different types of hyperlinks:
In the following tutorial, we import an Excel file and add different types of hyperlinks with SpreadJS v13.1.
Read the full SpreadJS v13.1 release.
Create an HTML file with SpreadJS references in it.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head
meta charset="utf-8" />
<title>SpreadJS Hyperlinks</title>
<link href=" ./node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css" rel="
stylesheet" type="text/css" />
<script type="text/javascript" src=" ./node_modules/@grapecity/spread-sheets/dist/gc.spread.all.min.
js"></script>
<script type="text/javascript" src=" ./node_modules/@grapecity/spread-excelio/dist/gc.spread.excelio.min.js">
</script>
</head>
<body>
<div id="spreadSheet" style="width: 1300px; height: 800px; border: 1px solid gray"></div>
</body>
</html>
Install the SpreadJS NPM files
npm install @grapecity/spread-sheets @grapecity/spread-sheets-excelio
This action installs the latest SpreadJS files in the project and makes those references in the HTML page work correctly.
Import an Excel template into SpreadJS.
For simplicity, we've created a JS file from the Excel template using the SpreadJS Designer and included it in the demo zip file (at the end of this post).
To load this file in the HTML page, add the following line:
<script type="text/javascript" src="./ExcelTemplate.js"></script>
And add script code to initialize the SpreadJS instance and load that template into it:
<script>
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementByID("spreadSheet"),{ sheetCount: 1});
spread.fromJSON(ExcelTemplate);
var sheet = spread.getActiveSheet();
}
</script>
var sheetJSONString = JSON.stringify(sheet.toJSON());
We have set up the HTML page, and are ready to add the new hyperlinks to SpreadJS.
In this case, we want the link to appear in the cell next to "Employer Website" in the above template.
//URL
sheet.setValue(2, 2, "GrapeCity")
sheet.setHyperlink(2, 2, { url: "https://www.grapecity.com"});
When clicked, it opens the system's default mail application and creates an email draft addressed to your specified recipient.
//Email Address
sheet.setValue(2, 6, "us.sales@grapecity.com")
sheet.setHyperlink(2, 6, { url: "us.sales@grapecity.com"});
Like URL hyperlinks, the email value doesn't have to match the email address.
Automatically navigates to a specified cell and sheet in the workbook.
sjs://<Sheet Name><Cell Reference>
//Sheet Location
sheet.setValue(10, 4, "Personal Totals");
sheet.setHyperlink(10, 4, { url: "sjs://Personal Monthly Budget!G62:J67"});
Below we will create a function for a new custom command and generate a budget sheet. This function accomplishes the following:
//Custom Command
sheet.setValue(11, 4, "New Minthly Budget");
sheet.setHyperlink(11, 4, {
command: function (sheet) {
spread.addSheet(spread.sheets.length, new GC.Spread.Sheets.Worksheet ());
var newSheet = spread.getSheet(spread.sheets.length - 1);
newSheet.fromJSON(JSON.parse(sheetJSONString));
newSheet.name("Personal Monthly Budget " + (spread.sheets.length - 1));
spread.setActiveSheetIndex(spread.sheets.length-1);
addHyperlinks(spread, sheetJSONString);
}
});