我面临着与一系列功能共享脚本的问题,这些功能旨在帮助操作同一原始谷歌电子表格的不同副本(每个用户都有个人副本)。 每个电子表格副本都有自己的有界脚本副本,所以如果我改进任何功能,我必须更新脚本的每个副本。
为了解决这个问题,我看到可以从脚本项目中调用库,并将所有共享函数放在该库中。我读到的一些参考文献:
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
在那里你可以看到使用这种方法的几个问题:
所以我想知道是否有可能以其他方式包含代码,我发现一些关于使用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 )。
我的代码:
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");
}