我有一个后端服务,有些用户的Google表格中有数据。我希望我的服务代表其所有者获取和修改这些工作表中的数据。所以我写了一个分为两部分的解决方案:
包含doGet和doPost函数的脚本编辑器代码 将库导入到该脚本中。该库实际上完成了所有繁重的工作。 脚本的简化版本:
function doGet(req) {
// Let Mylib handle this
return Mylib.handleGET(req);
}
function doPost(req) {
// Let Mylib handle this
return Mylib.handlePOST(req);
}
用户通过“发布”->“作为Web应用程序部署...”将其电子表格公开给全世界...并向我的服务提供生成的Web应用程序URL。
这非常有效,除了我不想打扰用户更新库的版本,然后在每次我对库发布更新时重新部署应用程序。因此,我认为将Library作为Editor Addon来实现将是一个很好的解决方案,但是我现在似乎无法从Editor脚本中访问Addon的handleGET
和handlePOST
函数。>
任何想法仍如何实现?还是有更好的方法?
答案 0 :(得分:0)
已经测试了不同的选项,我决定采用eval
的方式:
var libUrl = 'https://example.com/my-lib-url.js';
// Load My Library from server
function loadLibrary(url: string) {
// This line is needed only to auto detect Auth scope required for the
// fetched library. Without this we'd have to provide the required
// scope in the appscript.json manifest file.
SpreadsheetApp.getActiveSpreadsheet();
const key = 'my-lib-contents';
const cache = CacheService.getScriptCache();
let lib = cache.get(key);
if (lib == null) {
console.log('Fetching library...');
lib = UrlFetchApp.fetch(url).getContentText();
cache.put(key, lib, 3600); // cache for 60 minutes
}
eval(lib);
}
function doGet(req: any) {
if (!Mylib.handleGET) loadLibrary(libUrl);
return result(AccountsManager.handleGET(req));
}
function doPost(req: any) {
if (!Mylib.handlePOST) loadLibrary(libUrl);
return result(AccountsManager.handlePOST(req));
}