Search code examples
google-apps-scriptgoogle-sheets

How to add a link to a spreadsheet cell using Google Apps Script


It is possible to add a "link" to a cell in a google spreadsheet through the user interface. I want to do the same using a Google Apps Script. I was expecting there to be a method on the Range class for this but cannot see one. Does anyone know how to do this?


Solution

  • RANGE.setFormula plus HYPERLINK Formula should get you what you want.

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    
    var cell = sheet.getRange("B5");
    cell.setFormula('=HYPERLINK("http://www.google.com/","Google")');