I'm working on an Excel add-in using the JavaScript APIs to build add-ins in Excel 2016.
The problem I have is not to place the url/link in the cell - I rather want to make this url clickable (as you may know it from entering a url into a cell and hit ).
In VBA the solution was this (e.g.):
With Worksheets(1)
.Hyperlinks.Add .Range("E5"), "http://example.microsoft.com"
End With
Unfortunately, I can't find a hyperlink function in the JavaScript API. Any idea?
Thanks a lot for any help and best regards Eric
On a worksheet, select the cell where you want to create a link. On the Insert tab, select Hyperlink. You can also right-click the cell and then select Hyperlink... on the shortcut menu, or you can press Ctrl+K. Under Display Text:, type the text that you want to use to represent the link.
Javascript Excel - Import Data From ExcelThe Excel file is read into Uint8Array object, which is then passed to the load method exposed by the Excel library. Once the worksheet is loaded into the Excel library object, we can read each cell value and build a JSON array that will be used as the igGrid data source.
There are two types of hyperlinks in Excel, one that you do in VBA through Hyperlinks.add, and another that you do via a formula. The latter is easily supported by the Excel JavaScript object model.
Excel.run(function (ctx) {
var firstCellInSelection = ctx.workbook.getSelectedRange().getCell(0, 0);
firstCellInSelection.formulas = [['=HYPERLINK("http://www.bing.com")']];
return ctx.sync();
}).catch(function (error) {
console.log(error);
});
~ Michael Zlatkovsky, Developer on Office Extensibility Team, MSFT
Adding hyperlinks is available in the beta for OfficeJS as of now. You can load the beta version with...
<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/beta/hosted/Office.js"></script>
...and get/set hyperlinks with
Set a hyperlink in cell A1:
Excel.run((context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
const range = sheet.getRange('A1');
range.hyperlink = {
address: `mailto:[email protected]`,
documentReference: null,
screenTip: null,
textToDisplay: 'Send me a Mail!',
};
return context.sync();
})
Get hyperlink from cell A1:
Excel.run((context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
const range = sheet.getRange('A1').load('values, hyperlink');
return context.sync().then(() => console.log(range.hyperlink));
})
API Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With