2

I'm not really well-versed with codes and I just wanted to try this out for my team. What I have is just a simple code that pops up an alert message (with OK button) everytime we open our Google Spreadsheet. What I wanted to happen is when we click "OK", it will open a link in a new tab. Here's my "script editor" code:

function onOpen() {
 var ss= SpreadsheetApp.getActiveSpreadsheet();
 var ui = SpreadsheetApp.getUi().alert('Welcome to HAVENLY Offshore Team Workspace! Check out our CONFLUENCE updates today!');
}

Please help me achieve my ultimate goal here, I've been trying to find a solution for hours already. Thanks in advance!

Max Makhrov
  • 17,309
  • 5
  • 55
  • 81
  • not possible like that. build instead a little webapp dialog and have the user click an anchor. show us your attempts at that. – Zig Mandel Nov 30 '16 at 13:37

2 Answers2

1

You can do this if you use a custom dialog instead of an alert. Custom dialogs are much more powerful by allowing you to define your own html. Just create the following files in the script attached to the sheet.

Code.gs

function onOpen() {
  openWelcomeDialog();
}

function openWelcomeDialog() {
  // get the html from the file called "Pages.html"
  var html = HtmlService.createHtmlOutputFromFile('Pages');

  // open the dialog
  SpreadsheetApp.getUi()
    .showModelessDialog(html, "Welcome to HAVENLY Offshore Team Workspace!")
}

Pages.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

    <!-- target="_blank" directs the link to a new blank tab -->
    <a target="_blank" href="http://stackoverflow.com/questions/17711146/how-to-open-link-in-new-tab-on-html">
      Check out our CONFLUENCE updates today!
    </a>
  </body>
</html>

Update You can also close the dialog when the user clicks on the link with an onclick event handler:

<a onclick="google.script.host.close()" target="_blank" href="||url||">
  Check out our CONFLUENCE updates today!
</a>
Joshua Dawson
  • 629
  • 10
  • 17
  • Thnx for the solution. It needs one correction: instead of 'HtmlService.createHtmlOutputFromFile('Welcome')' it should state 'HtmlService.createHtmlOutputFromFile('Pages'). Now I'm looking for a way to have a 'href = URL'-link, where URL is a spreadsheet just created in the previous lines of code in my script. Any suggestions? – user3722096 Dec 29 '16 at 10:38
  • @user Hmmm, it would be good to make the URL dynamic. I'll add an update to the question later today. – Joshua Dawson Dec 29 '16 at 16:05
  • Looking forward to it! Thnx in advance! – user3722096 Dec 29 '16 at 16:15
  • @user Solved, however I think the Dynamic Spreadsheet Link should have its own question. It's related to the current question but doesn't really belong among its answers. – Joshua Dawson Dec 29 '16 at 20:56
  • @user If you want to write the question, I can answer if you notify me. – Joshua Dawson Dec 29 '16 at 20:56
1
function onOpen(){
  confluencePopup()
}

function confluencePopup(){
  // will display the dialog box with html text
  var text = "Welcome to HAVENLY Offshore Team Workspace! Check out our CONFLUENCE updates today!'"
  var url = "https://stackoverflow.com/questions/40888267/need-to-link-a-website-to-the-ok-button"
  var rawHTML = '<a onclick="google.script.host.close()" target="_blank" href="' + String(url) + '">' + text + '</a>'
  var htmlSerivce = HtmlService.createHtmlOutput(rawHTML)
  SpreadsheetApp.getUi()
      .showModelessDialog(htmlSerivce, "Welcome")
}

Change the url to your link, and text to the anchor text you would like. You can then just call this function in your onOpen() function. When run it will display a popup with your anchor text, and link to your page.

You may need to configure permissions to run the pop-up. See Google's documentation for that information. Simply you need to add oauthScopes to your appsscript.json file.

Edit: this can be altered to change the link dynamically with a variable, and does not require an additional file to run.

Jace
  • 36
  • 4