How To Automate Shared Drive Tasks In Google Drive With Apps Script

How To Automate Shared Drive Tasks In Google Drive With Apps Script

Using the Drive API and Google Apps Script, you can effectively manage and browse the content of shared drives in Google Drive. To begin, select the Add symbol in the Services area to add the Drive API search to your Google project. The most recent version of the Drive API is v3, but Google Apps Script only supports the older version (v2).

You can use a number of capabilities that provide you access to shared drive material once the Drive API service is activated.

How To Automate Shared Drive Tasks In Google Drive With Apps Script

Create a Shared Drive:
function createSharedDrive() {
  const driveName = 'Digital Inspiration';
  const sharedDrive = Drive.Drives.insert({ name: driveName }, Utilities.getUuid());
  console.log('Shared Drive created', sharedDrive.id);
}
Share a Shared Drive with a User:
function shareSharedDriveWithUser({ driveId, role, email }) {
  // The role can be writer, reader, organizer, or commenter
  const response = Drive.Permissions.insert(
    {
      role: role,
      type: 'user',
      value: email,
    },
    driveId,
    {
      supportsAllDrives: true,
      sendNotificationEmails: true,
      fields: 'emailAddress,role',
    }
  );
  console.log('Shared Drive shared with %s', response.emailAddress);
}
List all Shared Drives:

To list all Shared Drives that are accessible to the current user, you can use the listSharedDrives function.

function listSharedDrives() {
  let pageToken = null;
  const response = [];

  do {
    const { items = [], nextPageToken = null } = Drive.Drives.list({
      pageToken,
      maxResults: 50,
      orderBy: 'name',
      fields: 'nextPageToken, items(id, name)',
    });
    items.forEach((item) => response.push(item));
    pageToken = nextPageToken;
  } while (pageToken !== null);

  console.log(response);
}
List Files in a Shared Drive:

If you need to list files within a specific Shared Drive, use the listFilesInSharedDrive function.

function listFilesInSharedDrive(teamDriveId) {
  let pageToken = null;
  const response = [];

  do {
    const { items = [], nextPageToken = null } = Drive.Files.list({
      pageToken,
      maxResults: 50,
      supportsAllDrives: true,
      includeItemsFromAllDrives: true,
      q: `'${teamDriveId}' in parents and trashed = false and mimeType != 'application/vnd.google-apps.folder'`,
      fields: 'nextPageToken, items(id,title,mimeType)',
    });
    items.forEach((item) => response.push(item));
    pageToken = nextPageToken;
  } while (pageToken !== null);

  console.log(response);
}
Move Files in Shared Drives:

Moving files in Shared Drives can be achieved with the moveFilesBetweenSharedDrives function.

function moveFilesBetweenSharedDrives({ parentFolderId, destinationFolderId, fileId }) {
  const data = Drive.Files.update({}, fileId, null, {
    addParents: destinationFolderId,
    removeParents: parentFolderId,
    supportsAllDrives: true,
    fields: 'title,embedLink',
  });
  console.log('File Moved', data.title, data.embedLink);
}
Copy Files in Shared Drives:

You can determine whether a user can move an item outside of the drive by using the getCanMoveItemOutOfDrive() method.

To copy files within Shared Drives, utilize the copyFilesInSharedDrives function.

function copyFilesInSharedDrives({ title, destinationFolderId, fileId }) {
  const data = Drive.Files.copy({ parents: [{ id: destinationFolderId }], title }, fileId, {
    supportsAllDrives: true,
    fields: 'title,embedLink',
  });
  console.log('File Copied', data.title, data.embedLink);
}

You may effectively manage and search the content of shared disks in Google Drive by using these Google Apps Script code samples. With the help of these features, you can easily organize, share, and move files between your shared drives, facilitating collaboration and facilitating quick access to the information you require.

So go ahead and leverage Google Apps Script’s capabilities to regain control over your shared drives and improve workflow!

Leave a Comment