Skip to content

Instantly share code, notes, and snippets.

@daleth90
Last active November 10, 2020 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daleth90/93fead7f6d9cfb3217c8199c67909d4f to your computer and use it in GitHub Desktop.
Save daleth90/93fead7f6d9cfb3217c8199c67909d4f to your computer and use it in GitHub Desktop.
// 這份程式碼綁定在某張試算表上
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