0

I'm attaching a script to a button, that upon click, it opens a brand new tab to the desired link within my script. However, I can't seem to get rid of the dialog box which makes me interact with my link and becomes redundant. How would one achieve this?

I have looked into e.preventDefault() and (window).blur(function() to see if these would lead me to the right direction, but still evokes a dialog box.

function openForm(){

  var html = "<script> window.open('Any Link In Here');</script>";

}

How I expect this to work is by clicking on my button, it'll just open a brand new tab to any desired link I want, however nothing happens.

2 Answers2

0

Why not simply use:

function openForm(){
  var html = "<script> window.open('Any Link In Here', '_blank');</script>";
}
ManUtopiK
  • 4,495
  • 3
  • 38
  • 52
  • When using the Function you provided, I get an error showing "_RefereneError: Window is not defined_" after I have clicked my button. Any idea what this might be? – FeatherDuster Sep 05 '19 at 16:27
  • Mmh, I didn't see the google-sheets tag. You made this script in google doc? – ManUtopiK Sep 05 '19 at 16:32
  • Something odd, so using the updated script seems fine, however, once I attach it to the button, I get the notifications at the top saying Script Finished, however, it will not open any link. Here is a copy of my sheet. Please ask for permission as this is under a work account. https://docs.google.com/spreadsheets/d/1cFlBlVuGgHRD96fqIAGdlQLohCmwjmxILt95KZNJ_2A/edit?usp=sharing – FeatherDuster Sep 05 '19 at 16:45
0

I found the answer. Credit to @Tedinoz for helping me and for finding this link Google Apps Script to open a URL.

In this link 'TheMaster' provided an excellent answer. Two things are working here, an HTML source and a javascript. Within the HTML is var winRef = window.open(url1); which opens the URL in another tab, however a dialog box opens. The next line shows

winRef ? google.script.host.close() : window.alert('Allow popup to redirect you to '+url1) ;

What this seems to do is closes the dialog box within google sheet, which appears to be this section

winRef ? google.script.host.close()

So in short, when setting this up name the HTML file whatever you want, with the Javascript, make sure line 6 on the javascript reflects the same name as the HTML source.

HtmlService.createHtmlOutputFromFile('openUrl').setHeight(50), // 'openUrl is the name of the HTML source, name this whatever. 

Once done, go to your button, attach the script which you name it by the javascript function modalUrl.

Here is the full script. If 'TheMaster' ever sees this, please let me know if I nailed what I said above.

openUrl.html

<!DOCTYPE html>
<html>
  <head>
   <base target="_blank">
    <script>
     var url1 ='https://stackoverflow.com/a/54675103';
     var winRef = window.open(url1);
     winRef ? google.script.host.close() : window.alert('Allow popup to redirect you to '+url1) ;
     window.onload=function(){document.getElementById('url').href = url1;}
    </script>
  </head>
  <body>
    Kindly allow pop ups</br>
    Or <a id='url'>Click here </a>to continue!!!
  </body>
</html>

code.gs

function modalUrl(){
  SpreadsheetApp.getUi()
   .showModalDialog(
     HtmlService.createHtmlOutputFromFile('openUrl').setHeight(50),
     'Opening StackOverflow'
   )
}