以编程方式将容器绑定脚本添加到 Google Docs 文件

Add container-bound script to a Google Docs file programmatically

我正在使用驱动器 REST API 将文件上传到 Google 驱动器。默认情况下,我想在这些文件上启用一些默认加载项。 我正在阅读有关容器绑定脚本的内容 here.

但只有在 Google 文档 UI 中打开文件后才能添加容器绑定脚本。有什么方法可以在上传时或以编程方式上传后将脚本绑定到文件吗?

您可以使用 Apps 脚本 API 在 Google 文档中创建容器绑定脚本类型的项目。而当你有一些脚本,你想在容器绑定脚本类型的项目中添加脚本时,请执行以下流程。

1。在 Google 文档

中创建新项目

您可以使用 Method: projects.create 创建它。

端点:

POST https://script.googleapis.com/v1/projects

请求正文:

{
  "title": string, // project name
  "parentId": string, // file ID of Google Docs
}

2。在创建的项目中导入脚本

您可以使用Method: projects.updateContent在创建的项目中导入脚本。

端点:

PUT https://script.googleapis.com/v1/projects/{scriptId}/content

请求正文:

{
  "files": [
    {
      object(File)
    }
  ],
}

注:

  • 为了使用这个 API,请在范围中包含 https://www.googleapis.com/auth/script.projects

参考资料:

如果这不是你想要的,我很抱歉。