Skip to content

Commit

Permalink
Update drive.gs Advanced Drive Service samples (#433)
Browse files Browse the repository at this point in the history
* Update drive.gs Advanced Drive Service samples

* Update the samples to work with Drive API v3.

* Set page token for list revisions

* Update drive.gs

Fixed commit issues

* Update drive.gs

---------

Co-authored-by: Steve Bazyl <[email protected]>
  • Loading branch information
kencenerelli and sqrrrl authored Dec 11, 2023
1 parent 17fe1a8 commit 82c5c04
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions advanced/drive.gs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function uploadFile() {
// Makes a request to fetch a URL.
const image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
let file = {
title: 'google_logo.png',
name: 'google_logo.png',
mimeType: 'image/png'
};
// Insert new files to user's Drive
file = Drive.Files.insert(file, image);
console.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
// 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);
Expand All @@ -49,16 +49,16 @@ function listRootFolders() {
try {
folders = Drive.Files.list({
q: query,
maxResults: 100,
pageSize: 100,
pageToken: pageToken
});
if (!folders.items || folders.items.length === 0) {
console.log('No folders found.');
if (!folders.files || folders.files.length === 0) {
console.log('All folders found.');
return;
}
for (let i = 0; i < folders.items.length; i++) {
const folder = folders.items[i];
console.log('%s (ID: %s)', folder.title, folder.id);
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) {
Expand All @@ -71,20 +71,24 @@ function listRootFolders() {

// [START drive_add_custom_property]
/**
* Adds a custom property to a file. Unlike Apps Script's DocumentProperties,
* 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 (if the visibility is set to PUBLIC).
* @param {string} fileId The ID of the file to add the property to.
* 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 addCustomProperty(fileId) {
function addAppProperty(fileId) {
try {
const property = {
key: 'department',
value: 'Sales',
visibility: 'PUBLIC'
let file = {
'appProperties': {
'department': 'Sales'
}
};
// Adds a property to a file
Drive.Properties.insert(property, fileId);
// 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);
Expand All @@ -94,29 +98,33 @@ function addCustomProperty(fileId) {

// [START drive_list_revisions]
/**
* Lists the revisions of a given file. Note that some properties of revisions
* are only available for certain file types. For example, Google Workspace
* application files do not consume space in Google Drive and thus list a file
* size of 0.
* Lists the revisions of a given file.
* @param {string} fileId The ID of the file to list revisions for.
*/
function listRevisions(fileId) {
try {
const revisions = Drive.Revisions.list(fileId);
if (!revisions.items || revisions.items.length === 0) {
console.log('No revisions found.');
return;
}
for (let i = 0; i < revisions.items.length; i++) {
const revision = revisions.items[i];
const date = new Date(revision.modifiedDate);
console.log('Date: %s, File size (bytes): %s', date.toLocaleString(),
revision.fileSize);
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);
}
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed with error %s', err.message);
}
} while (pageToken);
}

// [END drive_list_revisions]

0 comments on commit 82c5c04

Please sign in to comment.