- 您想要检索函数列表。
- 您希望使用 Google Apps 脚本来实现此目的。
我可以像上面那样理解。如果我的理解是正确的,这个答案怎么样?请将此视为多个答案之一。
模式一:
在此模式中,Apps Script API 用于检索函数列表。当使用Apps Script API的projects.getContent方法时,您可以检索Google Apps Script项目中的所有函数名称。
试试这个API:
首先,作为测试用例,您可以在试试这个API https://developers.google.com/apps-script/api/reference/rest/v1/projects/getContent?apix_params=%7B%22scriptId%22%3A%22Please%20set%20your%20script%20ID%20here%22%2C%22fields%22%3A%22files(functionSet%2Cname)%22%7D。使用时,请设置脚本ID和files(functionSet,name)
to fields
.
示例脚本:
这是 Google Apps 脚本的示例脚本。使用Apps Script API的projects.getContent方法。在使用该脚本之前,请先进行以下设置。
Settings:
- Add
https://www.googleapis.com/auth/script.projects.readonly
到范围。
- 如果运行此脚本的脚本是在 2019 年 4 月 8 日之后创建的,请将 Cloud Platform 项目链接到 Google Apps 脚本项目。 https://developers.google.com/apps-script/guides/cloud-platform-projects
- 在 API 控制台启用 Apps 脚本 API。
- 复制并粘贴以下示例脚本。
- Run
myFunction()
.
这样,就可以检索到所有函数的列表。
Script:
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)
}
}
}
Note:
参考:
- 方法:projects.getContent https://developers.google.com/apps-script/api/reference/rest/v1/projects/getContent?apix_params=%7B%22scriptId%22%3A%22Please%20set%20your%20script%20ID%20here%22%2C%22fields%22%3A%22files%2FfunctionSet%22%7D
- 谷歌云平台项目 https://developers.google.com/apps-script/guides/cloud-platform-projects
如果我误解了你的问题并且这不是你想要的方向,我深表歉意。