Google Apps Script 快速入門導覽課程

快速入門導覽課程說明如何設定及執行會呼叫 Google Workspace API

Google Workspace 快速入門導覽課程會使用 API 用戶端程式庫 驗證和授權流程的詳細資訊建議做法 您為自己的應用程式使用用戶端程式庫本快速入門導覽課程會使用 適合用於測試的簡易驗證方式 環境。在正式環境中,建議您瞭解 驗證與授權選擇存取認證 挑選適合您應用程式的語言版本

撰寫 Google Apps Script 這個 API 會向 Google Docs 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 Docs API

  1. 開啟 Apps Script 專案。
  1. 按一下「編輯器」圖示
  2. 按一下「服務」旁邊的「新增服務」
  3. 選取 Google 文件 API 然後按一下「Add」(新增)

執行範例

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

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

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

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

後續步驟