拡張ドライブ サービス

高度なドライブ サービスを使用すると、Apps Script で Google Drive API を使用できます。Apps Script の組み込み Drive サービスと同様に、この API を使用すると、スクリプトで Google ドライブのファイルやフォルダの作成、検索、変更を行うことができます。ほとんどの場合、組み込みサービスの方が使いやすいですが、この高度なサービスでは、カスタム ファイル プロパティへのアクセスや、ファイルとフォルダの変更履歴など、いくつかの追加機能が提供されます。

リファレンス

このサービスの詳細については、Google Drive API のリファレンス ドキュメントをご覧ください。Apps Script のすべての高度なサービスと同様に、高度なドライブ サービスでは、公開 API と同じオブジェクト、メソッド、パラメータを使用します。詳しくは、メソッド シグネチャの決定方法をご覧ください。

問題を報告してその他のサポートを受けるには、Drive API サポートガイドをご覧ください。

サンプルコード

このセクションのコードサンプルでは、API のバージョン 3 を使用します。

ファイルをアップロード

次のコードサンプルは、ユーザーのドライブにファイルを保存する方法を示しています。

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);   } }