Home > Software engineering >  Google Apps Script from My Drive to Shared Drives
Google Apps Script from My Drive to Shared Drives

Time:04-14

I have designed a feedback system via Google Sheets and Apps Script. It works all good on My Drive. When I moved it to a Shared Drive, the "DriveApp" didn't like the change.

Based on this very useful article that John from Google shard with me, I activated Advanced Drive Service and used the method Drive.Drives.insert allows you to specify the Drive Id.

How can I specify a folder in a shared drive with this method? Like the one we have with DriveApp (DriveApp.getFolderByID). How would you do that?

CodePudding user response:

From How can I specify a folder in a shared drive with this method? Like the one we have with DriveApp (DriveApp.getFolderByID). How would you do that?, I guessed that you might want to retrieve the folder information. If my understanding is correct, how about the following sample script?

Sample script 1:

When you can know the folder ID of the folder in a shared Drive, you can use the following script. Before you use this, please enable Drive API at Advanced Google services.

const folderId = "###"; // Please set the folder ID in a shared Drive.

// This sample uses Drive service (DriveApp).
const folder = DriveApp.getFolderById(folderId);
const folderName1 = folder.getName();
console.log(folderName1)

// This sample uses Drive API.
const obj = Drive.Files.get(folderId, {supportsTeamDrives: true});
const folderName2 = obj.title;
console.log(folderName2)
  • In this sample script, the folder name of the folder is in a shared Drive using Google Apps Script. This script includes 2 patterns. Please confirm it.

  • In the case of DriveApp, when the file ID and folder ID are used, the files and folders in a shared Drive can be retrieved.

Sample script 2:

When you cannot know the folder ID of the folder in a shared Drive, you can use the following script.

const driveId = "###"; // Please set your Drive ID.

// This sample uses Drive service (DriveApp).
const folders = DriveApp.getFolderById(driveId).getFolders();
while (folders.hasNext()) {
  const folder = folders.next();
  const folderName1 = folder.getName();
  console.log(folderName1)
}

// This sample uses Drive API.
const res = Drive.Files.list({ corpora: "drive", includeItemsFromAllDrives: true, supportsAllDrives: true, driveId, q: `mimeType='${MimeType.FOLDER}' and trashed=false` });
res.items.forEach(({title}) => console.log(title));
  • In this script, in the case of DriveApp, the folder list is retrieved from just under the top folder of the shared Drive. Please be careful about this.
  • In the case of Drive API, the folder list of all folders in the shared Drive is retrieved. But, this is a sample script. So, in this case, 100 folders can be retrieved. When you want to retrieve more, please set maxResults and pageToken. Please be careful about this.

Note:

  • These sample scripts are simple scripts. So, please modify them for your actual situation.

References:

  • Related