Spreadsheet onOpen菜单从库中 [英] Spreadsheet onOpen menu from a Library

查看:105
本文介绍了Spreadsheet onOpen菜单从库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在几个用户中分发一个包含onOpen菜单的电子表格。我想将菜单定义和功能保留在库中,因此如果我添加一些菜单选项或更新某些功能,则使用该电子表格的每个人都会自动更新。



到目前为止,在电子表格脚本中,我只有这个简单的代码:

  function onOpen(){
myLib.loadMenu ();
}

菜单在电子表格中加载没有问题,但不管我如何命名调用菜单和实际功能(带或不带myLib)。当使用该菜单中的选项时,我总是收到一个错误Script function doSomething could not be found。



谢谢,Fausto

p>

EDIT-1 :让我提供更多详细信息和示例代码 我的目标是可以从库中向电子表格菜单添加更多选项,而不必更新每个用户的电子表格。



这是示例代码,全部包含在电子表格脚本中,没有库();

 函数onOpen(){
testMenu();
}
function testMenu(){
SpreadsheetApp.getActiveSpreadsheet()。addMenu(
'Testing',[
{name:'Do Something#1',functionName :'someFunction1'},
null,
{name:'Do Something#2',functionName:'someFunction2'}]);
}
函数someFunction1(){
SpreadsheetApp.getActiveSheet()。getRange(A1)。setValue(来自someFunction1的Hello);

函数someFunction2(){
SpreadsheetApp.getActiveSheet()。getRange(A2)。setValue(Bye from someFunction2);
}

我想要做的是获得相同的功能,电子表格脚本和库,如下所示:



在电子表格脚本中

  function onOpen(){
xsLib.testMenu();

$ / code $ / pre

在图书馆

  function testMenu(){
SpreadsheetApp.getActiveSpreadsheet()。addMenu(
'Testing',[
{name:'Do Something#1' ,functionName:'someFunction1'},
null,
{name:'Do Something#2',functionName:'someFunction2'}]);
}
函数someFunction1(){
SpreadsheetApp.getActiveSheet()。getRange(A1)。setValue(来自someFunction1的Hello);

函数someFunction2(){
SpreadsheetApp.getActiveSheet()。getRange(A2)。setValue(Bye from someFunction2);





$ b

在这种拆分方法中,从库调用构建的菜单在电子表格中正确显示,但是当使用该测试菜单中的选项时,出现如下错误消息:无法找到脚本函数someFunction1。

解决方案

最后我检查了你不能给脚本中没有包含的触发器函数。你最可能需要做的是在你的scpreadsheet的脚本中创建一个包装函数。像这样:

 函数doSomething(){
myLib.doSomething();

$ / code>

编辑:
您正在查找的功能目前无法使用。为了达到预期效果,您需要指定要调用的函数属于该库,并且不允许。



有一个针对此功能的功能请求:



http://code.google.com/p/google-apps-script-issues/issues/detail?id=799



如果您对此功能感兴趣,请对此问题进行投票以提高其优先级。



Best,

<安东


I will distribute a spreadsheet with onOpen menu among several users. I would like to keep the menu definitions and functionality within a Library, so if I add some menu options or update some functions, everyone using that spreadsheet will get updated automatically.

So far, on the spreadsheet script I have only this simple code:

function onOpen() {
  myLib.loadMenu();
}

The menu loads on the spreadsheet without a problem, however no matter how I named the call on the menu and the actual function (with or without myLib.) I always get an error "Script function doSomething could not be found" when using an option from that menu.

Any idea on how should I name the function call in the menu and the actual function in the library, for this approach to work.

Thanks, Fausto

EDIT-1: Let me give more details and sample code

My goal is being able to add more options to that spreadsheet menu from the library, without having to update every user's spreadsheet.

This is sample code all included in the spreadsheet script, no library been used yet and it works without problem

function onOpen() {
  testMenu();
}
function testMenu() {
  SpreadsheetApp.getActiveSpreadsheet().addMenu(
    'Testing', [
      { name: 'Do Something #1', functionName: 'someFunction1' },
      null,
      { name: 'Do Something #2', functionName: 'someFunction2' } ]);
}
function someFunction1() {
  SpreadsheetApp.getActiveSheet().getRange("A1").setValue("Hello from someFunction1");
}
function someFunction2() {
  SpreadsheetApp.getActiveSheet().getRange("A2").setValue("Bye from someFunction2");
}

What I want to do is get the same functionality, but splitting the code between the spreadsheet script and a library, as follow

On the spreadsheet script

function onOpen() {
  xsLib.testMenu();
}

On the library

function testMenu() {
  SpreadsheetApp.getActiveSpreadsheet().addMenu(
    'Testing', [
      { name: 'Do Something #1', functionName: 'someFunction1' },
      null,
      { name: 'Do Something #2', functionName: 'someFunction2' } ]);
}
function someFunction1() {
  SpreadsheetApp.getActiveSheet().getRange("A1").setValue("Hello from someFunction1");
}
function someFunction2() {
  SpreadsheetApp.getActiveSheet().getRange("A2").setValue("Bye from someFunction2");
}

On this splitting approach the menu built from the library call shows correctly on the spreadsheet, however when using an option from that Testing menu, I get a message error like: "Script function someFunction1 could not be found"

解决方案

Last I checked you cannot give to a trigger functions that are not contained within the script. What you most likely need to do is to create a wrapper function in the script for your scpreadsheet. Something like this:

function doSomething() {
  myLib.doSomething();
}

EDIT: The functionality that you are looking for is currently unavailable. To achieve the desired effect you would need to specify that the function that you want to call belongs to the library and that is not allowed.

There is a feature request for this:

http://code.google.com/p/google-apps-script-issues/issues/detail?id=799

If you are interested in having this functionality, please go and vote on this issue to increase its priority.

Best,

Anton

这篇关于Spreadsheet onOpen菜单从库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆