获取GAS项目中的功能列表

时间:2019-04-11 19:52:39

标签: google-apps-script google-apps-script-editor

我想看看是否有一种方法可以获取我在Google Apps脚本项目中拥有的所有功能的列表。我已经看到了多个线程来获取所有Google Apps Script项目的列表,但到目前为止还没有一个线程可以列出每个项目中的所有功能。有人知道这是否可能吗?我浏览了《 Google Apps脚本参考概述》,但找不到任何对我印象深刻的东西(我当然可以错过它)。如果有人有任何建议,请告诉我!。

我能提供的最好的例子是:

我有一个Google Spreadsheet文件。与该Google Spreadsheet相连的是一个GAS项目(可通过Google Sheet菜单“工具->脚本编辑器”访问),该项目具有几个不同的函数,这些函数用于从工作表中获取值,进行一些计算并将结果发布到另一个工作表中

我要完成的工作:运行某种可以向我提供GAS项目中所有功能的列表(最好是字符串值)。例如:

["runMyCalculations","myOnEdit","sortClosedFiles","formatSheets"]

所有这些功能只有在我打开脚本编辑器并在下拉菜单中选择它并单击“运行”按钮时才能运行。

我想做的是创建所有功能的动态列表,这样我就可以将它们传递给“打开时”触发的功能,该功能在工作表中创建自定义菜单,列出所有功能我有。我想要这样做,因此我可以只对工作表进行更改,转到下拉菜单并运行我需要运行的功能,而不必打开脚本编辑器。

1 个答案:

答案 0 :(得分:0)

您可以使用Apps Script API从Apps脚本文件中获取所有内容。 以下代码可以选择传递文件名来获取。您必须提供Apps脚本文件ID。传入gs文件名是可选的。提供了3个功能。完成所有工作的函数,使用测试参数调用该函数的函数以及日志记录函数。不需要OAuth库,因为令牌是从ScriptApp服务获取的。

注意:您需要启用Apps脚本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)
}