Google Apps 脚本快速入门

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

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

创建 Google Apps 脚本,用于向 GoogleSlide API 发出请求。

目标

  • 创建脚本。
  • 启用 GoogleSlide API。
  • 运行示例。

前提条件

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

创建脚本

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

slides/quickstart/quickstart.gs
/**
 * Creates a Slides API service object and logs the number of slides and
 * elements in a sample presentation:
 * https://docs.google.com/presentation/d/1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc/edit
 */
function logSlidesAndElements() {
  const presentationId = '1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc';
  try {
    // Gets the specified presentation using presentationId
    const presentation = Slides.Presentations.get(presentationId);
    const slides = presentation.slides;
    // Print the number of slides and elements in presentation
    console.log('The presentation contains %s slides:', slides.length);
    for ( let i = 0; i < slides.length; i++) {
      console.log('- Slide # %s contains %s elements.', i + 1, slides[i].pageElements.length);
    }
  } catch (err) {
    // TODO (developer) - Handle  Presentation.get() exception from Slides API
    console.log('Failed to found Presentation with error %s', err.message);
  }
}

  1. 点击“保存”
  2. 点击未命名项目,输入 quickstart,然后点击 Rename

启用 GoogleSlides API

  1. 打开 Apps 脚本项目。
  2. 点击 Editor 图标
  3. 点击服务旁边的“添加服务”图标
  4. 选择 GoogleSlide API,然后点击添加

运行示例

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

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

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

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

后续步骤