Change Google Drive Folder color with Apps Script

Stéphane Giron
2 min readMay 3, 2019

--

In Apps Script you can use the DriveApp service to manage your files and folders, but the methods getColor and setColor do not exist.

You can use the drive advanced service to query the API directly but with recent change you will have to activate the Drive API in GCP and will need a GCP project.

To have a smooth integration and skip the Google Cloud project creation here is a function to manage Folder color in Apps Script. These functions queries the API directly with UrlFetchApp() service.

REST API parameter

To manage color folder you have to use the parameter : folderColorRgb

Be careful, you can’t set custom Hexadecimal code. It must be one of the valid Hexadecimal code available in the About REST call (ref).

! Fast reading, Apps Script code or GitHub link at the end of the page.

Code to Get and Set folder color in Google Drive

We define service

var folderColor = {}

Get list of all colors code and name

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"
};

Get folder color name or hexadecimal code

// Get Hexadecimal code of the color usedfolderColor.getHexa = function(id){
var color = this.getColor(id);
return color.folderColorRgb;
}
//Get Color namefolderColor.getName = function(id){
var hexa = this.getHexa(id);
Logger.log(hexa)
for(var key in colorPalette){
Logger.log(key + ' : ' + colorPalette[key])
if(hexa == colorPalette[key]){
return key
}
}
throw "Error to get the color name please check Hexa value : "+ hexa;
}
// Helper to query API and get color parameterfolderColor.getColor = function(id){
var url = 'https://www.googleapis.com/drive/v2/files/'+id+'?fields=folderColorRgb';
var param = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}
};
var html = UrlFetchApp.fetch(url,param).getContentText();
return JSON.parse(html);
}

Set folder color name or hexadecimal code

// Set folder color by namefolderColor.setColorByName = function(id,name){
if(!colorPalette[name]){
throw "Name is not valid, please check name in colorPalette.";
}
this.setColor(id,colorPalette[name]);
return true;
}
// Set folder color by Hexadecimal codefolderColor.setColorByHexa = function(id,hexa){
for(var key in colorPalette){
if(hexa == colorPalette[key]){
break;
}
throw "Hexadecimal color code is not a valid code.";
}
this.setColor(id,hexa);
return true;
}
// Helper to query API for setting color parameterfolderColor.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;
}

Get the code

You can copy the script file to get the code or copy the content of code.js in your script to manage colors of your folders.

Manage folder color script : Open here >>

View in GitHub : link

--

--