// 這份程式碼綁定在某張試算表上 | |
const URL = "https://stackoverflow.com/search?q=google+script+open+url"; | |
// 開啟試算表的時候,製作一個自訂選單,按了會呼叫下面的 request 函式 | |
function onOpen(e) { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Custom Menu') | |
.addItem('Open URL', 'request') | |
.addToUi(); | |
} | |
// 開啟連結 | |
function request() { | |
// return URL; // 無效,Google 限制一定先要透過 HtmlService 進行消毒 | |
var html = '<html><body><a href="'+ URL +'" target="blank" onclick="google.script.host.close()">Link</a></body></html>'; | |
// return html; // 無效,Google 限制一定先要透過 HtmlService 進行消毒 | |
var output = HtmlService.createHtmlOutput(html); | |
// 如果這份程式碼綁定在 Google Docs、Sheets、Forms,因為 IFRAME 沙盒模式,這句就無效 | |
// 然而,如果這份程式碼是屬於 Web App 的 get 方法,那這句是有效的 | |
// return output; | |
// 最通俗的解法,多使用一層 HTML,並呼叫UI顯示連結給使用者點擊 | |
SpreadsheetApp.getUi().showModalDialog(output, "Download"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment