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

如何以编程方式重命名绑定到Google表格的Apps Script脚本副本

问题原因说明

  1. 绑定到Google表格的Apps Script(绑定型脚本)本身不会作为独立文件展示在谷歌云盘里,所以你用Drive API或者DriveApp通过脚本ID访问时提示文件不存在属于正常情况,无法通过Drive相关接口操作绑定型脚本。
  2. 调用Apps Script API提示权限不足是因为你没有在项目清单文件中配置对应的OAuth授权范围,且你使用的API端点有误。

解决步骤

1. 配置项目授权范围

在Apps Script编辑器中进入「项目设置」,勾选「在编辑器中显示appsscript.json清单文件」,回到编辑器打开自动生成的appsscript.json文件,在oauthScopes数组中添加以下权限(原有项目需要的权限比如表格访问权限请保留):

{
  "timeZone": "你的项目时区",
  "dependencies": {},
  "oauthScopes": [
    "https://www.googleapis.com/auth/spreadsheets.currentonly",
    "https://www.googleapis.com/auth/script.projects",
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "runtimeVersion": "V8"
}

2. 使用正确的API端点与请求方式

你之前使用的scriptmanagement.googleapis.com不是正确的操作端点,操作Apps Script项目请使用script.googleapis.com端点,修改脚本名称的示例代码如下:

function renameBoundScript(newName) {
  const scriptId = ScriptApp.getScriptId();
  const accessToken = ScriptApp.getOAuthToken();
  const url = `https://script.googleapis.com/v1/projects/${scriptId}`;
  
  const options = {
    method: "PATCH",
    headers: {
      Authorization: `Bearer ${accessToken}`,
      "Content-Type": "application/json"
    },
    payload: JSON.stringify({
      title: newName
    }),
    muteHttpExceptions: true
  };
  
  const response = UrlFetchApp.fetch(url, options);
  console.log("响应码:" + response.getResponseCode());
  console.log("响应内容:" + response.getContentText());
}

调用renameBoundScript("你的自定义脚本名称")即可完成绑定脚本的重命名,避免重名问题。

如果需要备份脚本的全部代码内容,把请求方法改为GET,请求路径调整为https://script.googleapis.com/v1/projects/${scriptId}/content即可拉取到所有代码文件的内容,完成备份操作。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 11:48:03