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

Jef*_*eff 3 google-sheets google-apps-script

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

Tan*_*ike 5

  • 您想要检索函数列表。
  • 您希望使用 Google Apps 脚本来实现此目的。

我可以像上面那样理解。如果我的理解是正确的,这个答案怎么样?请将此视为多个答案之一。

模式一:

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

试试这个API:

首先,作为测试用例,您可以在Try this API进行测试。使用时,请设置脚本 IDfiles(functionSet,name)fields

示例脚本:

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

设置:
  1. 添加https://www.googleapis.com/auth/script.projects.readonly到范围。
  2. 如果运行此脚本的脚本是在 2019 年 4 月 8 日之后创建的,请将 Cloud Platform 项目链接到 Google Apps 脚本项目。
  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())
}
Run Code Online (Sandbox Code Playgroud)

模式2:

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

示例脚本:

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

笔记:

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

参考:

如果我误解了你的问题并且这不是你想要的方向,我很抱歉。