고급 Drive 서비스를 통해 Apps Script에서 Google Drive API를 사용할 수 있습니다. Apps Script의 기본 제공 Drive 서비스와 마찬가지로 이 API를 사용하면 스크립트가 Google Drive에서 파일과 폴더를 만들고, 찾고, 수정할 수 있습니다. 대부분의 경우 내장 서비스가 더 사용하기 쉽지만 이 고급 서비스는 맞춤 파일 속성 액세스, 파일 및 폴더의 버전 등 몇 가지 추가 기능을 제공합니다.
참조
이 서비스에 관한 자세한 내용은 Google Drive API의 참조 문서를 참고하세요. Apps Script의 모든 고급 서비스와 마찬가지로 고급 Drive 서비스는 공개 API와 동일한 객체, 메서드, 매개변수를 사용합니다. 자세한 내용은 메서드 서명을 결정하는 방법을 참고하세요.
/** * Uploads a new file to the user's Drive. */functionuploadFile(){try{// Makes a request to fetch a URL.constimage=UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();letfile={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,Filesize(bytes):%s',file.id,file.size);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedtouploadfilewitherror%s',err.message);}}
폴더 나열
다음 코드 샘플은 사용자의 Drive에서 최상위 폴더를 나열하는 방법을 보여줍니다. 페이지 토큰을 사용하여 전체 결과 목록에 액세스합니다.
/** * Lists the top-level folders in the user's Drive. */functionlistRootFolders(){constquery='"root"inparentsandtrashed=falseand'+'mimeType="application/vnd.google-apps.folder"';letfolders;letpageToken=null;do{try{folders=Drive.Files.list({q:query,pageSize:100,pageToken:pageToken});if(!folders.files||folders.files.length===0){console.log('Allfoldersfound.');return;}for(leti=0;i < folders.files.length;i++){constfolder=folders.files[i];console.log('%s(ID:%s)',folder.name,folder.id);}pageToken=folders.nextPageToken;}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedwitherror%s',err.message);}}while(pageToken);}
수정사항 나열
다음 코드 샘플은 지정된 파일의 버전을 나열하는 방법을 보여줍니다. 일부 파일에는 여러 버전이 있을 수 있으며 전체 결과 목록에 액세스하려면 페이지 토큰을 사용해야 합니다.
/** * Lists the revisions of a given file. * @param {string} fileId The ID of the file to list revisions for. */functionlistRevisions(fileId){letrevisions;constpageToken=null;do{try{revisions=Drive.Revisions.list(fileId,{'fields':'revisions(modifiedTime,size),nextPageToken'});if(!revisions.revisions||revisions.revisions.length===0){console.log('Allrevisionsfound.');return;}for(leti=0;i < revisions.revisions.length;i++){constrevision=revisions.revisions[i];constdate=newDate(revision.modifiedTime);console.log('Date:%s,Filesize(bytes):%s',date.toLocaleString(),revision.size);}pageToken=revisions.nextPageToken;}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedwitherror%s',err.message);}}while(pageToken);}
파일 속성 추가
다음 코드 샘플은 appProperties 필드를 사용하여 파일에 맞춤 속성을 추가합니다. 맞춤 속성은 스크립트에만 표시됩니다. 다른 앱에도 표시되는 맞춤 속성을 파일에 추가하려면 대신 properties 필드를 사용하세요. 자세한 내용은 커스텀 파일 속성 추가를 참고하세요.
/** * 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. */functionaddAppProperty(fileId){try{letfile={'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 exceptionconsole.log('Failedwitherror%s',err.message);}}
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2024-12-21(UTC)"],[[["The advanced Drive service in Apps Script provides more features than the built-in service, like access to custom file properties and revisions."],["This advanced service requires enabling before use and mirrors the functionality of the Google Drive API."],["Code samples demonstrate how to upload files, list folders and revisions, and add custom properties to files in Google Drive using this service."],["The provided samples utilize version 3 of the Google Drive API and illustrate common Drive operations within Apps Script."],["For comprehensive details, refer to the Google Drive API reference documentation and support guide."]]],[]]