고급 드라이브 서비스
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
고급 Drive 서비스를 통해 Apps Script에서 Google Drive API를 사용할 수 있습니다. Apps Script의 내장 Drive 서비스와 마찬가지로 이 API를 사용하면 스크립트가 Google Drive에서 파일과 폴더를 만들고, 찾고, 수정할 수 있습니다. 대부분의 경우 내장 서비스를 사용하는 것이 더 쉽지만 이 고급 서비스는 파일 및 폴더의 수정 버전뿐만 아니라 맞춤 파일 속성에 대한 액세스 등 몇 가지 추가 기능을 제공합니다.
참조
이 서비스에 관한 자세한 내용은 Google Drive API의 참고 문서를 참고하세요. Apps Script의 모든 고급 서비스와 마찬가지로 고급 Drive 서비스는 공개 API와 동일한 객체, 메서드, 매개변수를 사용합니다. 자세한 내용은 메서드 서명이 결정되는 방식을 참고하세요.
문제를 신고하고 기타 지원을 받으려면 Drive API 지원 가이드를 참고하세요.
샘플 코드
이 섹션의 코드 샘플은 API의 버전 3을 사용합니다.
파일 업로드
다음 코드 샘플은 사용자의 Drive에 파일을 저장하는 방법을 보여줍니다.
폴더 나열
다음 코드 샘플은 사용자의 Drive에 있는 최상위 폴더를 나열하는 방법을 보여줍니다. 결과의 전체 목록에 액세스하기 위해 페이지 토큰을 사용합니다.
버전 나열
다음 코드 샘플은 지정된 파일의 버전을 나열하는 방법을 보여줍니다. 일부 파일에는 여러 버전이 있을 수 있으며 페이지 토큰을 사용하여 전체 결과 목록에 액세스해야 합니다.
파일 속성 추가
다음 코드 샘플에서는 appProperties
필드를 사용하여 파일에 맞춤 속성을 추가합니다. 맞춤 속성은 스크립트에만 표시됩니다. 다른 앱에도 표시되는 맞춤 속성을 파일에 추가하려면 대신 properties
필드를 사용하세요. 자세한 내용은 맞춤 파일 속성 추가를 참고하세요.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-08-31(UTC)
[null,null,["최종 업데이트: 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```"]]