匯入及匯出專案

由於 Apps Script 專案位於 Google 雲端硬碟,開發人員可以使用 Google Drive API 匯入及匯出 Apps Script 原始碼 (請勿與 Apps Script 中的 Drive 服務混淆)。

舉例來說,開發人員可以在本機上使用慣用的程式碼編輯器編寫新的 Apps Script 程式碼,並使用 Git 等版本控管系統與其他開發人員協作。最終版本完成後,她可以使用 REST API 將檔案上傳 (匯入) 至 Google 雲端硬碟,並像使用其他 Apps Script 專案一樣使用這些檔案。

您可以在本機版本中變更程式碼,然後使用 Google 雲端硬碟 API 同步至 Apps Script 專案。現有專案可從 Google 雲端硬碟下載 (匯出) 至本機。

功能與限制

如要使用 Google Drive API 匯入或匯出專案,請注意下列事項:

  1. 伺服器端指令碼檔案應以「.gs」結尾。您可能想使用 .js 檔案在本機開發,但請務必先重新命名檔案,加入 .gs 副檔名,再匯入 Google 雲端硬碟。
  2. 用戶端指令碼檔案必須以「.html」結尾。包括用戶端 .html、.js 或 .css 檔案。同樣地,您可以使用其他擴充功能在本機開發,但匯入 Google 雲端硬碟前,請務必使用 .html 擴充功能。
  3. 將專案檔案匯入 Google 雲端硬碟時,這些檔案中的所有現有資料都會遭到覆寫。你無法附加或插入部分文字,必須更新整個檔案。
  4. 伺服器端指令碼檔案必須包含有效的 JavaScript。如果伺服器 .js 檔案發生錯誤,Google Drive API 更新呼叫會失敗,並顯示 5xx 錯誤。如要避免這種情況,請先對程式碼執行 Lint,再匯入程式碼。
  5. 無法匯入空白檔案。如果您嘗試上傳空白檔案,Google 雲端硬碟 API 更新呼叫會失敗,並顯示 5xx 錯誤。
  6. 只能匯入或匯出獨立指令碼。無法透過 Google Drive API 存取繫結至容器的指令碼。
  7. 只能匯入或匯出原始碼。 Google 雲端硬碟 API 也不會公開專案屬性或記錄等資源。您無法透過 Google Drive API 執行指令碼版本管理、發布或執行指令碼等動作。
  8. 您不限於單一伺服器 Code.gs 檔案。您可以將伺服器程式碼分散到多個檔案,方便開發。所有伺服器檔案都會載入相同的全域命名空間,因此如要提供安全封裝,請使用 JavaScript 類別。

Drive API

開發人員可透過 Google Drive API,以程式輔助方式存取 Google 雲端硬碟中的檔案。這個 API 會使用 GET 下載檔案,並使用 PUT/POST 上傳檔案。如需詳細說明文件和快速入門指南,請參閱 Google Drive API 總覽頁面

本指南著重於使用 Files 資源列出及移動檔案,並使用下列呼叫:

授權

所有傳送至 Google Drive API 的要求都必須由經過驗證的使用者透過 OAuth 2.0 通訊協定進行授權。詳情請參閱 Google Drive API 授權說明文件

除了應用程式可能需要的其他範圍 (例如 https://www.googleapis.com/auth/drive),所有嘗試匯入或匯出 Google Apps Script 專案的應用程式都必須要求特殊範圍:

https://www.googleapis.com/auth/drive.scripts

列出現有專案

如要列出雲端硬碟中的所有 Apps Script 專案,請使用 Files 資源查詢 MIME 類型為 application/vnd.google-apps.script 的檔案。 如要篩選回應,只納入您擁有的檔案,請加入搜尋參數 'me' in owners

以下是範例要求和回應,顯示透過 JSON 回應傳回的 Apps Script 專案陣列。

GET https://www.googleapis.com/drive/v2/files?q=mimeType%3D'application%2Fvnd.google-apps.script'+and+'me'+in+owners
Authorization:  Bearer ya29.fakebearerstring
{
 "kind": "drive#fileList",
 "etag": "\"kjsas92/f3zGUXczKMxEB_9ZTMRFOF3d1ZU\"",
 "selfLink": "https://www.googleapis.com/drive/v2/files?q=mimeType%3D'application/vnd.google-apps.script'+and+'me'+in+owners",
 "items": [
  {
   "kind": "drive#file",
   "id": "1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D",
   "etag": "\"kjsas92/MTM3MDk3ODY5ODQyNg\"",
   "selfLink": "https://www.googleapis.com/drive/v2/files/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D",
   "alternateLink": "https://script.google.com/a/google.com/d/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D/edit?usp=drivesdk",
   "iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_11_script_list.png",
   "title": "Mail merge",
   "mimeType": "application/vnd.google-apps.script",
   "description": "",
   "labels": {
    "starred": false,
    "hidden": false,
    "trashed": true,
    "restricted": false,
    "viewed": true
   },
   "createdDate": "2013-06-11T19:24:45.188Z",
   "modifiedDate": "2013-06-11T19:24:58.426Z",
   "modifiedByMeDate": "2013-06-11T19:24:58.426Z",
   "lastViewedByMeDate": "2013-06-11T19:24:58.426Z",
   "parents": [
    {
     "kind": "drive#parentReference",
     "id": "0APdyIOzo7bWDUk9PVA",
     "selfLink": "https://www.googleapis.com/drive/v2/files/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D/parents/0APdyIOzo7bWDUk9PVA",
     "parentLink": "https://www.googleapis.com/drive/v2/files/0APdyIOzo7bWDUk9PVA",
     "isRoot": true
    }
   ],
   "exportLinks": {
    "application/json": "https://script.google.com/feeds/download/export?id=1234567890abcefghijklmnopqrstuvwxyz&format=json"
   },
   "userPermission": {
    "kind": "drive#permission",
    "etag": "\"kjsas92/259X2r5DVstv1CcIQTjt_RQPSW8\"",
    "id": "me",
    "selfLink": "https://www.googleapis.com/drive/v2/files/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D/permissions/me",
    "role": "owner",
    "type": "user"
   },
   "quotaBytesUsed": "0",
   "ownerNames": [
    "John Doe"
   ],
   "owners": [
    {
     "kind": "drive#user",
     "displayName": "John Doe",
     "picture": {
      "url": "https://lh4.googleusercontent.com/-yd1rIb6Pe2Y/AAAAAAAAAAI/AAAAAAAAAGs/PP5vTuZonik/s64/photo.jpg"
     },
     "isAuthenticatedUser": true,
     "permissionId": "1234566789"
    }
   ],
   "lastModifyingUserName": "John Doe",
   "lastModifyingUser": {
    "kind": "drive#user",
    "displayName": "John Doe",
    "picture": {
     "url": "https://lh4.googleusercontent.com/-yd1rIb6Pe2Y/AAAAAAAAAAI/AAAAAAAAAGs/PP5vTuZonik/s64/photo.jpg"
    },
    "isAuthenticatedUser": true,
    "permissionId": "1234566789"
   },
   "editable": true,
   "writersCanShare": true,
   "shared": false,
   "explicitlyTrashed": true,
   "appDataContents": false
  }
 ]
}

如果您知道 Apps Script 專案的檔案 ID,可以直接透過下列 API 呼叫擷取:

GET https://www.googleapis.com/drive/v2/files/1234567890abcefghijklmnopqrstuvwxyz
Authorization:  Bearer ya29.fakebearerstring

從雲端硬碟匯出專案

從 API 取得 File 資源後,exportLinks 屬性會包含要擷取的網址,以取得專案內容的 JSON 資料。這個網址的範例如下:

https://script.google.com/feeds/download/export?id=1234567890abcefghijklmnopqrstuvwxyz&format=json

對這個網址提出要求,即可擷取專案本身的內容。 請務必加入 Authorization 標頭,並使用相同的 OAuth Bearer 權杖

以下是要求和回應範例:

GET https://script.google.com/feeds/download/export?id=1234567890abcefghijklmnopqrstuvwxyz&format=json
Authorization:  Bearer ya29.fakebearerstring
{
  "files": [
    {
      "id":"9basdfbd-749a-4as9b-b9d1-d64basdf803",
      "name":"Code",
      "type":"server_js",
      "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
    },
    {
      "id":"3asf7c0d-1afb-4a9-8431-5asdfc79e7ae",
      "name":"index",
      "type":"html",
      "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    Hello, world!\n  \u003c/body\u003e\n\u003c/html\u003e"
    }
  ]
}

上例包含 HTML 服務指南中的簡單網頁應用程式程式碼。請注意,您會收到 Files 陣列,每個陣列都包含下列 4 個屬性:

id 專案內檔案的內部 ID,更新時需要參照這個檔案。
name 檔案名稱 (不含副檔名),如指令碼編輯器中所示。
type 這兩種檔案分別是 server_js 和 html。
source 檔案中包含的編碼原始碼。

將專案匯入雲端硬碟

如要更新現有專案,請對 update API 檔案發出 HTTP PUT 呼叫,並提供適當的 fileId。以下範例顯示媒體上傳部分的交易範例。使用用戶端程式庫,應用程式就能輕鬆在同一個上傳呼叫中加入中繼資料和媒體。請注意,Content-Type 標頭會指定本例中上傳的內容類型。

PUT https://www.googleapis.com/upload/drive/v2/files/1234567890abcefghijklmnopqrstuvwxyz
Authorization:  Bearer ya29.fakebearerestring
Content-Type:  application/vnd.google-apps.script+json
{
  "files": [
    {
      "id":"9basdfbd-749a-4as9b-b9d1-d64basdf803",
      "name":"Code",
      "type":"server_js",
      "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
    },
    {
      "id":"3asf7c0d-1afb-4a9-8431-5asdfc79e7ae",
      "name":"index",
      "type":"html",
      "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    New message!!\n  \u003c/body\u003e\n\u003c/html\u003e"
    }
  ]
}

在專案中建立新檔案

如要在專案中建立新檔案,請傳送沒有 id 屬性的檔案 PUT 要求。如果檔案含有不明 ID,系統會顯示錯誤訊息。

刪除專案中的檔案

如要從專案中刪除檔案,請傳送不含該檔案的 PUT 要求 (但必須包含專案中的所有其他檔案)。匯入程序中未傳回的檔案會從伺服器刪除。

重新命名專案中的檔案

如要在專案中重新命名檔案,請傳送 PUT 要求,其中包含現有的 id,但使用新的 name。請注意,伺服器會忽略任何變更為 type 的嘗試。

建立新專案

如要建立新專案,請將 POST 要求傳送至檔案 insert API。與 update 呼叫類似,您可以使用用戶端程式庫加入中繼資料,例如專案名稱和說明。

以下是媒體上傳交易的範例。系統會在雲端硬碟中建立名為「Untitled」的專案。網址中必須有 convert 參數。 與 update 呼叫相同,您必須提供 Content-Type 標頭。

POST https://www.googleapis.com/upload/drive/v2/files?convert=true
Authorization:  Bearer ya29.fakebearerestring
Content-Type:  application/vnd.google-apps.script+json
{
  "files": [
    {
      "name":"Code",
      "type":"server_js",
      "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
    },
    {
      "name":"index",
      "type":"html",
      "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    Hello, world!!\n  \u003c/body\u003e\n\u003c/html\u003e"
    }
  ]
}