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

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

रेफ़रंस

इस सेवा के बारे में विस्तृत जानकारी के लिए, यह संदर्भ देखें दस्तावेज़ बना लिया है. सभी पसंद करें Apps Script में, बेहतर सेवाएं Drive सेवा उन्हीं ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल करती है जो सार्वजनिक एपीआई. ज़्यादा जानकारी के लिए, देखें कि पेमेंट के तरीके के हस्ताक्षर कैसे होते हैं तय किया गया है.

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

नमूना कोड

इस सेक्शन के कोड सैंपल वर्शन 3 का इस्तेमाल करते हैं को ट्रैक किया जा सकता है.

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

नीचे दिया गया कोड सैंपल, किसी उपयोगकर्ता की ड्राइविंग.

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