Layanan Drive Lanjutan
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Layanan Drive lanjutan memungkinkan Anda menggunakan
Google Drive API di Apps Script. Seperti layanan Drive bawaan Apps Script, API ini memungkinkan skrip membuat, menemukan, dan mengubah file serta folder di Google Drive. Dalam sebagian besar kasus, layanan bawaan lebih mudah digunakan, tetapi layanan lanjutan ini menyediakan beberapa fitur tambahan, termasuk akses ke properti file kustom serta revisi untuk file dan folder.
Referensi
Untuk mengetahui informasi mendetail tentang layanan ini, lihat dokumentasi
referensi untuk Google Drive API. Seperti semua
layanan lanjutan di Apps Script, layanan
Drive lanjutan menggunakan objek, metode, dan parameter yang sama seperti
API publik. Untuk mengetahui informasi selengkapnya, lihat Cara tanda tangan metode ditentukan.
Untuk melaporkan masalah dan menemukan dukungan lainnya, lihat panduan dukungan Drive API.
Kode contoh
Contoh kode di bagian ini menggunakan versi 3
API.
Upload file
Contoh kode berikut menunjukkan cara menyimpan file ke Drive pengguna.
Mencantumkan folder
Contoh kode berikut menunjukkan cara mencantumkan folder tingkat teratas di Drive pengguna. Perhatikan penggunaan token halaman untuk mengakses daftar lengkap hasil.
Mencantumkan revisi
Contoh kode berikut menunjukkan cara mencantumkan revisi untuk file tertentu. Perhatikan
bahwa beberapa file dapat memiliki beberapa revisi dan Anda harus menggunakan token halaman untuk
mengakses daftar lengkap hasil.
Menambahkan properti file
Contoh kode berikut menggunakan kolom appProperties
untuk menambahkan properti kustom ke file. Properti kustom hanya dapat dilihat oleh skrip. Untuk menambahkan
properti kustom ke file yang juga dapat dilihat oleh aplikasi lain, gunakan
kolom properties
. Untuk mengetahui informasi selengkapnya, lihat Menambahkan properti file kustom.
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-31 UTC.
[null,null,["Terakhir diperbarui pada 2025-08-31 UTC."],[[["\u003cp\u003eThe advanced Drive service in Apps Script provides more features than the built-in service, like access to custom file properties and revisions.\u003c/p\u003e\n"],["\u003cp\u003eThis advanced service requires enabling before use and mirrors the functionality of the Google Drive API.\u003c/p\u003e\n"],["\u003cp\u003eCode samples demonstrate how to upload files, list folders and revisions, and add custom properties to files in Google Drive using this service.\u003c/p\u003e\n"],["\u003cp\u003eThe provided samples utilize version 3 of the Google Drive API and illustrate common Drive operations within Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eFor comprehensive details, refer to the Google Drive API reference documentation and support guide.\u003c/p\u003e\n"]]],[],null,["# Advanced Drive Service\n\nThe advanced Drive service lets you use the\n[Google Drive API](/drive/web) in Apps Script. Much like\nApps Script's [built-in Drive\nservice](/apps-script/reference/drive), this API allows scripts to create, find,\nand modify files and folders in Google Drive. In most cases, the built-in\nservice is easier to use, but this advanced service provides a few extra\nfeatures, including access to custom file properties as well as revisions for\nfiles and folders.\n| **Note:** This is an advanced service that must be [enabled before\n| use](/apps-script/guides/services/advanced#enable_advanced_services).\n\nReference\n---------\n\nFor detailed information on this service, see the [reference\ndocumentation](/drive/api/reference/rest/v3) for the Google Drive API. Like all\nadvanced services in Apps Script, the advanced\nDrive service uses the same objects, methods, and parameters as\nthe public API. For more information, see [How method signatures are\ndetermined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the [Drive API support\nguide](/drive/api/support).\n\nSample code\n-----------\n\nThe code samples in this section use [version 3](/drive/api/reference/rest/v3)\nof the API.\n\n### Upload files\n\nThe following code sample shows how to save a file to a user's\nDrive. \nadvanced/drive.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/drive.gs) \n\n```javascript\n/**\n * Uploads a new file to the user's Drive.\n */\nfunction uploadFile() {\n try {\n // Makes a request to fetch a URL.\n const image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();\n let file = {\n name: 'google_logo.png',\n mimeType: 'image/png'\n };\n // Create a file in the user's Drive.\n file = Drive.Files.create(file, image, {'fields': 'id,size'});\n console.log('ID: %s, File size (bytes): %s', file.id, file.size);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to upload file with error %s', err.message);\n }\n}\n```\n\n### List folders\n\nThe following code sample shows how to list the top-level folders in the user's\nDrive. Note the use of page tokens to access the full list of\nresults. \nadvanced/drive.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/drive.gs) \n\n```javascript\n/**\n * Lists the top-level folders in the user's Drive.\n */\nfunction listRootFolders() {\n const query = '\"root\" in parents and trashed = false and ' +\n 'mimeType = \"application/vnd.google-apps.folder\"';\n let folders;\n let pageToken = null;\n do {\n try {\n folders = Drive.Files.list({\n q: query,\n pageSize: 100,\n pageToken: pageToken\n });\n if (!folders.files || folders.files.length === 0) {\n console.log('All folders found.');\n return;\n }\n for (let i = 0; i \u003c folders.files.length; i++) {\n const folder = folders.files[i];\n console.log('%s (ID: %s)', folder.name, folder.id);\n }\n pageToken = folders.nextPageToken;\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed with error %s', err.message);\n }\n } while (pageToken);\n}\n```\n\n### List revisions\n\nThe following code sample shows how to list the revisions for a given file. Note\nthat some files can have several revisions and you should use page tokens to\naccess the full list of results. \nadvanced/drive.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/drive.gs) \n\n```javascript\n/**\n * Lists the revisions of a given file.\n * @param {string} fileId The ID of the file to list revisions for.\n */\nfunction listRevisions(fileId) {\n let revisions;\n const pageToken = null;\n do {\n try {\n revisions = Drive.Revisions.list(\n fileId,\n {'fields': 'revisions(modifiedTime,size),nextPageToken'});\n if (!revisions.revisions || revisions.revisions.length === 0) {\n console.log('All revisions found.');\n return;\n }\n for (let i = 0; i \u003c revisions.revisions.length; i++) {\n const revision = revisions.revisions[i];\n const date = new Date(revision.modifiedTime);\n console.log('Date: %s, File size (bytes): %s', date.toLocaleString(),\n revision.size);\n }\n pageToken = revisions.nextPageToken;\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed with error %s', err.message);\n }\n } while (pageToken);\n}\n```\n\n### Add file properties\n\nThe following code sample uses the `appProperties` field to add a custom\nproperty to a file. The custom property is only visible to the script. To add a\ncustom property to the file that's also visible to other apps, use the\n`properties` field, instead. For more information, see [Add custom file\nproperties](/drive/api/guides/properties). \nadvanced/drive.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/drive.gs) \n\n```javascript\n/**\n * Adds a custom app property to a file. Unlike Apps Script's DocumentProperties,\n * Drive's custom file properties can be accessed outside of Apps Script and\n * by other applications; however, appProperties are only visible to the script.\n * @param {string} fileId The ID of the file to add the app property to.\n */\nfunction addAppProperty(fileId) {\n try {\n let file = {\n 'appProperties': {\n 'department': 'Sales'\n }\n };\n // Updates a file to add an app property.\n file = Drive.Files.update(file, fileId, null, {'fields': 'id,appProperties'});\n console.log(\n 'ID: %s, appProperties: %s',\n file.id,\n JSON.stringify(file.appProperties, null, 2));\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed with error %s', err.message);\n }\n}\n```"]]