Add container-bound script to a Google Docs file programmatically

人走茶凉 提交于 2020-05-15 05:43:18

问题


I am uploading files to Google Drive using the Drive REST API. I would like to have some default add-ons enabled on those files by default. I was reading about container-bound scripts here.

But it is only possible to add container-bound script after opening the file in Google Docs UI. Is there any way to bind a script to a file while uploading or after uploading programmatically?


回答1:


You can create the project of container-bound script type in Google Docs using Apps Script API. And when you have some scripts and you want to add the scripts in the project of container-bound script type, please do the following flow.

1. Create new project in Google Docs

You can create it using Method: projects.create.

Endpoint :

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

Request body :

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

2. Import scripts in created project

You can import the scripts in the created project using Method: projects.updateContent.

Endpoint :

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

Request body :

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

Note :

  • In order to use this API, please include https://www.googleapis.com/auth/script.projects in the scopes.

References :

  • Method: projects.create
  • Method: projects.updateContent

If this was not what you want, I'm sorry.



来源:https://stackoverflow.com/questions/50040686/add-container-bound-script-to-a-google-docs-file-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!