[HOWTO] ๐Ÿ“ 19 Functions For Managing A Google Drive Folder!

:computer: Introduction

This tutorial will show you some useful functions for managing Google Drive folders with 0 extensions, together with the help of App Inventor.

or (particularly the Web component) and Google Apps Script. You will be able to:

  • Add an editor/viewer to your folder.

  • Check what access (comment, edit, view, etc.) a user has to this folder/

  • Check if the folder is starred or is in the trash of your Drive.

  • Create a new folder inside the folder.

  • Dump this folder into the trash.

  • Edit and get the description of this folder.

  • Get the creation date and the last updated date of this folder.

  • Get the owner's email and name of this folder.

  • Move this folder to another destination folder.

  • Remove an editor or a viewer.

  • Renaming the folder, or picking it up back from the Trash.

  • Star or Unstar this folder.

It's simple, I will provide you a script and blocks and you will copy the script into your Google Apps Script project, but with a twist. We will also edit the manifest of this App Script project. You probably haven't experienced this, but fear not, I will explain and it will be easy as pie.

*You will need a Google account that can at least view the target folder.

:memo: Step 1. Setting Up Apps Script.

As usual, create a new Apps Script project. If you haven't, then open Drive, click on the "New" button, hover over "More" and click on Google Apps Script. That creates us a new Apps Script project.

Give it a name by clicking on the title if necessary so we can find it later. Next, copy this script and replace the code already in your project with this new script.

function doPost(e) {
  var action = e.parameter.action;
  var id = e.parameter.fileId;
  var file = DriveApp.getFolderById(id);
  if (action == "renameFolder"){
    var newFolderName = e.parameter.newName;
    file.setName(newFolderName);
    return ContentService.createTextOutput("File Name Successfully Changed.");
  } else if (action == "checkAccess") {
    var address = e.parameter.email;
    var permission = file.getAccess(address);
    if (permission == DriveApp.Permission.VIEW) {
      return ContentService.createTextOutput("You can only view this folder.");
    } else if (permission == DriveApp.Permission.COMMENT) {
      return ContentService.createTextOutput("You can only comment or view this folder.");
    } else if (permission == DriveApp.Permission.EDIT) {
      return ContentService.createTextOutput("You can view, edit or comment this folder.");
    } else if (permission == DriveApp.Permission.OWNER) {
      return ContentService.createTextOutput("You are the owner of this folder.");
    } else if (permission == DriveApp.Permission.ORGANIZER) {
      return ContentService.createTextOutput("You are the organizer of this folder, and can organize files inside this folder.");
    } else if (permission == DriveApp.Permission.FILE_ORGANIZER) {
      return ContentService.createTextOutput("You are the file organizer of this folder, and can edit, trash, and move content within a shared drive.");
    } else {
      return ContentService.createTextOutput("You do not have any permissions.");
    }
  } else if (action == "getOwner") {
    var owner = file.getOwner();
    var ownerEmail = owner.getEmail();
    var ownerName = owner.getName();
    var output = ownerEmail + "," + ownerName;
    return ContentService.createTextOutput(output);
  } else if (action == "isStarred") {
    var star = file.isStarred().toString();
    return ContentService.createTextOutput(star);
  } else if (action == "isTrashed") {
    var trash = file.isTrashed().toString();
    return ContentService.createTextOutput(trash);
  } else if (action == "newFolder") {
    var name = e.parameter.folderName;
    file.createFolder(name);
    return ContentService.createTextOutput("Folder created.");
  } else if (action == "addEditor") {
    var address = e.parameter.email;
    file.addEditor(address);
    return ContentService.createTextOutput("Editor added.");
  } else if (action == "addViewer") {
    var address = e.parameter.email;
    file.addViewer(address);
    return ContentService.createTextOutput("Viewer added.");
  } else if (action == "getDateCreated") {
    var date = file.getDateCreated();
    var milliseconds = date.getTime().toString();
    return ContentService.createTextOutput(milliseconds);
  } else if (action == "getLastUpdated") {
    var date = file.getLastUpdated();
    var milliseconds = date.getTime().toString();
    return ContentService.createTextOutput(milliseconds);
  } else if (action == "move") {
    var destinationId = e.parameter.destinationFolderId;
    var destinationFolder = DriveApp.getFolderById(destinationId);
    file.moveTo(destinationFolder);
    return ContentService.createTextOutput("Moved to destination.");
  } else if (action == "getDescription") {
    var description = file.getDescription();
    return ContentService.createTextOutput(description);
  } else if (action == "editDescription") {
    var description = e.parameter.newDescription;
    file.setDescription(description);
    return ContentService.createTextOutput("Edited description");
  } else if (action == "moveToTrash"){
    file.setTrashed(true);
    return ContentService.createTextOutput("Moved to your trash.");
  } else if (action == "restoreFromTrash") {
    file.setTrashed(false);
    return ContentService.createTextOutput("Restored from your trash.");
  } else if (action == "star"){
    file.setStarred(true);
    return ContentService.createTextOutput("Starred this folder.");
  } else if (action == "unstar") {
    file.setStarred(false);
    return ContentService.createTextOutput("Unstarred this folder.");
  } else if (action == "removeEditor") {
    var address = e.parameter.email;
    file.removeEditor(address);
    return ContentService.createTextOutput("Removed the editor with the address " + address + ".");
  } else if (action == "removeViewer") {
    var address = e.parameter.email;
    file.removeViewer(address);
    return ContentService.createTextOutput("Removed the viewer with the address " + address + ".");
  }
}

Done. Now, click on this little disk image to save your script.

Good. Now, click on the wheel (settings) icon on the left navigation bar.

image

You will be redirected to the settings of this project. Enable "Show 'appsscript.json' manifest file in editor".

image

Go back to the editor by clicking on the code icon on the left panel.

image

On the left control menu back in the script editor, you will see "appsscript.json" showing on top of "Code.gs". Code.gs is the Google Apps Script script that we code with normally, while appsscript.json is the manifest file of this apps script.

Android apps have an AndroidManifest.xml to define elements of this Android app, as you already know. This JSON file is the equivalent. Click on it.

image

You will see code in the manifest editor. Yours might be different, but mine is this.

{
  "timeZone": "Asia/Hong_Kong",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

According to the official Google documentation,

Scripts that use this method require authorization with one or more of the following scopes:

  • https://www.googleapis.com/auth/drive

  • https://www.googleapis.com/auth/drive.readonly

If you are an editor or the owner of the folder, use the first link. The first link includes read and write permission. Else, use the second link. It only includes read permission.

We will have to add an element in this manifest file that will tell Google what OAuth Scopes (or if you do not understand, permissions) we will use. So, we will add another element in the manifest.

"oauthScopes": [
    "https://www.googleapis.com/auth/drive"
  ]

Replace this link if you do not need to edit the folder.

For me, the whole manifest would become:

{
  "timeZone": "Asia/Hong_Kong",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/drive"
  ]
}

Click on that image Save button again in the manifest editor. Now, the web app is ready to be deployed! To deploy the script, click on image, select "New deployment", click on the image icon in the popup, and select "Web app".

Make sure you are executing as you and anyone has access to this script. Then click Deploy.

image

If it asks you to authorize access, click here.

The web app might ask you to authorize access. If so, click on image, then a new popup would show up for you to log in to.

Click on your account in the popup.

image

It will show that "Google hasn't verified this app". Don't worry, this script is totally safe. Click on Advanced

image

and

image

and

and the popup would close. Back in Apps Script, after it finished

image

, it would show the deployment ID and the deployment URL. We only need the URL, so click on image for the URL and head back to App Inventor.

If it doesn't, click here.

In Apps Script, after it finished

image

, it would show the deployment ID and the deployment URL. We only need the URL, so click on image for the URL and head back to App Inventor.

:memo: Step 2. Programming Blocks In App Inventor.

Modify these blocks and check which parts you need and which parts you don't. This is only a demo app.

(click image to expand)

There are two things you definitely need to change:

  • the variable appsScriptUrl: replace it with the apps script URL that you have just deployed.

  • the variable folderId: replace it with the folder ID of your Drive folder. To get it, open the folder in Drive, and copy the text after the URL https://drive.google.com/drive/folders/. For example, if the URL to your folder is https://drive.google.com/drive/folders/1A7zfTHkNvm46wTuVElVWqU_IrLH0Fl5 (link modified), the folder ID would be 1A7zfTHkNvm46wTuVElVWqU_IrLH0Fl5.

:inbox_tray: Downloads

AIA: GoogleDrive.aia (13.3 KB)

:notebook_with_decorative_cover: Tests

Tested on Android 11 on Xiaomi 11 Lite.

:blue_book: Some Notes

  • Editors can only change permissions (add/remove editors or viewers) if the owner has enabled Editors can change permissions and share in the "Share folder" dialog.

  • Only editors or the owner can edit and write to the folder.

  • Only suggestors, editors, viewers or the owner can read the folder.

  • Removing viewers or editors doesn't block users from accessing the Folder if they belong to a class of users who have general accessโ€”for example, if the Folder is shared with the user's entire domain, or if the Folder is in a shared drive that the user can access.

  • If you use the RemoveViewer function to an editor, it will have zero effect.

:cake: Done.

If you liked this, please leave a like in this tutorial, I would genuinely appreciate it because I spent a lot of time and effort in this, thanks and done.

10 Likes

You had better explain the folder/file permissions requirements (for those folders and files that are not owned by the script creator google account)

1 Like

(added to new FAQ)

1 Like

Updated the tutorial. Is it OK now?

I guess so...assumes potential users have a good grasp of how google drive works :wink:

1 Like

Hey Gordon,
As I can see this works for folders ,So whether this method works for files as well.
Thanks for reading.

It worked perfectly fine with fileId Also.
So, Thanks

1 Like