خدمة Drive المتقدّمة

تتيح لك خدمة Drive المتقدمة استخدام Google Drive API في "برمجة تطبيقات Google" يشبه إلى حد كبير Drive المدمج في "برمجة تطبيقات Google" خدمة، تسمح واجهة برمجة التطبيقات هذه للنصوص البرمجية بإنشاء الملفات والمجلدات وتعديلها في Google Drive. في معظم الحالات، تتضمن أسهل من ذي قبل، ولكن هذه الخدمة المتقدمة توفر بعض الإعدادات الإضافية الأخرى، بما في ذلك الوصول إلى خصائص الملفات المخصصة وكذلك مراجعات الملفات والمجلدات.

مَراجع

للحصول على معلومات تفصيلية حول هذه الخدمة، يُرجى الاطّلاع على المرجع Google Drive API. مثل الكل والخدمات المتقدمة في برمجة التطبيقات، الإصدار تستخدم خدمة Drive العناصر والطرق والمعلمات نفسها المستخدمة في واجهة برمجة التطبيقات العامة. لمزيد من المعلومات، راجِع آلية عمل توقيعات الطرق. العزم.

للإبلاغ عن المشاكل والعثور على خيارات دعم أخرى، يُرجى الاطِّلاع على دعم Drive API الدليل.

نموذج التعليمات البرمجية

تستخدم عيّنات التعليمات البرمجية في هذا القسم الإصدار 3 لواجهة برمجة التطبيقات.

تحميل ملفات

يوضح نموذج التعليمات البرمجية التالي كيفية حفظ ملف في ملف Google Drive.

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

سرد المجلدات

يوضح نموذج التعليمات البرمجية التالي كيفية سرد مجلدات المستوى الأعلى في مجلد Google Drive. لاحظ استخدام الرموز المميزة للصفحة للوصول إلى القائمة الكاملة نتائجك.

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