고급 드라이브 서비스

고급 드라이브 서비스를 사용하면 Apps Script의 Google Drive API 매우 유사함 Apps Script에 기본 제공되는 드라이브 서비스를 사용한다면 이 API를 사용하면 Google Drive의 파일과 폴더를 수정하고 수정할 수도 있습니다 대부분의 경우 내장된 이 고급 서비스는 사용하기 더 쉽지만, 몇 가지 추가 기능을 기능에 대한 액세스를 비롯한 다양한 기능을 제공합니다. 확인할 수 있습니다

참조

이 서비스에 대한 자세한 내용은 참조 문서 문서를 참조하세요. 모두 좋아요 표시 고급 서비스를 제공한다는 점에서 Drive 서비스는 확인할 수 있습니다 자세한 내용은 메서드 서명의 원리 결정됨

문제를 신고하고 기타 지원을 찾으려면 Drive API 지원 가이드를 참조하세요.

샘플 코드

이 섹션의 코드 샘플은 버전 3을 사용합니다. API에 대해 알아봤습니다

파일 업로드

다음 코드 샘플은 파일을 사용자의 드라이브.

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