Script function not found: entries

2019-08-23 21:47发布

Based on Adding arguments to a function in the onOpen entries object , I'm trying to add entries to the menu of a google sheet.

I'm getting The error in the title:

Script function not found: entries For more information, see https://developers.google.com/apps-script/refere

What am I doing wrong in my code below?

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  var entries = [{
    name : "Summary",
    functionName : "copyRowsByColumnPattern",
  },
  {
    name : "Summary2",
    functionName : "copyRowsByColumnPattern",
  }];

  ui.createMenu('Push to Sheet').addItem('Select Sheet', 'entries').addToUi();
}

3条回答
混吃等死
2楼-- · 2019-08-23 22:04

The problem is .addItem('Select Sheet', 'entries').addToUi();, you're passing the string entries as the second argument instead of the object entries.

Just change that to

.addItem('Select Sheet', entries).addToUi();

and it should work.

Actually, if you want to add multiple items at once, you need to use addMenu

So, it should be: addMenu('Select Sheet', entries)

查看更多
不美不萌又怎样
3楼-- · 2019-08-23 22:06

To create a menu, as indicated by the Apps Script Menus guide, there are two routes:

  1. Ui class (for Docs, Forms, and new-version Sheets
  2. Spreadsheet#addMenu() (for old-version Sheets).

Given a set of parameterless functions to run:

const entries = [
  {name: "function1", functionName: "foo"},
  {name: "function2", functionName: "bar"},
  {name: "function3", functionName: "baz"},
];

With the Ui route, you can only add items one-by-one:

const menu = _____.getUi()
//   .createMenu("menu title");
// OR
//   .createAddonMenu(); 
entries.forEach(function (menuItem) {
  menu.addItem(menuItem.name, menuItem.functionName);
});
menu.addToUi();

This approach allows defining separators and sub-menus as well, hence the one-by-one nature.

The Spreadsheet#addMenu syntax allows direct initialization:

const wb = SpreadsheetApp.getActive();
wb.addMenu("menu title", entries);

Both routes require your Script Project to have defined foo(), bar(), and baz() prior to the Menu creation.

Note also that the Apps Script server is not persistent - if you define those functions programmatically in onOpen, they cease to be defined once the script has finished running, and would need to be redefined every time before they could be accessed.

查看更多
太酷不给撩
4楼-- · 2019-08-23 22:17

The easiest way to create a menu is as follows:

function onOpen() {
  // Add a custom menu to the spreadsheet.
  SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
    .createMenu('Custom Menu')
    .addItem('First item', 'menuItem1')
    .addItem('Second item', 'menuItem2')
    .addItem('Third item', 'menuItem3')
    .addToUi();
}

Documentation

查看更多
登录 后发表回答