Open a Link with Google Apps Script

To open a link in a new tab from an Apps Script code, copy-paste one of the 2 functions available on this page and simply enter the URL when calling the function.

Usage:

linkOpen(url)

Simple Version

First, copy the following function into the script editor:

function linkOpen(url) {
  // Source : https://sheets-pratique.com/en/codes/link-open
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(`<script>window.addEventListener('load',()=>{window.open('${url}'),google.script.host.close()});</script></body></html>`), 'Opening...');
}

You will then simply use the linkOpen function with the URL passed as an argument:

function example() {
  linkOpen('https://sheets-pratique.com');
}
The linkOpen function briefly displays a dialog box to open the link using window.open and then automatically closes the dialog box.

Version with Animation

This other version of the linkOpen function displays a waiting animation for about 1 second before opening the requested page in the new tab:

function linkOpen(url) {
  // Source : https://sheets-pratique.com/en/codes/link-open
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(`<!DOCTYPE html><html><head>
  <style>div{text-align:center}img{width:50px;animation:s 1s infinite linear}@keyframes s{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}</style>
  </head><body><div><img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiB3aWR0aD0iMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZmlsbD0iIzQ1NWZiYiIgZD0iTTEyLDRWMkExMCwxMCAwIDAsMCAyLDEySDRBOCw4IDAgMCwxIDEyLDRaIi8+PC9zdmc+" alt="loading"></div><script>window.addEventListener('load',()=>{setTimeout(()=>{window.open('${url}'),google.script.host.close()},1000)});</script></body></html>`).setWidth(200).setHeight(65), 'Opening...');
}

Preview:

google sheets wait dialog loading link open

To modify the display duration, replace the number 1000 in the code with the desired duration (1000 = 1 second).