在onOpen条目对象中为函数添加参数

时间:2013-07-07 17:23:48

标签: google-apps-script google-sheets google-apps-script-menu

我是否可以在函数中添加参数,这些函数将添加到Google电子表格文档的“脚本中心”菜单中?这是正常的onOpen方法。

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Summary",
    functionName : "myOnOpen"
  }
];
  sheet.addMenu("Script Center Menu", entries);
};

我想知道是否有类似以下内容。

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Summary",
    functionName : "myOnOpen",
    arguments : [value: "Some value"]
  }
  ];
  sheet.addMenu("Script Center Menu", entries);
};

谢谢!

2 个答案:

答案 0 :(得分:8)

我认为这不可能与你所写的完全一样,但你可以通过以下方式实现:

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Summary",
    functionName : "myParameterisedOpen",
  }
  ];
  sheet.addMenu("Script Center Menu", entries);
};

function myParameterisedOpen() {
  myOnOpen("Some value");
};

答案 1 :(得分:0)

So, I wanted to create a bunch of parameterized functions like this, but dynamically based on what was in my sheet. After a bunch of trial and error, and realizing there is a debugger in Google Scripts I found this works:

function onOpen() {
  var name = 'bob';
  var functionName = 'generate' + name + 'Schedule';
  this[functionName] = function() { return generateSchedule(name); }
  generatebobSchedule();
}

function generateSchedule(e) {
  Browser.msgBox("Alert", e, Browser.Buttons.OK);
}