【问题标题】:Getting a list of functions within your GAS project获取 GAS 项目中的函数列表
【发布时间】:2019-04-11 19:52:39
【问题描述】:

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

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

我有一个 Google 电子表格文件。附加到该 Google 电子表格的是一个 GAS 项目(通过 Google 表格菜单“工具 -> 脚本编辑器”访问),它具有几个不同的功能,用于从表格中获取值、进行一些计算并将结果发布到不同的表格.

我想要完成的工作:运行某种函数,它可以为我提供我在 GAS 项目中拥有的所有函数的列表(最好是字符串值)。例如:

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

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

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

【问题讨论】:

  • 记录this的属性。
  • 这就是我需要的信息!谢谢!
  • 额外问题:在我测试过的 GAS 项目中,我有我的 Code.gs 文件和我的 macros.gs 文件。 “this”似乎是一个包含函数的对象,其中包含我的所有函数(来自 Code.gs 和 macros.gs)。有没有办法只返回 Code.gs 中的函数?据我所知,“this”对象的唯一属性是实际的函数本身。不知道它们来自什么来源(代码或宏)。
  • 没有。 Apps 脚本在所有文件中都有一个命名空间。
  • 我已更新答案以显示 Apps Script API 的使用。

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


【解决方案1】:

您可以使用 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)
}

【讨论】:

  • 这行得通!我很欣赏代码。我猜它背后的解释是“this”是指文件本身。从来不知道这个。很高兴知道!谢谢!
  • 额外问题:在我测试过的 GAS 项目中,我有我的 Code.gs 文件和我的 macros.gs 文件。 “this”似乎是一个包含函数的对象,其中包含我的所有函数(来自 Code.gs 和 macros.gs)。有没有办法只返回 Code.gs 中的函数?据我所知,“this”对象的唯一属性是实际的函数本身。不知道它们来自什么来源(代码或宏)。
  • 是的,有一种方法可以仅从 Code.gs 获取函数名称,您需要使用 Apps Script API。我已经更新了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 2015-02-04
  • 2011-08-21
  • 1970-01-01
  • 2013-04-29
  • 2012-11-28
  • 1970-01-01
相关资源
最近更新 更多