You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Apps Script编程设置容器绑定脚本的Google Cloud Platform项目

解决方案

推荐方案:使用官方Apps Script API

该方案为谷歌官方公开支持的稳定实现,无需抓取未公开的页面请求,直接调用Apps Script API的projects.update接口即可完成GCP项目绑定:

  • 先在执行脚本的appsscript.json配置文件中添加必要的OAuth授权作用域:
{
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.projects",
    "https://www.googleapis.com/auth/script.external_request"
  ]
}
  • 提前确认目标GCP项目已经启用Apps Script API,且你对需要绑定的Apps Script项目和目标GCP项目都拥有编辑权限
  • 代码实现参考:
function changeGCPproject() {
  const scriptId = '目标容器绑定脚本的ID';
  const gcpProjectNumber = '目标GCP项目的数字ID';

  const url = `https://script.googleapis.com/v1/projects/${scriptId}`;
  const payload = {
    "cloudPlatform": {
      "projectId": gcpProjectNumber
    }
  };
  const token = ScriptApp.getOAuthToken();
  const params = {
    method: 'PUT',
    headers: {
      Authorization: 'Bearer ' + token,
      'Content-Type': 'application/json'
    },
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };
  const response = UrlFetchApp.fetch(url, params);
  console.log(response.getContentText());
}

不推荐方案:使用未公开内部接口

你要找的页面请求属于谷歌内部未公开接口,无任何稳定性保障,随时可能调整参数或直接下线,仅作参考:
请求URL为https://script.google.com/home/projects/${scriptId}/setCloudProject,POST请求体需要携带projectNumber参数,但该接口存在额外的内部权限校验,调用失败概率极高,禁止在生产环境使用。

补充说明

  • 容器绑定脚本创建完成后,可以通过SpreadsheetApp.getActiveSpreadsheet().getContainerInfo().getScriptId()直接获取对应的脚本ID,无需手动复制
  • GCP项目绑定完成后无法通过API解绑,只能手动进入脚本设置页面操作,绑定前请确认配置信息无误

内容的提问来源于stack exchange,提问作者Alex Libengood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 06:42:01