Script function not found: entries

…衆ロ難τιáo~ 提交于 2020-01-16 11:25:29

问题


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();
}

回答1:


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.




回答2:


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




回答3:


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)



来源:https://stackoverflow.com/questions/51826107/script-function-not-found-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!