Google Apps 脚本快速入门

快速入门介绍了如何设置和运行调用 Google Workspace API。

Google Workspace 快速入门使用 API 客户端库来处理一些 身份验证和授权流程的详细信息。我们建议 您需要为自己的应用使用客户端库本快速入门使用 适用于测试的简化身份验证方法 环境对于生产环境,我们建议您了解 身份验证和授权 早于 选择访问凭据 适合您应用的广告格式

创建 Google Apps 脚本 向 Google 文档 API 发出请求。

目标

  • 配置环境。
  • 创建并配置脚本。
  • 运行脚本。

前提条件

  • Google 账号
  • 访问 Google 云端硬盘

创建脚本

  1. 前往 script.google.com/create 创建新脚本。
  2. 将脚本编辑器的内容替换为以下代码:

docs/quickstart/quickstart.gs
/**
 * Prints the title of the sample document:
 * https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit
 * @see https://developers.google.com/docs/api/reference/rest/v1/documents/get
 */
function printDocTitle() {
  const documentId = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE';
  try {
    // Get the document using document id
    const doc = Docs.Documents.get({'includeTabsContent': true}, documentId);
    // Log the title  of document.
    console.log('The title of the doc is: %s', doc.title);
  } catch (err) {
    // TODO (developer) - Handle exception from Docs API
    console.log('Failed to found document with an error %s', err.message);
  }
}

  1. 点击“保存”
  2. 点击未命名项目,输入 快速入门,然后点击重命名

配置脚本

启用 Google 文档 API

  1. 打开 Apps 脚本项目。
  1. 点击 Editor
  2. 服务旁边,点击“添加服务”
  3. 选择 Google 文档 API 然后点击添加

运行示例

在 Apps 脚本编辑器中,点击运行

首次运行该示例时,它会提示您授予访问权限:

  1. 点击查看权限
  2. 选择账号。
  3. 点击允许

脚本的执行日志显示在窗口底部。

<ph type="x-smartling-placeholder"> <ph type="x-smartling-placeholder">

后续步骤