Google Apps Script to open a URL


Is there a way to write a google apps script so when ran, a second browser window opens to (or another site of my choice)?

I am trying to come up with a work-around to my previous question here: Can I add a hyperlink inside a message box of a Google Apps spreadsheet

This question is tagged with html redirect google-apps-script hyperlink

~ Asked on 2012-05-24 20:24:26

The Best Answer is


You can build a small UI that does the job like this :

function test(){
function showURL(href){
  var app = UiApp.createApplication().setHeight(50).setWidth(200);
  app.setTitle("Show URL");
  var link = app.createAnchor('open ', href).setId("link");
  var doc = SpreadsheetApp.getActive();;

If you want to 'show' the URL, just change this line like this :

  var link = app.createAnchor(href, href).setId("link");

EDIT : link to a demo spreadsheet in read only because too many people keep writing unwanted things on it (just make a copy to use instead).

EDIT : UiApp was deprecated by Google on 11th Dec 2014, this method could break at any time and needs updating to use HTML service instead!

EDIT : below is an implementation using html service.

function testNew(){

function showAnchor(name,url) {
  var html = '<html><body><a href="'+url+'" target="blank" onclick="">'+name+'</a></body></html>';
  var ui = HtmlService.createHtmlOutput(html)

~ Answered on 2012-05-24 21:15:54


This function opens a URL without requiring additional user interaction.

 * Open a URL in a new tab.
function openUrl( url ){
  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'";"_blank";'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ }'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );

This method works by creating a temporary dialog box, so it will not work in contexts where the UI service is not accessible, such as the script editor or a custom G Sheets formula.

~ Answered on 2017-11-03 14:49:36

Most Viewed Questions: