Google Apps Script 快速入門導覽課程

快速入門導覽課程說明如何設定及執行呼叫 Google Workspace API 的應用程式。

Google Workspace 快速入門導覽課程會使用 API 用戶端程式庫,處理驗證和授權流程的一些細節。建議您針對自己的應用程式使用用戶端程式庫。本快速入門導覽課程採用適用於測試環境的簡化驗證方式。對於實際工作環境,建議您先瞭解驗證和授權,再選擇應用程式適用的存取憑證

建立 Google Apps Script,向 Google Contacts API 提出要求。

目標

  • 建立指令碼。
  • 啟用 Google Contacts 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. 按一下「Untitled project」,輸入 quickstart,然後按一下「Rename」

啟用 Google Contacts API

  1. 開啟 Apps Script 專案。
  2. 按一下「Editor」圖示
  3. 按一下「服務」旁邊的「新增服務」圖示
  4. 選取 Google Contacts API,然後按一下「Add」

執行範例

在 Apps Script 編輯器中按一下「執行」

首次執行範例時,系統會提示您授予存取權:

  1. 按一下「查看權限」
  2. 選擇所需帳戶。
  3. 按一下 [Allow]

指令碼的執行記錄會顯示在視窗底部。

後續步驟