您可以使用 Apps 脚本 API 从 Apps 脚本文件中获取所有内容。
下面的代码可以选择传入一个文件名来获取。您必须提供 Apps 脚本文件 ID。传入 gs 文件名是可选的。提供了3个功能。完成所有工作的函数,一个使用测试参数调用该函数的函数,以及一个日志记录函数。不需要 OAuth 库,因为令牌是从 ScriptApp 服务获取的。
注意:您需要启用 Apps Script API,并批准您的云端硬盘权限才能使此代码正常工作。确保在您第一次运行此代码时检查来自 UrlFetchApp.fetch() 调用的返回是否有错误消息。它可能有一个链接,您需要使用它来启用 Apps Script API。
function getFuncNames(po) {
var allFiles,dataContentAsString,downloadUrl,fileContents,fileData,i,options,
theAccessTkn,thisFileName;
var ndxOfFunction=0,counter=0, ndxOfEnd=0, functionName="", allFncNames=[],
hasSpaces = 0;
var innerObj, thisFile, fileType = "", thisGS_Content,howManyFiles, allGsContent="";
/*
Get all script function names. If no gs file name is provided, the code
gets all the function names.
*/
/*
po.fileID - required - The Apps Script file ID
po.gsFileName - optional - the gs code file name to get - gets just one
file instead of all files
*/
//ll('po',po);
if (!po.fileID) {
return false;
}
theAccessTkn = ScriptApp.getOAuthToken();//Get an access token for OAuth
downloadUrl = "https://script.google.com/feeds/download/export?id=" +
po.fileID + "&format=json";//create url
options = {
"kind": "drive#file",
"id": po.fileID,
"downloadUrl": downloadUrl,
"headers": {
'Authorization': 'Bearer ' + theAccessTkn,
},
"contentType": "application/vnd.google-apps.script+json",
"method" : "GET"
};
fileData = UrlFetchApp.fetch(downloadUrl, options);//Get all the content from the Apps Script file
//ll('fileData',fileData)
dataContentAsString = fileData.getContentText();
fileContents = JSON.parse(dataContentAsString);//Parse string into object
allFiles = fileContents.files;//All the files in the Apps Script project
howManyFiles = allFiles.length;
for (i=0;i<howManyFiles;i++) {
thisFile = allFiles[i];//Get one inner element that represents one file
if (!thisFile) {continue;}
fileType = thisFile.type;
if (fileType !== "server_js") {continue;}//This is not a gs file - its HTML or json
thisFileName = thisFile.name;
//ll('typeof thisFileName',typeof thisFileName)
//ll('thisFileName',thisFileName)
//ll('equal',po.gsFileName !== thisFile.name)
if (po.gsFileName) {//Is there a setting for the file name to restrict the search to
if (po.gsFileName !== thisFile.name) {//The name to search for is not this file name
continue;
}
}
thisGS_Content = thisFile.source;//source is the key name for the file content
allGsContent = allGsContent + thisGS_Content;
}
//ll('allGsContent',allGsContent)
while (ndxOfFunction !== -1 || counter < 1000) {
ndxOfFunction = allGsContent.indexOf("function ");
//ll('ndxOfFunction',ndxOfFunction)
if (ndxOfFunction === -1) {break};
allGsContent = allGsContent.slice(ndxOfFunction+9);//Remove everything in front of 'function' first
ndxOfEnd = allGsContent.indexOf("(");
functionName = allGsContent.slice(0,ndxOfEnd);
allGsContent = allGsContent.slice(ndxOfEnd+2);//Remove the
hasSpaces = functionName.indexOf(" ");
if (hasSpaces !== -1) {continue;}
if (functionName.length < 150) {
allFncNames.push(functionName);
}//Any string over 150 long is probably not a function name
counter ++;
};
//ll('allFncNames',allFncNames)
return allFncNames;
};
function runOtherFnk() {
getFuncNames({fileID:"Your File ID here",gsFileName:"Code"});
}
function ll(a,b) {
//Logger.log(typeof a)
if (typeof b === 'object') {
b = JSON.stringify(b);
}
Logger.log(a + ":" + b)
}
以下代码从this 对象中提取文件名:
function getAllFnks() {
var allFnks,fnkStr,k;
allFnks = [];
for (k in this) {
//Logger.log(k)
//Logger.log(typeof k)
//Logger.log(this[k])
//Logger.log(typeof this[k])
fnkStr = this[k];
if (fnkStr) {
fnkStr = fnkStr.toString();
//Logger.log(typeof fnkStr)
} else {
continue;
}
//Logger.log(fnkStr.toString().indexOf('function'))
if (fnkStr.indexOf('function') === 1) {
allFnks.push(k);
}
}
Logger.log(allFnks)
Logger.log('Number of functions: ' + allFnks.length)
}