从脚本编辑器调用谷歌编辑器插件的功能



我有一个后端服务,一些用户在其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提供给我的服务。

这效果非常好,除了我不想打扰用户以更新库的版本,然后每次发布到库的更新时重新部署了该应用程序。因此,我认为将图书馆作为编辑器插件实现是我的情况的优雅解决方案,但是现在我似乎无法从编辑器脚本到达addon的handleGEThandlePOST函数。

有什么想法如何仍能实现?也许有更好的方法?

在测试了不同的选项之后,我决定使用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));
}

最新更新