4

Is there a way to add a hyperlink inside a message box of a Google Apps spreadsheet?

I have this code that displays a msgbox.

// The code below will display a message box
Browser.msgBox("Go to this site for help");
}

Is there a way to insert a hyperlink in that message box as well? Something like:

// The code below will display a message box
Browser.msgBox("Go to this site for help" & <a href="www.google.com">Help</a>);
}
PY_
  • 1,189
  • 8
  • 18
  • 29

3 Answers3

13

Google's UI service is deprecated as of Dec. 11, 2014. See here.

You should now use the HTML Service. The code to display a message with a link is below.

var htmlOutput = HtmlService
    .createHtmlOutput('Go to <a href="https://www.google.ca/">this site</a> for help!')
    .setWidth(250) //optional
    .setHeight(50); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Help Dialog Title');

It appears Google Sheets won't run scripts for anyone who opens a public spreadsheet (obviously for security). If you want to see a live version of the dialog, just copy the above code into function onOpen() {} in the script editor, save & refresh the spreadsheet. Otherwise, it looks like the image below.

Help Dialog Example

If you have more HTML than a simple link, you can also create a dialog from an HTML file. In the script editor, select File > New > Html file and name it "index" (or whatever you want and change file name in code).

var html = HtmlService.createHtmlOutputFromFile('index');
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
    .showModalDialog(html, 'Dialog title');
Matt
  • 1,792
  • 5
  • 21
  • 33
  • 2
    Works very nicely, you can add target=_blank if you want the URL to open in a new tab, rather than in the modal – Leo Oct 31 '18 at 09:16
  • Newbie having a problem, added your code (thanks!) to onOpen function in Google Sheets, but it doesn't run when I open the spreadsheet. It DOES run from the script editor when I click "Run" there. I also tried using an alert instead in onOpen, and this runs when opening the spreadsheet. – maxhugen Nov 05 '20 at 00:13
4

This is an example of a popup showing a link to an url

function showurl() {
  var app = UiApp.createApplication().setHeight('60').setWidth('150');
  app.setTitle("Anchor in a popup ;-)");
  var panel = app.createPopupPanel()
  var link = app.createAnchor('This is your link', 'https://sites.google.com/site/appsscriptexperiments/home');
  panel.add(link);
  app.add(panel);
  var doc = SpreadsheetApp.getActive();
  doc.show(app);
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Is there anyway to replace the link with a button that achieves the same result? (opening a new page) – tknickman Apr 18 '14 at 17:20
  • You can create a button inside the pop up UI but not a button 'alone', see this post to see how to "disguise" a link with a button. – Serge insas Apr 18 '14 at 22:53
  • http://stackoverflow.com/questions/22875759/use-button-to-open-url-in-new-window-with-google-appsscript/22884575#22884575 – Serge insas Apr 18 '14 at 22:59
  • 2
    This code is now deprecated, see [my answer below](http://stackoverflow.com/a/40225227/1966294). – Matt Oct 24 '16 at 18:39
3

Sorry. Message boxes do not accept hyperlinks or tags. Plain text only.

ScampMichael
  • 3,688
  • 2
  • 16
  • 23
  • 2
    As a workaround, use the DisplayPopupPanel() in your UiInstance to display the link you want. You can create a "dialog-like" look this way. – mzimmerman May 16 '12 at 17:36
  • Interesting. Do you have any links to an example of this? – PY_ May 23 '12 at 17:09