設定 Apps Script 專案

如要設定 Apps Script 專案,透過 REST 呼叫直接呼叫 Google Forms API,需要完成幾個步驟。假設您已設定 Google Cloud 專案,請執行下列操作:

  1. 建立新的 Apps Script 專案。
  2. 將相關聯的 Google Cloud 專案編號變更為您啟用 Forms API 的專案。
  3. 編輯資訊清單檔案 (appsscript.json),新增必要的 OAuth 範圍。
  4. 新增 Apps Script 程式碼,擷取 OAuth 權杖並使用該權杖發出 REST 呼叫。

建立及設定新的 Apps Script 專案

  1. 使用設定 Google Cloud 專案時的 Google 帳戶,前往 Apps Script 資訊主頁,然後按一下「New project」(新專案)
  2. 開啟專案後,按一下「專案設定」
  3. 選取「在編輯器中顯示『appsscript.json』資訊清單檔案」核取方塊。
  4. 在「Google Cloud Platform (Google Cloud) 專案」部分,按一下「變更專案」,然後輸入您為 Forms API 設定的 Google Cloud 專案編號。

您的 Apps Script 專案現已設定完成,可以存取 Forms API 了。下一個必要步驟是新增適當的 OAuth 範圍。

新增 OAuth 範圍

如要在 Apps Script 中產生範圍正確的 OAuth 權杖,請在專案的資訊清單檔案中設定必要範圍。

  1. 在編輯器中開啟 appsscript.json
  2. 將範圍新增至資訊清單主體。

    {
      ...
    "oauthScopes": [
        "https://www.googleapis.com/auth/script.external_request",
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/drive.readonly",
        "https://www.googleapis.com/auth/forms.body",
        "https://www.googleapis.com/auth/forms.body.readonly",
        "https://www.googleapis.com/auth/forms.responses.readonly"
      ],
     ...
     }
    
  3. 按一下 儲存專案,並視需要修正語法錯誤。現在專案應該可以透過 REST 呼叫呼叫 Forms API。

新增 Apps Script 程式碼來呼叫 API

編寫呼叫表單的程式碼前,您需要找出自己擁有的表單,並記下表單 ID。編輯表單時,您可以在網址中找到表單 ID:

https://docs.google.com/forms/d/<FORM_ID>/edit

如要呼叫 API,請使用 Apps Script UrlFetchApp 呼叫。

  1. 開啟 Code.gs 並新增下列程式碼:

    forms-api/snippets/retrieve_all_responses.gs
    function callFormsAPI() {
      console.log("Calling the Forms API!");
      const formId = "<YOUR_FORM_ID>";
    
      // Get OAuth Token
      const OAuthToken = ScriptApp.getOAuthToken();
      console.log(`OAuth token is: ${OAuthToken}`);
    
      const formsAPIUrl = `https://forms.googleapis.com/v1/forms/${formId}/responses`;
      console.log(`formsAPIUrl is: ${formsAPIUrl}`);
    
      const options = {
        headers: {
          Authorization: `Bearer ${OAuthToken}`,
          Accept: "application/json",
        },
        method: "get",
      };
    
      const response = UrlFetchApp.fetch(formsAPIUrl, options);
      console.log(`Response from forms.responses was: ${response}`);
    }
  2. YOUR_FORM_ID 換成您稍早記下的值。

    範例:var formId = 'tL5ygBC8zpbTnTp76JCZdIg80hA-cnpbTnTjnsewCKJH';

  3. 按一下「儲存專案」,並視需要修正語法錯誤。

測試程式碼

  1. 按一下「執行」
  2. 使用與先前相同的 Google 帳戶,視需要授權專案。

啟動後,您應該會在「執行記錄」中看到類似以下內容的回應:

Execution started
Calling the Forms API!
OAuth token is: ya29.a0ARrdaM8IMjtlv…
formsAPIUrl is: https://forms.googleapis.com/v1beta/forms/…/responses
Response from Forms.responses was: {
"responses": [
    {
      "responseId":"...",
      "createTime": "2021-03-25T01:23:58.146Z",
      "lastSubmittedTime": "2021-03-25T01:23:58.146607Z",
      "answers": {
        "1e9b0ead": {
          "questionId": "1e9b0ead",
          "textAnswers": {
            "answers": [
              {
                "value": "Red"
              }
            ]
          }
        },
        "773ed8f3": {
          "questionId": "773ed8f3",
          "textAnswers": {
            "answers": [
              {
                "value": "Tesla"
              }
            ]
          }
        }
      }
    }
  ]
}
Execution completed

後續步驟

使用 Apps Script 成功呼叫 API 後,請參閱參考文件,並嘗試對 API 發出其他呼叫。