如何获得项目中所有功能的列表?

时间:2019-07-19 04:49:55

标签: google-apps-script google-sheets

我想以编程方式提取Google Apps项目中所有功能的列表。在任何服务中似乎都没有一种方法可以轻松地将它们拉出,除非它似乎以一种或另一种方式存储在.this函数中。最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

  • 您要检索功能列表。
  • 您想使用Google Apps脚本实现这一目标。

我可以像上面那样理解。如果我的理解是正确的,那么这个答案呢?请认为这只是几个答案之一。

模式1:

在这种模式下,Apps Script API用于检索功能列表。使用Apps Script API的projects.getContent方法时,您可以检索Google Apps Script项目中的所有函数名称。

尝试使用此API:

首先,作为测试用例,您可以在Try this API进行测试。使用此功能时,请将脚本ID和files(functionSet,name)设置为fields

示例脚本:

这是Google Apps脚本的示例脚本。使用Apps Script API的projects.getContent方法。使用此脚本之前,请进行以下设置。

设定:
  1. https://www.googleapis.com/auth/script.projects.readonly添加到范围。
  2. If the script for running this script is created after April 8, 2019, please link the Cloud Platform Project to the Google Apps Script Project.
  3. 在API控制台上启用Apps脚本API。
  4. 复制并粘贴以下示例脚本。
  5. 运行myFunction()

通过这种方式,可以检索所有功能的列表。

脚本:
function myFunction() {
  var scriptId = "###"; // Please set the script ID here.

  var url = "https://script.googleapis.com/v1/projects/" + scriptId + "/content?fields=files(functionSet%2Cname)";
  var options = {
    "method" : "get",
    "headers": {"Authorization": "Bearer " +  ScriptApp.getOAuthToken()},
  };
  var res = UrlFetchApp.fetch(url, options);
  Logger.log(res.getContentText())
}

模式2:

在这种模式下,使用this。您的问题也提到了这一点。在这种情况下,不使用任何API。请复制以下脚本并将其粘贴到脚本编辑器,然后运行myFunction()。这样,可以检索所有功能的列表。

示例脚本:

function myFunction() {
  for (var key in this) {
    if (typeof this[key] == "function") {
      Logger.log(key)
    }
  }
}

注意:

  • 这两种模式都可以检索每个函数的脚本。

参考文献:

如果我误解了您的问题,而这不是您想要的方向,我深表歉意。

相关问题