使用eval

时间:2018-05-13 09:41:25

标签: javascript google-apps-script google-sheets eval

我面临着与一系列功能共享脚本的问题,这些功能旨在帮助操作同一原始谷歌电子表格的不同副本(每个用户都有个人副本)。 每个电子表格副本都有自己的有界脚本副本,所以如果我改进任何功能,我必须更新脚本的每个副本。

为了解决这个问题,我看到可以从脚本项目中调用,并将所有共享函数放在该库中。我读到的一些参考文献:

How can I invoke my standalone script in my spreadsheet?; How to use a google apps script in multiple documents; How to pass bounded script to another user so he can add it to his own spreadsheets?; Distribute Google Apps Script and push updates; https://issuetracker.google.com/issues/36755072

在那里你可以看到使用这种方法的几个问题:

  • 要宣传您的图书馆更新(但不必再次触摸用户工作表),必须使用开发模式将其包含在带有图纸的脚本中,并为您的用户提供 编辑 访问您的库脚本(如果他们只有视图访问权限,他们使用您的库的静态版本)。
  • 您需要重新设计原始的表单绑定代码,因此函数会被调用,前面是用于指定库的名称:libraryname.functionName() - 可能不是问题,但我提到它只是为了防止其他人正因为此而得到错误,就像我一样 - 。
  • 从我的测试中,我觉得使用该库时代码运行速度要慢得多,而与表单边界脚本中粘贴的代码相同。

所以我想知道是否有可能以其他方式包含代码,我发现一些关于使用eval()来包含来自外部网址的javascript代码的评论

importing external javascript to google apps script; inject external javascript file in google app script(Google Docs); https://ctrlq.org/code/20380-load-external-javascript-with-eval

基本上,这就是他们这样说的方式:

eval(UrlFetchApp.fetch('http://path.to/external/javascript.js').getContentText());

我的问题:

  • 我不完全理解eval()在上面的示例中应该如何工作。是否打算从在谷歌应用服务器中运行的脚本运行? (所以我的文件仍然会调用SpreadsheetApp。或DriveApp。函数)

  • 如果eval()中的代码定义了一堆函数,那么在eval()完成后它们是否仍然可用?我们可以从用户驱动的事件中调用它们,例如单击菜单选项吗?

  • 如果我们可以打电话给他们,应该以特殊的方式进行奖励吗?我无法做到这一点

我可以使用eval(remote.js)加载一个新菜单(在remote.js中定义)。 但是remote.js中定义的其他函数不再可用于菜单调用(单击菜单会引发错误,如找不到脚本函数:f1 )。

  • 我是否可以在定义菜单之前加载所有功能?我尝试在开始时定义函数f1(),但它与f2()没有区别,后者在最后。我还尝试了两个单独的调用,第一个eval(remote_functions.js),第二个eval(remote_menuLoader.js),但这也没有什么区别(所以我的代码下面只显示一个eval调用,它会加载一个具有函数和菜单定义的remote.js将调用它们。

我的代码:

Google电子表格限制脚本:

var app = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
  app.addMenu("LOAD remote menu", [
    {name: "eval(remote.js) to load new menu", functionName: "evalRemote"}
  ]);
}
function evalRemote() {
  var remotejs_url="https://drive.google.com/uc?authuser=0&id=1wCjewWFTPdH-Luw53zWh_KHX-RhRhFIn&export=view";
  eval(UrlFetchApp.fetch(remotejs_url).getContentText());
  var message=remote_onOpen(app);  
  Browser.msgBox(message);
}

remote.js文件:

function f1() {
    Browser.msgBox("you called f1");
}

function remote_onOpen(app) {
    var menuName="REMOTE-loaded menu";
    app.addMenu(menuName, [
    {name: "FUNCTION 1", functionName: "f1"}
    ,{name: "FUNCTION 2", functionName: "f2"}
    ]);
    return("remote_onOpen(app) function was called;\\n " 
    + "A reference to '" + app.getName() + "' was passed \\n"
    + "in order to create this new menu on it:\\n" 
    + "'" + menuName + "'\\n\\n" 
    + "You should now have 2 new functions available.");
}

function f2() {
    Browser.msgBox("you called f2");
}

0 个答案:

没有答案
相关问题