Drive की बेहतर सेवा

बेहतर Drive सेवा की मदद से, Apps Script में Google Drive API का इस्तेमाल किया जा सकता है. Apps Script की बिल्ट इन Drive सेवा की तरह, यह एपीआई भी स्क्रिप्ट को Google Drive में फ़ाइलें और फ़ोल्डर बनाने, ढूंढने, और उनमें बदलाव करने की अनुमति देता है. ज़्यादातर मामलों में, पहले से मौजूद सेवा का इस्तेमाल करना आसान होता है, लेकिन यह बेहतर सेवा कुछ और सुविधाएं भी देती है. इनमें कस्टम फ़ाइल प्रॉपर्टी के ऐक्सेस के साथ-साथ फ़ाइलों और फ़ोल्डर में बदलाव करना शामिल है.

रेफ़रंस

इस सेवा के बारे में ज़्यादा जानकारी के लिए, Google Drive API से जुड़ा रेफ़रंस दस्तावेज़ देखें. Apps Script की सभी बेहतर सेवाओं की तरह, Drive में भी उन ही ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल किया जाता है जो सार्वजनिक एपीआई में हैं. ज़्यादा जानकारी के लिए, मेथड सिग्नेचर तय करने का तरीका देखें.

समस्याओं की रिपोर्ट करने और अन्य सहायता ढूंढने के लिए, Drive API की सहायता गाइड देखें.

नमूना कोड

इस सेक्शन में मौजूद कोड सैंपल, एपीआई के वर्शन 3 का इस्तेमाल करते हैं.

फ़ाइलें अपलोड करें

नीचे दिया गया कोड सैंपल किसी फ़ाइल को उपयोगकर्ता की 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);
  }
}

फ़ोल्डर की सूची बनाएं

नीचे दिए गए कोड सैंपल में, उपयोगकर्ता की 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);
  }
}