TL;DR:我正在尋找一種使用現有 google 腳本向 Drive 檔案夾中所有未來添加的檔案添加操作按鈕的方法。
大家好,
我有一個 Google-apps 腳本,用于檢查 Google Drive 檔案夾中檔案計數的變化,并在更改時對其應用某個腳本(所有腳本都在同一個專案中,所有檔案的格式都相同)。
我決定向其中一張作業表添加一個操作按鈕,以根據用戶的請求應用特定的過濾器。
我正在尋找一種方法來將此操作按鈕添加到所有未來添加的檔案中。我已經想到了下一個可能的實作:
- 使用腳本創建繪圖 - 我無法找到生成新繪圖的方法。
- 使特定范圍可點擊 - 也找不到辦法做到這一點
- 將選單添加到檔案的工具列 - 我拋出了一個
SpreadsheetApp.newMenu()無法從當前背景關系呼叫的例外。
有沒有人實施類似的東西并且可以提供幫助?
謝謝!
uj5u.com熱心網友回復:
至于選單,你可以試試這個:
function onOpen() { //[OPTIONAL] Created a custom menu "Timestamp" on your Spreadsheet, where you can run the script
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Whatever you want it to be called') //keep the ''
.addItem('Function Description', 'function call') //for the function, don't use ()
.addSubMenu(ui.createMenu('submenu name') //only if needed
.addItem('Function Description','function call')
.addSeparator() //aestethics only
.addItem('Function Description','function call')
.addToUi();
}
uj5u.com熱心網友回復:
即時工具列
這里有幾個函式可以讓您構建一個側邊欄,讓您可以即時訪問各種 html 控制元件,包括可以通過 google.script.run 呼叫任何服務器端函式的按鈕。
第一個功能啟動側邊欄,提供對可以使用 html 創建的各種工具的即時訪問。
function launchSideBarButtons() {
const ss = SpreadsheetApp.getActive();
let html = '<html><head><style>input {margin: 2px 5px 1px 0;}</style></head><body>';
html = '<input type="text" id="txt1" placeholder="Enter Folder Id" value="folderid" />';//Change the folderid to the default folder that you wish to check on enter a new folder id at the time just prior to execution
html = '<br><input type= "text" id="txt2" placeholder="Display Number Files" readonly />';
html = '<br><input type="button" value="Check Folder" onClick="checkFolder();" />';
html = '<script>';
html = 'function checkFolder() { let id=document.getElementById("txt1").value;google.script.run.withSuccessHandler(function(l){document.getElementById("txt2").value=l;}).checkMyFolder(id);}';
html = 'console.log("My Code")';
html = '</script></body></html>';
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutput(html).setTitle("Instant Tool Bar"));
}
該函式是服務器端函式,實際打開檔案夾并統計檔案數。
function checkMyFolder(id) {
const folder = DriveApp.getFolderById(id);
if (folder) {
const files = folder.getFiles();
let n = 0;
while (files.hasNext()) {
let file = files.next();
n ;
}
return n;
} else {SpreadsheetApp.getUi().alert("Check folder id")}
}
這個函式創建了一個可安裝的 onMyNewOpen() 觸發器,當電子表格打開時它會啟動側邊欄,這樣你就不必為了單擊按鈕而瀏覽選單。它的設定使得無論您運行多少次它都只會創建一個觸發器。
function onMyNewOpen() {
if (ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "launchSideBarButtons").length == 0) {
ScriptApp.newTrigger("launchSideBarButtons").forSpreadsheet(SpreadsheetApp.getActive()).onOpen().create();
}
}
對話框:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360219.html
