PY_*_*PY_ 22 html redirect hyperlink google-apps-script
有没有办法编写谷歌应用程序脚本,所以当运行时,第二个浏览器窗口打开到www.google.com(或我选择的其他网站)?
我想在这里找到解决上一个问题的方法: 我可以在Google Apps电子表格的消息框中添加超链接吗?
Ser*_*sas 27
您可以构建一个小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);
}
Run Code Online (Sandbox Code Playgroud)
如果你想"显示"网址,只需像这样更改此行:
var link = app.createAnchor(href, href).setId("link");
Run Code Online (Sandbox Code Playgroud)
编辑:以只读方式链接到演示电子表格,因为有太多人继续写不需要的东西......制作副本使用.
编辑:!! UiApp于2014年12月11日被Google折旧,此方法可能随时中断,需要更新才能使用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");
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*ris 20
此功能打开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 ..." );
}
Run Code Online (Sandbox Code Playgroud)
此方法通过创建临时对话框来工作,因此它不能在无法访问UI服务的上下文中工作,例如脚本编辑器或自定义G表格公式.
确实不需要按照奖励答案中的建议创建自定义点击事件,也不需要按照已接受的答案中的建议显示网址。
window.open(url)1会在没有用户交互的情况下自动打开网页,前提是禁用了弹出窗口阻止程序(斯蒂芬的回答就是这种情况)
<!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>
Run Code Online (Sandbox Code Playgroud)
function modalUrl(){
SpreadsheetApp.getUi()
.showModalDialog(
HtmlService.createHtmlOutputFromFile('openUrl').setHeight(50),
'Opening StackOverflow'
)
}
Run Code Online (Sandbox Code Playgroud)
Google Apps脚本不会自动打开网页,但可以用来显示带有链接或按钮的消息,用户可以点击这些链接或按钮来打开所需的网页.
值得注意的是,UiApp现已弃用.来自UiApp类 - Google Apps脚本 - Google Developers
已过时.UI服务已于2014年12月11日弃用.要创建用户界面,请改用HTML服务.
HTML Service链接页面中的示例非常简单,
Code.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');
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我喜欢@Stephen M. Harris 的回答,直到最近它对我都有效。我不知道为什么它停止工作。
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
}
Run Code Online (Sandbox Code Playgroud)
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