我正在尝试在电子表格中创建一个菜单而不打开它。我以前在 onOpen() 函数中做过,但现在我试图在普通函数中做。使用此脚本,我可以创建一个文件,但它不会在文件中创建我的“操作”菜单。
function Frank_PersistentVariable(){
//create file ID
var ss = SpreadsheetApp.create("TEST").getId();
//clean cache of file IDs
CacheService.getPrivateCache().remove('cachedObject');
//load file ID to cache
CacheService.getPrivateCache().put('cachedObject', ss);
}
function Frank_Menu(){
//create menu
var menu = [
{name: "action1", functionName: "MyFunction1"},
{name: "action2", functionName: "MyFunction2"},
{name: "action3", functionName: "MyFunction3"},
];
//call file ID from cache
var cachedObject = CacheService.getPrivateCache().get('cachedObject');
//call file using ID
var ss = SpreadsheetApp.openById(cachedObject);
//display menu in file
ss.addMenu("actions", menu);
}