Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find all empty folders in a Shared Drive and change color with Apps Script?

I’m new to Apps Script and I’m trying to make a script that finds all the empty folders and change their color. I managed to do it, when I try it in my Drive it works perfectly, but when I try run it in the Shared Drive it gives me this error:

Exception: Request failed for https://www.googleapis.com returned code 404. Truncated server response: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "File not found: 166gK63I72FZvqY0XjSE63z4SyQuSgGp... (use muteHttpExceptions option to examine full response)

I searched around for some solutions, the only one being to create a project in Google Cloud Platform. I did so, and linked my script to the project in GCP but it gives the same error.
Basically, I want my script to have access to my Shared Drive. The Shared Drive it’s pretty big, it has a lot of folders and files, I don’t know if maybe the error is due to too many api calls, or if it’s a problem with the permission.
I hope someone can help! Thanks a lot! I leave you here my script:

function findEmptyFolders() {
  var parentFolder = DriveApp.getFolderById('Shared Drive ID');
  var folders = parentFolder.getFolders();
  while (folders.hasNext()) {
    var childfolder = folders.next();
    recurseFolder(parentFolder, childfolder);
  }
}

function recurseFolder(parentFolder, folder) {
        var filecount = 0;
        var foldercount = 0;

        var files = folder.getFiles();               
        var childfolders = folder.getFolders();

        while (childfolders.hasNext()) {
            var childfolder = childfolders.next();
            recurseFolder(folder, childfolder);
            foldercount++;
        }

        while (files.hasNext()) { 
            var file = files.next();
            filecount++;
        }

       if (filecount == 0 && foldercount == 0) {            
            var id = folder.getId();
            Logger.log(folderColor.setColorByName(id,'Yellow cab'));
       }             
}

var folderColor = {};

folderColor.setColorByName = function(id,name){
  if(!colorPalette[name]){
    throw "Name is not valid, please check name in colorPalette.";
  }
  this.setColor(id,colorPalette[name]);
  return true;
}

folderColor.init = function(){
  return this;
}

folderColor.setColor = function(id,hexa){
  var url = 'https://www.googleapis.com/drive/v2/files/'+id+'?fields=folderColorRgb';
  var param = {
    method      : "patch",
    contentType: 'application/json',
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    payload: JSON.stringify({folderColorRgb:hexa})
  };
  var html = UrlFetchApp.fetch(url,param).getContentText();
  
  return html;
}

var colorPalette = {
  "Chocolate ice cream":"#ac725e",
  "Old brick red":"#d06b64",
  "Cardinal":"#f83a22",
  "Wild straberries":"#fa573c",
  "Mars orange":"#ff7537",
  "Yellow cab":"#ffad46",
  "Spearmint":"#42d692",
  "Vern fern":"#16a765",
  "Asparagus":"#7bd148",
  "Slime green":"#b3dc6c",
  "Desert sand":"#fbe983",
  "Macaroni":"#fad165",
  "Sea foam":"#92e1c0",
  "Pool":"#9fe1e7",
  "Denim":"#9fc6e7",
  "Rainy sky":"#4986e7",
  "Blue velvet":"#9a9cff",
  "Purple dino":"#b99aff",
  "Mouse":"#8f8f8f",
  "Mountain grey":"#cabdbf",
  "Earthworm":"#cca6ac",
  "Bubble gum":"#f691b2",
  "Purple rain":"#cd74e6",
  "Toy eggplant":"#a47ae2"
 };

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your script, how about the following modification?

From:

var url = 'https://www.googleapis.com/drive/v2/files/'+id+'?fields=folderColorRgb';

To:

var url = 'https://www.googleapis.com/drive/v2/files/'+id+'?fields=folderColorRgb&supportsAllDrives=true';

Reference:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading