進階雲端硬碟服務

進階雲端硬碟服務可讓您在 Apps Script 中使用 Google Drive API。與 Apps Script 的內建雲端硬碟服務類似,這個 API 可讓指令碼建立、尋找及修改 Google 雲端硬碟中的檔案和資料夾。在大多數情況下,內建服務較容易使用,但這個進階服務提供幾項額外功能,包括存取自訂檔案屬性,以及檔案和資料夾的修訂版本。

參考資料

如要進一步瞭解這項服務,請參閱 Google Drive API 的參考說明文件。如同 Apps Script 的所有進階服務,進階雲端硬碟服務會使用與公用 API 相同的物件、方法和參數。詳情請參閱「方法簽章的判定方式」。

如要回報問題及尋找其他支援,請參閱 Drive API 支援指南

程式碼範例

本節中的程式碼範例使用的是 3 版的 API。

上傳檔案

以下程式碼範例顯示如何將檔案儲存到使用者的雲端硬碟。

advanced/drive.gs
/**
 * Uploads a new file to the user's Drive.
 */
function uploadFile() {
  try {
    // Makes a request to fetch a URL.
    const image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
    let file = {
      name: 'google_logo.png',
      mimeType: 'image/png'
    };
    // Create a file in the user's Drive.
    file = Drive.Files.create(file, image, {'fields': 'id,size'});
    console.log('ID: %s, File size (bytes): %s', file.id, file.size);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to upload file with error %s', err.message);
  }
}

列出資料夾

以下程式碼範例顯示如何列出使用者雲端硬碟中的頂層資料夾。請注意,使用頁面權杖來存取完整的結果清單。

advanced/drive.gs
/**
 * Lists the top-level folders in the user's Drive.
 */
function listRootFolders() {
  const query = '"root" in parents and trashed = false and ' +
    'mimeType = "application/vnd.google-apps.folder"';
  let folders;
  let pageToken = null;
  do {
    try {
      folders = Drive.Files.list({
        q: query,
        pageSize: 100,
        pageToken: pageToken
      });
      if (!folders.files || folders.files.length === 0) {
        console.log('All folders found.');
        return;
      }
      for (let i = 0; i < folders.files.length; i++) {
        const folder = folders.files[i];
        console.log('%s (ID: %s)', folder.name, folder.id);
      }
      pageToken = folders.nextPageToken;
    } catch (err) {
      // TODO (developer) - Handle exception
      console.log('Failed with error %s', err.message);
    }
  } while (pageToken);
}

列出修訂版本

以下程式碼範例顯示如何列出特定檔案的修訂版本。請注意,某些檔案可能會有多個修訂版本,您應使用頁面符記存取完整的結果清單。

advanced/drive.gs
/**
 * Lists the revisions of a given file.
 * @param {string} fileId The ID of the file to list revisions for.
 */
function listRevisions(fileId) {
  let revisions;
  const pageToken = null;
  do {
    try {
      revisions = Drive.Revisions.list(
          fileId,
          {'fields': 'revisions(modifiedTime,size),nextPageToken'});
      if (!revisions.revisions || revisions.revisions.length === 0) {
        console.log('All revisions found.');
        return;
      }
      for (let i = 0; i < revisions.revisions.length; i++) {
        const revision = revisions.revisions[i];
        const date = new Date(revision.modifiedTime);
        console.log('Date: %s, File size (bytes): %s', date.toLocaleString(),
            revision.size);
      }
      pageToken = revisions.nextPageToken;
    } catch (err) {
      // TODO (developer) - Handle exception
      console.log('Failed with error %s', err.message);
    }
  } while (pageToken);
}

新增檔案屬性

以下程式碼範例使用 appProperties 欄位在檔案中加入自訂屬性。只有指令碼看得到自訂屬性。如要在檔案中為也可供其他應用程式查看的檔案新增自訂屬性,請改用 properties 欄位。詳情請參閱「新增自訂檔案屬性」。

advanced/drive.gs
/**
 * Adds a custom app property to a file. Unlike Apps Script's DocumentProperties,
 * Drive's custom file properties can be accessed outside of Apps Script and
 * by other applications; however, appProperties are only visible to the script.
 * @param {string} fileId The ID of the file to add the app property to.
 */
function addAppProperty(fileId) {
  try {
    let file = {
      'appProperties': {
        'department': 'Sales'
      }
    };
    // Updates a file to add an app property.
    file = Drive.Files.update(file, fileId, null, {'fields': 'id,appProperties'});
    console.log(
        'ID: %s, appProperties: %s',
        file.id,
        JSON.stringify(file.appProperties, null, 2));
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', err.message);
  }
}