2017-07-30 38 views
1

我試圖在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") 
} 

然後警告框,完美的作品,但是當我點擊「是」,沒有真正發生。它不會將我重定向到任何地方。

回答

3

你得到「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?'); 
} 
相關問題