在Google电子表格中点击其重定向到新标签中的网址后,按钮的脚本?

问题描述:

我试图在google电子表格中添加一个按钮,该按钮会将用户重定向到脚本中指定的特定URL。这是我使用的脚本:在Google电子表格中点击其重定向到新标签中的网址后,按钮的脚本?

function testNew() { 
    showURL('Search Here','https://docs.google.com'); 
} 
// 


function showURL(name,url) { 
    var html = '<html><body><a href="'+url+'" target="parent" onclick="google.script.host.close()">'+name+'</a></body></html>'; 
    var ui = HtmlService.createHtmlOutput(html).setHeight(50).setWidth(200) 
    var ui = SpreadsheetApp.getUi(); 
    var response = ui.alert('Confirm', 'Are you sure you want to continue?', ui.ButtonSet.YES_NO) ; 
} 

if (response = ui.Button.YES) { 
    openURL(name,url) 

} 
else { 
    Logger.log("You should have tried") 
} 

当我运行此脚本,按一下按钮,它说:“UI未检测”。当我删除此:

if (response = ui.Button.YES) { 
    openURL(name,url) 

} 
else { 
    Logger.log("You should have tried") 
} 

然后警告框,完美的作品,但是当我点击“是”,没有真正发生。它不会将我重定向到任何地方。

你得到“ui not detected”,因为你已经写了下面的部分功能showURL

if (response = ui.Button.YES){ 
    openURL(name,url) 
}else { 
    Logger.log("You should have tried") 
} 

如果你想重新定向到不同的页面,请参考下面的代码。

function testNew(){ 
    showURL('Search Here','https://docs.google.com'); 
} 

function showURL(name,url) { 
    var html = '<html><body style="margin:0px;"><div class="modal-dialog-buttons"><a href="'+url+'" target="parent" onclick="google.script.host.close()"><button style="margin:5px;">Yes</button></a><button onclick="google.script.host.close()">No</button></div></body></html>'; 
    var ui = HtmlService.createHtmlOutput(html).setHeight(50).setWidth(400) 
    SpreadsheetApp.getUi().showModalDialog(ui, 'Are you sre you want to contine?'); 
}