展示操作

您可以使用 Googleslides API 创建演示文稿文件和管理现有文件。本页面上的示例展示了使用 presentations.batchUpdate 方法的一些常见页面呈现操作。

这些示例以 HTTP 请求的形式呈现,与语言无关。如需了解如何使用 Google API 客户端库以不同语言实现批量更新,请参阅以下指南:

创建演示文稿

以下 presentations.create 代码示例展示了如何创建名为“My New Presentation”的空白演示文稿文件。

您还可以使用 Google Drive API files.create 方法创建空白演示文稿文件,只需将 application/vnd.google-apps.presentation 指定为 MIME 类型即可。

以下是创建演示文稿的请求协议:

POST https://slides.googleapis.com/v1/presentations
{
  "title": "My New Presentation"
}

列出现有的演示文稿文件

Google 幻灯片 API 不提供检索演示文稿列表的方法,但 Drive API 可提供检索方法。files.list 方法(如下所示)使用字段掩码从云端硬盘返回演示文稿文件列表,其中包含文件 ID、文件名和用于打开文件的链接。该请求还将 application/vnd.google-apps.presentation 指定为 MIME 类型

以下是列出现有演示文稿文件的请求协议:

GET https://www.googleapis.com/drive/v3/files?q="mimeType=application/vnd.google-apps.presentation"&fields=files(id,name,webViewLink)

此请求的响应具有以下结构:

{
 "files": [
    {
     "id": "abcdefghijklmnopqrstuvwxyz0123456789",
     "name": "Project Vision",
     "webViewLink": "https://docs.google.com/a/google.com/presentation/d/abcdefghijklmnopqrstuvwxyz0123456789/edit?usp=drivesdk"
    },
    {
     "id": "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
     "name": "Untitled Presentation",
     "webViewLink": "https://docs.google.com/a/google.com/presentation/d/ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/edit?usp=drivesdk"
    },
    ...
  ]
}

替换整个演示文稿中的文本

以下 presentations.batchUpdate 代码示例展示了如何在 PRESENTATION_ID 指定的演示文稿中使用 ReplaceAllTextRequest 方法替换文本。您可以从演示文稿网址中发现演示文稿 ID 的值。

字符串“Gizmo Corp.”的每个实例都会替换为文本“小工具 Inc.”。这包括文本框和其他形状中的文本、幻灯片上以及母版中的文本。在这种情况下,通过将 matchCase 设置为 true,文本替换会区分大小写。

以下是替换演示文稿中文本的请求协议:

POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{
  "requests": [
    {
      "replaceAllText": {
          "containsText": {
            "text": "Gizmo Corp.",
            "matchCase": true
          },
          "replaceText": "Gadget Inc."
      }
    }
  ]
}