Open Link in a Window Using Script (Google Sheet)

Google Apps Script to open a URL

You can build a small UI that does the job like this :

function test(){
showURL("http://www.google.com")
}
//
function showURL(href){
var app = UiApp.createApplication().setHeight(50).setWidth(200);
app.setTitle("Show URL");
var link = app.createAnchor('open ', href).setId("link");
app.add(link);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}

If you want to 'show' the URL, just change this line like this :

  var link = app.createAnchor(href, href).setId("link");

EDIT : link to a demo spreadsheet in read only because too many people keep writing unwanted things on it (just make a copy to use instead).

EDIT : UiApp was deprecated by Google on 11th Dec 2014, this method could break at any time and needs updating to use HTML service instead!

EDIT :
below is an implementation using html service.

function testNew(){
showAnchor('Stackoverflow','http://stackoverflow.com/questions/tagged/google-apps-script');
}

function showAnchor(name,url) {
var html = '<html><body><a href="'+url+'" target="blank" onclick="google.script.host.close()">'+name+'</a></body></html>';
var ui = HtmlService.createHtmlOutput(html)
SpreadsheetApp.getUi().showModelessDialog(ui,"demo");
}

Google Script to open URL in same window as Spreadsheet

According to the setTarget documentation:

"(setting a target to _self) will only work if the app is being shown as a standalone service. It will have no effect when called inside a dialog, such as in a Google Spreadsheet."

Apps Script intentionally prevents scripts from being able to switch the view of the current window, as a security measure.

Google Apps Script to open a URL based on a cell

From I would like to use that cell as a reference to my script (which opens url on a button). and How can I modify this script in order to get the link from the B2 cell?, when your script is modified, how about the following modification?

From:

function openUrl( url="https://google.com" ){

To:

function openUrl(){
var url = SpreadsheetApp.getActiveSheet().getRange("B2").getValue();

References:

  • getRange(a1Notation)
  • getValue()

Google Script for opening a URL in dedicated browser (Internet Explorer) by click in Spreadsheets

Although it's not possible to force open links in specific browser, it is possible to detect the user agent string as the previous answer states. You can use use HtmlService.getUserAgent():

function testInternetExplorer(){
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
var ui = SpreadsheetApp.getUi();
var response = ui.alert('This app link is available only on Internet Explorer.\n Proceeding to use outdated browsers may cause serious damage to your device, data and privacy.\nAre you sure you want to continue?', ui.ButtonSet.YES_NO);
if(response == ui.Button.YES) {
if(/Trident|MSIE/.test(HtmlService.getUserAgent())){
ui.showModalDialog(html, 'Loading, please wait...');
} else {
ui.alert("Internet Explorer is not detected.\n Cannot proceed.")
}
}
}


Related Topics



Leave a reply



Submit