Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically add a hyperlink to a cell in a worksheet using office-js?

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

like image 732
Eric Haas Avatar asked Jun 30 '16 14:06

Eric Haas


People also ask

How do you insert a hyperlink into a worksheet cell?

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.

How fetch data from Excel in JavaScript?

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.


2 Answers

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

like image 133
Michael Zlatkovsky - Microsoft Avatar answered Oct 24 '22 15:10

Michael Zlatkovsky - Microsoft


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

  • OfficeJS Docs for RangeHyperlink
like image 34
S.Feederle Avatar answered Oct 24 '22 15:10

S.Feederle