【发布时间】:2012-05-31 11:11:24
【问题描述】:
有没有办法编写一个谷歌应用程序脚本,以便在运行时打开第二个浏览器窗口到 www.google.com(或我选择的其他网站)?
我正在尝试解决我之前的问题: Can I add a hyperlink inside a message box of a Google Apps spreadsheet
【问题讨论】:
标签: html redirect google-apps-script hyperlink
有没有办法编写一个谷歌应用程序脚本,以便在运行时打开第二个浏览器窗口到 www.google.com(或我选择的其他网站)?
我正在尝试解决我之前的问题: Can I add a hyperlink inside a message box of a Google Apps spreadsheet
【问题讨论】:
标签: html redirect google-apps-script hyperlink
此函数无需额外的用户交互即可打开一个 URL。
/**
* Open a URL in a new tab.
*/
function openUrl( url ){
var html = HtmlService.createHtmlOutput('<html><script>'
+'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
+'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
+'if(document.createEvent){'
+' var event=document.createEvent("MouseEvents");'
+' if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'
+' event.initEvent("click",true,true); a.dispatchEvent(event);'
+'}else{ a.click() }'
+'close();'
+'</script>'
// Offer URL as clickable link in case above code fails.
+'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
+'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
+'</html>')
.setWidth( 90 ).setHeight( 1 );
SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}
此方法通过创建一个临时对话框来工作,因此它不适用于无法访问 UI 服务的上下文,例如脚本编辑器或自定义 G 表格公式。
【讨论】:
openUrl( 'https://google.com' );
http://,然后就可以了
您可以构建一个小型 UI 来完成这样的工作:
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);
}
如果您想“显示”网址,只需像这样更改此行:
var link = app.createAnchor(href, href).setId("link");
编辑:link to a demo spreadsheet in 只读,因为太多人一直在上面写不需要的东西(只需复制一份以代替使用)。
编辑:Google 已于 2014 年 12 月 11 日弃用 UiApp,此方法随时可能中断,需要更新以改用 HTML 服务!
编辑: 下面是一个使用 html 服务的实现。
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");
}
【讨论】:
确实不需要按照赏金答案中的建议创建自定义点击事件,也不需要按照已接受答案中的建议显示网址。
window.open(url)1 会自动打开网页而无需用户交互,前提是禁用弹出窗口拦截器(就像 Stephen 的 answer 一样)
<!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>
function modalUrl(){
SpreadsheetApp.getUi()
.showModalDialog(
HtmlService.createHtmlOutputFromFile('openUrl').setHeight(50),
'Opening StackOverflow'
)
}
【讨论】:
Google Apps 脚本不会自动打开网页,但它可用于显示带有链接、按钮的消息,用户可以点击这些按钮打开所需的网页,甚至可以使用 Window object 和类似的方法addEventListener() 打开网址。
值得注意的是,UiApp 现在已被弃用。来自Class UiApp - Google Apps Script - Google Developers
已弃用。 UI 服务是deprecated on December 11, 2014。到 创建用户界面,请改用HTML service。
HTML 服务链接页面中的示例非常简单,
代码.gs
// Use this code for Google Docs, Forms, or new Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html, 'Dialog title');
}
index.html 的自定义版本,用于显示两个超链接
<a href='http://stackoverflow.com' target='_blank'>Stack Overflow</a>
<br/>
<a href='http://meta.stackoverflow.com/' target='_blank'>Meta Stack Overflow</a>
【讨论】:
基于前面的示例,我认为有一种更简洁的方法。在您的项目中创建一个index.html 文件并使用上面的斯蒂芬代码,只需将其转换为 HTML 文档。
<!DOCTYPE html>
<html>
<base target="_top">
<script>
function onSuccess(url) {
var a = document.createElement("a");
a.href = url;
a.target = "_blank";
window.close = function () {
window.setTimeout(function() {
google.script.host.close();
}, 9);
};
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) {
window.document.body.append(a);
}
event.initEvent("click", true, true);
a.dispatchEvent(event);
} else {
a.click();
}
close();
}
function onFailure(url) {
var div = document.getElementById('failureContent');
var link = '<a href="' + url + '" target="_blank">Process</a>';
div.innerHtml = "Failure to open automatically: " + link;
}
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).getUrl();
</script>
<body>
<div id="failureContent"></div>
</body>
<script>
google.script.host.setHeight(40);
google.script.host.setWidth(410);
</script>
</html>
然后,在您的 Code.gs 脚本中,您可以有类似以下的内容,
function getUrl() {
return 'http://whatever.com';
}
function openUrl() {
var html = HtmlService.createHtmlOutputFromFile("index");
html.setWidth(90).setHeight(1);
var ui = SpreadsheetApp.getUi().showModalDialog(html, "Opening ..." );
}
【讨论】:
我喜欢@Stephen M. Harris 的回答,直到最近才对我有用。我不知道为什么stopped working。
function openUrl( url ){
Logger.log('openUrl. url: ' + url);
const html = `<html>
<a id='url' href="${url}">Click here</a>
<script>
var winRef = window.open("${url}");
winRef ? google.script.host.close() : window.alert('Configure browser to allow popup to redirect you to ${url}') ;
</script>
</html>`;
Logger.log('openUrl. html: ' + html);
var htmlOutput = HtmlService.createHtmlOutput(html).setWidth( 250 ).setHeight( 300 );
Logger.log('openUrl. htmlOutput: ' + htmlOutput);
SpreadsheetApp.getUi().showModalDialog( htmlOutput, `openUrl function in generic.gs is now opening a URL...` ); // https://developers.google.com/apps-script/reference/base/ui#showModalDialog(Object,String) Requires authorization with this scope: https://www.googleapis.com/auth/script.container.ui See https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
}
https://developers.google.com/apps-script/reference/base/ui#showModalDialog(Object,String) 需要此范围的授权:https://www.googleapis.com/auth/script.container.ui 请参阅https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
【讨论】: