Class DriveApp

DriveApp

스크립트가 Google Drive에 있는 파일과 폴더를 만들고 찾고 수정할 수 있도록 허용합니다. 파일에 액세스하거나 폴더를 사용하려면 고급 Drive 서비스를 사용하세요.

// Logs the name of every file in the user's Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
  var file = files.next();
  console.log(file.getName());
}

속성

속성유형설명
AccessAccess개별 사용자 외에 파일 또는 폴더에 액세스할 수 있는 사용자 클래스를 나타내는 enum입니다. 명시적으로 액세스 권한을 부여받은 사용자입니다.
PermissionPermission파일 또는 폴더에 액세스할 수 있는 사용자에게 부여된 권한을 나타내는 열거형입니다. 명시적으로 액세스 권한이 부여된 모든 개별 사용자입니다.

메서드

메서드반환 유형간략한 설명
continueFileIterator(continuationToken)FileIterator이전 반복자의 연속 토큰을 사용하여 파일 반복을 재개합니다.
continueFolderIterator(continuationToken)FolderIterator이전 반복자의 연속 토큰을 사용하여 폴더 반복을 재개합니다.
createFile(blob)File임의 데이터의 지정된 Blob에서 사용자 Drive의 루트에 파일을 만듭니다.
createFile(name, content)File특정 이름과 콘텐츠로 사용자 Drive의 루트에 텍스트 파일을 만듭니다.
createFile(name, content, mimeType)File지정된 이름, 콘텐츠, MIME 유형으로 사용자 Drive의 루트에 파일을 만듭니다.
createFolder(name)Folder사용자 Drive의 루트에 특정 이름으로 폴더를 만듭니다.
createShortcut(targetId)File제공된 Drive 항목 ID의 바로가기를 만들어 반환합니다.
createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)File제공된 Drive 항목 ID와 리소스 키의 바로가기를 만들어 반환합니다.
enforceSingleParent(value)void항목 상위 요소에 영향을 주는 모든 호출에 대해EnforcementSingleParent 동작을 사용 설정하거나 사용 중지합니다.
getFileById(id)File지정된 ID가 있는 파일을 가져옵니다.
getFileByIdAndResourceKey(id, resourceKey)File지정된 ID와 리소스 키가 있는 파일을 가져옵니다.
getFiles()FileIterator사용자 드라이브에 있는 모든 파일의 컬렉션을 가져옵니다.
getFilesByName(name)FileIterator지정된 이름을 가진 사용자 드라이브에 있는 모든 파일 모음을 가져옵니다.
getFilesByType(mimeType)FileIterator지정된 MIME 유형을 가진 사용자 드라이브에 있는 모든 파일의 컬렉션을 가져옵니다.
getFolderById(id)Folder지정된 ID가 있는 폴더를 가져옵니다.
getFolderByIdAndResourceKey(id, resourceKey)Folder지정된 ID와 리소스 키가 있는 폴더를 가져옵니다.
getFolders()FolderIterator사용자 드라이브에 있는 모든 폴더의 컬렉션을 가져옵니다.
getFoldersByName(name)FolderIterator지정된 이름을 가진 사용자 드라이브에 있는 모든 폴더의 컬렉션을 가져옵니다.
getRootFolder()Folder사용자 Drive의 루트에 있는 폴더를 가져옵니다.
getStorageLimit()Integer사용자가 드라이브에 저장할 수 있는 바이트 수를 가져옵니다.
getStorageUsed()Integer사용자가 현재 드라이브에 저장 중인 바이트 수를 가져옵니다.
getTrashedFiles()FileIterator사용자 드라이브의 휴지통에 있는 모든 파일의 컬렉션을 가져옵니다.
getTrashedFolders()FolderIterator사용자 드라이브의 휴지통에 있는 모든 폴더의 컬렉션을 가져옵니다.
searchFiles(params)FileIterator지정된 검색어와 일치하는 사용자 Drive의 모든 파일 모음을 가져옵니다. 있습니다.
searchFolders(params)FolderIterator지정된 검색과 일치하는 사용자 Drive의 모든 폴더 컬렉션을 가져옵니다. 있습니다.

자세한 문서

continueFileIterator(continuationToken)

이전 반복자의 연속 토큰을 사용하여 파일 반복을 재개합니다. 이 메서드는 이는 한 실행에서 반복자를 처리하는 것이 최대 실행 시간을 초과하는 경우에 유용합니다. 연속 토큰은 일반적으로 1주일 동안 유효합니다.

// Continues getting a list of all 'Untitled document' files in the user's Drive.
// Creates a file iterator named 'previousIterator'.
const previousIterator = DriveApp.getFilesByName('Untitled document');

// Gets continuation token from the previous file iterator.
const continuationToken = previousIterator.getContinuationToken();

// Creates a new iterator using the continuation token from the previous file iterator.
const newIterator = DriveApp.continueFileIterator(continuationToken);

// Resumes the file iteration using a continuation token from 'firstIterator' and
// logs the file name.
if (newIterator.hasNext()) {
  const file = newIterator.next();
  console.log(file.getName());
}

매개변수

이름유형설명
continuationTokenString이전 파일 반복자의 연속 토큰입니다.

리턴

FileIterator - 연속 토큰이 생성될 때 이전 반복자에 남아 있던 파일 모음입니다. 알 수 있습니다.


continueFolderIterator(continuationToken)

이전 반복자의 연속 토큰을 사용하여 폴더 반복을 재개합니다. 이 메서드는 이는 한 실행에서 반복자를 처리하는 것이 최대 실행 시간을 초과하는 경우에 유용합니다. 연속 토큰은 일반적으로 1주일 동안 유효합니다.

// Continues getting a list of all folders in user's Drive.
// Creates a folder iterator named 'previousIterator'.
const previousIterator = DriveApp.getFolders();

// Gets continuation token from the previous folder iterator.
const continuationToken = previousIterator.getContinuationToken();

// Creates a new iterator using the continuation token from the previous folder iterator.
const newIterator = DriveApp.continueFolderIterator(continuationToken);

// Resumes the folder iteration using a continuation token from the previous iterator and logs
// the folder name.
if (newIterator.hasNext()) {
  const folder = newIterator.next();
  console.log(folder.getName());
}

매개변수

이름유형설명
continuationTokenString이전 폴더 반복자의 연속 토큰입니다.

리턴

FolderIterator - 계속될 때 이전 반복자에 남아 있던 폴더 모음입니다. 토큰이 생성되었습니다.


createFile(blob)

임의 데이터의 지정된 Blob에서 사용자 Drive의 루트에 파일을 만듭니다.

// Create an image file in Google Drive using the Maps service.
var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
DriveApp.createFile(blob);

매개변수

이름유형설명
blobBlobSource새 파일의 데이터입니다.

리턴

File - 새 파일입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive

createFile(name, content)

지정된 이름과 콘텐츠로 사용자 Drive의 루트에 텍스트 파일을 만듭니다. 발생 content가 50MB보다 큰 경우에는 예외입니다.

// Create a text file with the content "Hello, world!"
DriveApp.createFile('New Text File', 'Hello, world!');

매개변수

이름유형설명
nameString새 파일의 이름입니다.
contentString새 파일의 콘텐츠입니다.

리턴

File - 새 파일입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive

createFile(name, content, mimeType)

지정된 이름, 콘텐츠, MIME 유형으로 사용자 Drive의 루트에 파일을 만듭니다. 발생 content이 10MB보다 크면 예외가 발생합니다.

// Create an HTML file with the content "Hello, world!"
DriveApp.createFile('New HTML File', '<b>Hello, world!</b>', MimeType.HTML);

매개변수

이름유형설명
nameString새 파일의 이름입니다.
contentString새 파일의 콘텐츠입니다.
mimeTypeString새 파일의 MIME 유형입니다.

리턴

File - 새 파일입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive

createFolder(name)

사용자 Drive의 루트에 특정 이름으로 폴더를 만듭니다.

매개변수

이름유형설명
nameString새 폴더의 이름입니다.

리턴

Folder - 새 폴더입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive

createShortcut(targetId)

제공된 Drive 항목 ID의 바로가기를 만들어 반환합니다.

매개변수

이름유형설명
targetIdString대상 파일 또는 폴더의 파일 ID입니다.

리턴

File - 새로운 단축키입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive

createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)

제공된 Drive 항목 ID와 리소스 키의 바로가기를 만들어 반환합니다. 리소스 키는 대상 파일 또는 폴더에 액세스하기 위해 전달해야 하는 추가 매개변수입니다. 링크를 사용하여 공유되었습니다.

// Creates shortcuts for all folders in the user's drive that have a specific name.
// TODO(developer): Replace 'Test-Folder' with a valid folder name in your drive.
const folders = DriveApp.getFoldersByName('Test-Folder');

// Iterates through all folders named 'Test-Folder'.
while (folders.hasNext()) {
  const folder = folders.next();

  // Creates a shortcut to the provided Drive item ID and resource key, and returns it.
  DriveApp.createShortcutForTargetIdAndResourceKey(folder.getId(), folder.getResourceKey());
}

매개변수

이름유형설명
targetIdString대상 파일 또는 폴더의 ID입니다.
targetResourceKeyString대상 파일 또는 폴더의 리소스 키입니다.

리턴

File - 새로운 단축키입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive

enforceSingleParent(value)

항목 상위 요소에 영향을 주는 모든 호출에 대해 applySingleParent 동작을 사용 설정하거나 사용 중지합니다.

Google Drive의 폴더 구조 간소화 및 모델 공유 블로그에서 확인하세요.

// Enables enforceSingleParent behavior for all calls affecting item parents.
DriveApp.enforceSingleParent(true);

매개변수

이름유형설명
valueBooleanapplySingleParent 플래그의 새로운 상태입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive

getFileById(id)

지정된 ID가 있는 파일을 가져옵니다. 파일이 존재하지 않거나 사용자는 액세스 권한이 없습니다.

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace 'Test' with your file name.
const files = DriveApp.getFilesByName('Test');

if (files.hasNext()) {
  // Gets the ID of each file in the list.
  const fileId = files.next().getId();

  // Gets the file name using its ID and logs it to the console.
  console.log(DriveApp.getFileById(fileId).getName());
}

매개변수

이름유형설명
idString파일의 ID입니다.

리턴

File - 지정된 ID가 있는 파일입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFileByIdAndResourceKey(id, resourceKey)

지정된 ID와 리소스 키가 있는 파일을 가져옵니다. 리소스 키는 추가 매개변수입니다. 링크를 사용하여 공유된 파일에 액세스하려면 이 매개변수를 전달해야 합니다.

파일이 존재하지 않거나 사용자에게 권한이 없는 경우 스크립트 예외가 발생합니다. 액세스할 수 있습니다.

// Gets a list of all files in Drive with the given name.
// TODO(developer): Replace 'Test' with your file name.
const files = DriveApp.getFilesByName('Test');
if (files.hasNext()) {

  // Gets the first file in the list.
  const file = files.next();

  // Gets the ID and resource key.
  const key = file.getResourceKey();
  const id = file.getId();

  // Logs the file name to the console using its ID and resource key.
  console.log(DriveApp.getFileByIdAndResourceKey(id, key).getName());
}

매개변수

이름유형설명
idString파일의 ID입니다.
resourceKeyString폴더의 리소스 키입니다.

리턴

File - 지정된 ID가 있는 파일입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFiles()

사용자 드라이브에 있는 모든 파일의 컬렉션을 가져옵니다.

리턴

FileIterator - 사용자 드라이브에 있는 모든 파일 모음입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFilesByName(name)

지정된 이름을 가진 사용자 드라이브에 있는 모든 파일 모음을 가져옵니다.

매개변수

이름유형설명
nameString찾을 파일의 이름입니다.

리턴

FileIterator - 특정 이름을 가진 사용자 드라이브에 있는 모든 파일 모음입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFilesByType(mimeType)

지정된 MIME 유형을 가진 사용자 드라이브에 있는 모든 파일의 컬렉션을 가져옵니다.

매개변수

이름유형설명
mimeTypeString찾을 파일의 MIME 유형입니다.

리턴

FileIterator - 지정된 MIME가 있는 사용자 드라이브에 있는 모든 파일 모음입니다. 있습니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolderById(id)

지정된 ID가 있는 폴더를 가져옵니다. 폴더가 존재하지 않거나 사용자에게 액세스 권한이 없습니다.

매개변수

이름유형설명
idString폴더의 ID입니다.

리턴

Folder - 지정된 ID가 있는 폴더입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolderByIdAndResourceKey(id, resourceKey)

지정된 ID와 리소스 키가 있는 폴더를 가져옵니다. 리소스 키는 추가 매개변수입니다. 이 파일은 링크를 사용하여 공유된 폴더에 액세스하는 데 전달되어야 합니다.

폴더가 존재하지 않거나 사용자에게 권한이 없는 경우 스크립팅 예외가 발생합니다. 액세스할 수 있습니다.

매개변수

이름유형설명
idString폴더의 ID입니다.
resourceKeyString폴더의 리소스 키입니다.

리턴

Folder - 지정된 ID가 있는 폴더입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolders()

사용자 드라이브에 있는 모든 폴더의 컬렉션을 가져옵니다.

리턴

FolderIterator - 사용자 드라이브에 있는 모든 폴더의 모음입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFoldersByName(name)

지정된 이름을 가진 사용자 드라이브에 있는 모든 폴더의 컬렉션을 가져옵니다.

매개변수

이름유형설명
nameString찾을 폴더의 이름입니다.

리턴

FolderIterator - 특정 이름을 가진 사용자 Drive의 모든 폴더 모음입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getRootFolder()

사용자 Drive의 루트에 있는 폴더를 가져옵니다.

// Gets the user's My Drive folder and logs its name to the console.
console.log(DriveApp.getRootFolder().getName());

// Logs the Drive owner's name to the console.
console.log(DriveApp.getRootFolder().getOwner().getName());

리턴

Folder - 사용자 Drive의 루트 폴더입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getStorageLimit()

사용자가 드라이브에 저장할 수 있는 바이트 수를 가져옵니다.

// Gets the number of bytes the user can store in Drive and logs it to the console.
console.log(DriveApp.getStorageLimit());

리턴

Integer - 사용자가 Drive에 저장할 수 있는 바이트 수입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getStorageUsed()

사용자가 현재 드라이브에 저장 중인 바이트 수를 가져옵니다.

// Gets the number of bytes the user is currently storing in Drive and logs it to the console.
console.log(DriveApp.getStorageUsed());

리턴

Integer - 사용자가 현재 Drive에 저장 중인 바이트 수입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getTrashedFiles()

사용자 드라이브의 휴지통에 있는 모든 파일의 컬렉션을 가져옵니다.

// Gets a list of all the files in the trash of the user's Drive.
const trashFiles = DriveApp.getTrashedFiles();

// Logs the trash file names to the console.
while (trashFiles.hasNext()) {
  const file = trashFiles.next();
  console.log(file.getName());
}

리턴

FileIterator - 휴지통에 있는 파일 모음입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getTrashedFolders()

사용자 드라이브의 휴지통에 있는 모든 폴더의 컬렉션을 가져옵니다.

// Gets a collection of all the folders in the trash of the user's Drive.
const trashFolders = DriveApp.getTrashedFolders();

// Logs the trash folder names to the console.
while (trashFolders.hasNext()) {
  const folder = trashFolders.next();
  console.log(folder.getName());
}

리턴

FolderIterator - 휴지통에 있는 폴더 모음입니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

searchFiles(params)

지정된 검색어와 일치하는 사용자 Drive의 모든 파일 모음을 가져옵니다. 있습니다. 검색 기준은 Google Drive SDK 문서에 자세히 설명되어 있습니다. 참고로 Drive는 서비스는 Drive API v2를 사용하며 일부 쿼리 필드는 v3와 다릅니다. 필드를 검토합니다. 차이점에 대해 자세히 알아보세요.

params 인수는 문자열 값을 포함할 수 있는 쿼리 문자열이므로 주의가 필요합니다. 를 사용하면 따옴표를 올바르게 이스케이프 처리할 수 있습니다 (예: "title contains 'Gulliver\\'s Travels'" 또는 'title contains "Gulliver\'s Travels"').

// Logs the name of every file in the user's Drive that modified after February 28,
// 2022 whose name contains "untitled.""
var files = DriveApp.searchFiles(
    'modifiedDate > "2022-02-28" and title contains "untitled"');
while (files.hasNext()) {
  var file = files.next();
  console.log(file.getName());
}

매개변수

이름유형설명
paramsString검색 기준(Google Drive SDK 문서에 자세히 설명되어 있음)

리턴

FileIterator - 검색어와 일치하는 사용자 Drive의 모든 파일 모음 있습니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

searchFolders(params)

지정된 검색과 일치하는 사용자 Drive의 모든 폴더 컬렉션을 가져옵니다. 있습니다. 검색 기준은 Google Drive SDK 문서에 자세히 설명되어 있습니다. 참고로 Drive는 서비스는 Drive API v2를 사용하며 일부 쿼리 필드는 v3와 다릅니다. 필드를 검토합니다. 차이점에 대해 자세히 알아보세요.

params 인수는 문자열 값을 포함할 수 있는 쿼리 문자열이므로 주의가 필요합니다. 를 사용하면 따옴표를 올바르게 이스케이프 처리할 수 있습니다 (예: "title contains 'Gulliver\\'s Travels'" 또는 'title contains "Gulliver\'s Travels"').

// Logs the name of every folder in the user's Drive that you own and is starred.
var folders = DriveApp.searchFolders('starred = true and "me" in owners');
while (folders.hasNext()) {
  var folder = folders.next();
  console.log(folder.getName());
}

매개변수

이름유형설명
paramsString검색 기준(Google Drive SDK 문서에 자세히 설명되어 있음)

리턴

FolderIterator - 검색어와 일치하는 사용자 Drive의 모든 폴더 모음 있습니다.

승인

이 메서드를 사용하는 스크립트에는 다음 범위 중 하나 이상으로 승인이 필요합니다.

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

지원 중단된 메서드