【问题标题】:Find apps script published URL from file URL - search files by published Web App URL从文件 URL 中查找应用程序脚本发布的 URL - 按已发布的 Web 应用程序 URL 搜索文件
【发布时间】:2018-06-22 11:08:31
【问题描述】:

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

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

这是一个相当乏味的过程,但它已经完成了这一点。

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

我的预感是组织内部的其他人(不是开发人员的帐户)拥有该项目,但可能是 5 或 6 个不同的人,而且他们都不是开发人员或特别具有技术头脑。另一种可能性是开发者帐户确实拥有该项目,但它的名字很糟糕,我不高兴要经过数十次步骤 5-7 来找到它。

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    要以我概述的方式使用 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 Script、项目 ID、部署 ID、Apps Script API、发布 URL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多