首页 > 解决方案 > 如何使用 Google Docs API 编辑 Google Docs 标题?

问题描述

我已经尝试过 Create、batchUpdate、从https://developers.google.com/docs/api/how-tos/overview获取。

即使在batchUpdate我没有看到编辑选项title。我用它来编辑文档的内容 - 插入/删除,但不是标题。

鉴于我有文档 ID,如何编辑文档标题?有没有办法使用 API 编辑文档标题?

标签: google-docsgoogle-docs-api

解决方案


我相信你的目标如下。

  • 您想修改 Google 文档的标题。
    • 即,您要修改 Google Document 的文件名。

为此,这个答案怎么样?

为了修改 Google Document 的文件名,您可以使用 Drive API 中的“Files:update”方法来实现。遗憾的是,Google Docs API 不能用于修改 Google Document 的文件名。

端点:

PATCH https://www.googleapis.com/drive/v3/files/fileId

示例请求正文:

{"name":"updated title"}

示例卷曲:

curl --request PATCH \
  'https://www.googleapis.com/drive/v3/files/{documentId}' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"name":"updated title"}' \
  --compressed

参考:

添加:

当您想使用浏览器请求时,您可以使用此快速入门进行授权,并使用Files: update来修改标题。作为示例脚本,我将向您展示 Files:update for Javascript 方法的示例脚本,如下所示。请使用快速入门中的授权脚本。

示例脚本:

gapi.client.drive.files.update({
  fileId: fileId,
  resource: {name: "updated title"},
}).then((response) => {
  console.log(response);
});

推荐阅读