从文件URL查找应用程序脚本已发布的U​​RL-通过已发布的Web应用程序URL搜索文件

时间:2018-06-22 11:08:32

标签: url google-apps-script google-apps google-sites

我们从客户以前的开发人员那里继承了许多Google Apps脚本项目。 Apps脚本通过嵌入式窗口小部件部署在Google网站(sites.google.com)上的各个页面上。每当我们需要处理其中之一时,我们就可以通过以下方式找到项目:

  1. 转到sites.google.com上包含小部件的页面,
  2. 编辑窗口小部件,
  3. 注意发布的网址,
  4. 要访问script.google.com,
  5. 打开/编辑名为“类似于”我们想要的项目的项目,
  6. 点击发布>部署为网络应用
  7. 将“当前Web应用程序URL”与上面第3步中看到的内容进行比较

这是一个相当繁琐的过程,但到目前为止已经完成了。

其中一个小工具在通过sites.google.com进行访问时开始显示“需要授权...”消息,因此我们需要跟踪其所属的项目。我们完成了上面的步骤1-3,但是找不到任何具有与小工具相匹配的URL的项目。

我的直觉是组织内部的其他人(而不是开发人员的帐户)拥有该项目,但是可能有5或6个不同的人,他们都不是开发人员,或者不是专门从事技术工作的人。另一种可能性是开发人员帐户拥有,但该项目的名称很差,我不高兴要经过5到7遍步骤数十次才能找到它。

是否可以根据其URL定位特定项目? script.google.com上的搜索工具似乎仅搜索项目名称,不幸的是,在这种情况下,它没有帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用Apps Script API获取脚本的“部署ID”。

首先,您需要使用DriveApp来获取Apps Script项目文件的列表。然后,您需要遍历所有文件,获取文件ID,然后使用文件ID获取部署信息。

每个项目都有一个部署列表。获取一个部署,然后从JSON对象获取deploymentId。

要以我概述的方式使用Apps Script API,必须在appsscript.json清单文件中设置所需的作用域。

以下是设置外观的示例:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "webapp": {
    "access": "ANYONE",
    "executeAs": "USER_ACCESSING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/script.projects", 
                  "https://www.googleapis.com/auth/drive.scripts", 
                  "https://www.googleapis.com/auth/drive", 
                  "https://www.googleapis.com/auth/script.container.ui", 
                  "https://www.googleapis.com/auth/script.external_request", 
                  "https://www.googleapis.com/auth/script.scriptapp",
                  "https://www.googleapis.com/auth/script.deployments",
                  "https://www.googleapis.com/auth/script.deployments.readonly"]
}

首次运行代码时,会提示您授权权限。但是,即使在授予权限之后,您仍然需要转到开发者控制台并为该项目启用Apps Script API。

因此,您将从一个项目中运行代码以获取所有Apps脚本文件的列表,然后获取每个项目的部署,并从部署中获取部署ID。

运行代码时,使用“查看”菜单并选择“日志”。您会在日志中看到如下错误消息:

[18-06-22 08:51:32:841 EDT] response: {
  "error": {
    "code": 403,
    "message": "Apps Script API has not been used in project abc123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123"
          }
        ]
      }
    ]
  }
}

将Url复制到开发人员仪表板,然后将其粘贴到浏览器地址栏中。在信息中心中,启用API。

以下是您需要使用的代码示例:

function searchForProjectWithCertainID() {
  var files,params,projectID_toFind,rtrn,thisFileID;

  projectID_toFind = "Put ID to find here";

  //params = 'mimeType contains "json"';
  //files = DriveApp.searchFiles(params);

  files = DriveApp.getFilesByType(MimeType.GOOGLE_APPS_SCRIPT);//Get all Apps Script files

  while (files.hasNext()) {
    thisFileID = files.next().getId();

    //Logger.log(thisFileID)

    rtrn = getDeploymentID(thisFileID);

    if (rtrn === projectID_toFind) {
      break;
    }
  }
}

function getDeploymentID(scriptId) {

  var errMsg,L,options,response,theAccessTkn,url;

  theAccessTkn = ScriptApp.getOAuthToken();

  url = "https://script.googleapis.com/v1/projects/" + scriptId + "/deployments";

  options = {
    "method" : "GET",
    "muteHttpExceptions": true,
    "headers": {
      'Authorization': 'Bearer ' +  theAccessTkn
    }
  };

  response = UrlFetchApp.fetch(url,options);
  Logger.log('response: ' + response)

  response = JSON.parse(response);//The response must be parsed into JSON even though it is an object

  L = response.deployments.length;
  //Logger.log('response.deployments.length: ' + response.deployments.length)

  if (typeof response === 'object') {
    errMsg = response.error;
    if (errMsg) {
      errMsg = errMsg.message;
      return 'err' + errMsg;
    }
  }

  //Logger.log(response.deployments[L - 1].deploymentId);

  return response.deployments[L - 1].deploymentId;
}

List Project Deployements

关键字:Apps脚本,项目ID,部署ID,Apps Script API,发布的URL

相关问题