進階雲端硬碟服務

進階雲端硬碟服務能讓您使用 使用 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);
  }
}