我正在尝试在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")
}
当我运行此脚本并单击按钮时,它会显示&#34; ui未检测到&#34;。当我删除它:
if (response = ui.Button.YES) {
openURL(name,url)
}
else {
Logger.log("You should have tried")
}
然后警报框工作正常,但当我点击&#34;是&#34;时,没有任何事情发生。它并没有将我重定向到任何地方。
答案 0 :(得分:3)
您正在获得&#34; ui not detected
&#34;,因为您已在函数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?');
}