1

I have very basic knowledge of google spreadsheet scripts.

Right now I am looking for some function for open an external URL by clicking on a button. this is the link what i want to open from a sheet by clicking on a button: http://h22235.www2.hp.com/hpinfo/globalcitizenship/environment/productdata/Countries/us/ii_q4135b_us_eng_v18.pdf

Thanks for any help

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Adrienn Tóth
  • 21
  • 1
  • 1
  • 2
  • Welcome. Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, [describe the problem](https://meta.stackoverflow.com/q/254393/1595451) and what has been done so far to solve it. – Rubén May 02 '18 at 16:21

1 Answers1

2

This sounds simple but it actually quite a tricky requirement.

Google Apps Script will not automatically open web pages. You can create a HYPERLINK and your user could click on it but you don't seem to be able to initiate it automatically. If you record a macro for this operation it just has the activate() function for the Cell but doesn't have the line that opens the new browser.

The obsolete suggested solutions are to open a custom dialog that has a link or a button in it that the user can click on to open the new tab and this can be done using the HtmlService.

The third answer to this question has code to create a dialog with a link in it and this code works well.

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's not exactly what was requested as it requires the user to click on the link and they already clicked on a button or something else to get to the dialog.

It would seem simple for you to write some javascript into the dialog to automatically call window.open() and this is what is proposed here.

function openTab() {
  var selection = SpreadsheetApp.getActiveSheet().getActiveCell().getValue();

  var html = "<script>window.open('" + selection + "');google.script.host.close();</script>";
  var userInterface = HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Tab');
}

This code creates a dialog with a script that opens the link that it read from the Sheet and then calls google.script.host.close() to get rid of the dialog. Unfortunately, this doesn't appear to work and it took a while to figure out why.

The answer is that popup blockers in most popular browsers will only allow a new window to be opened if it is opened as a result of code running from a direct user action such as a click.

You can verify this by turning off Popup blocking in chrome and the code works fine.

It is highly unlikely that you can convince your users to turn off popup blocking so I don't think there is a way for you have a button that opens a new browser tab. The best I think you can do is to have a button that opens a dialog with a button or a link that will open the new browser tab.

Aidan
  • 1,550
  • 1
  • 13
  • 20