Better way to tag google drive folders

220 views
Skip to first unread message

Nathalie Hechinger

unread,
Jan 12, 2022, 7:41:05 PM1/12/22
to Google Apps Script Community
Hi,

I'd like a better way to tag folders. I hear labels are available but we do not have access to it. I found this script online where you can add the file name to the folder description.  I want to know if it is possible to set the folder description using the value found in the first column of my spreadsheet. 

function setDescriptionToFolderNames() { var file; var filename; var folders; var filedescription; var contents = DocsList.getAllFiles(); // sort ascending. Oldest first, in case of timeout: contents.sort(function(a,b) {return a.getLastUpdated()-b.getLastUpdated()}); // synchronize folder names of all files (only updates if folders have changed): for (var i = 0; i < contents.length; i++) { file = contents[i]; try { filename = file.getName(); //Logger.log("Checking: " +filename +" ("+file.getLastUpdated()+")"); folders = file.getParents(); // sort by folder name: folders.sort(function(a, b) { return a.getName().localeCompare(b.getName()); } ); filedescription = ""; for (var f = 0; f < folders.length; f++) { filedescription = filedescription+folders[f].getName()+" "; } if (filedescription != contents[i].getDescription()) { file.setDescription(filedescription); Logger.log("Updated: " +filename); } } catch(e){ Logger.log("Error: " +filename+" "+e); } } };

Laurie Nason

unread,
Jan 13, 2022, 12:11:57 AM1/13/22
to google-apps-sc...@googlegroups.com
Hi Natalie,
If you are wanting a to set a simple description/tag for a folder have a look at the Folder Class Info and there is a .setDescription() function for a folder that you can call for setting and .getDescription() for retrieving them.

In your google sheet, how are you defining which row corresponds to which folder? If you have a column with the URL of the folder, then you can iterate through that in your script, set the folder into a folder object and then set the description to the value in the corresponding tag column you want. 

This guy mentions that at the time of writing there is no properties service for folders, but does suggest a simple method for keeping multiple tags using the description property of a folder, so if you want to have multiple - that's probably the way to go.

Not sure if this helps you or not - feel free to come back to this group if not!
Laurie

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/18bf14dd-91e6-480a-b55b-05de918cd028n%40googlegroups.com.

Clark Lind

unread,
Jan 13, 2022, 9:34:09 AM1/13/22
to Google Apps Script Community
As Laurie suggests, that is also the way I do it. For example, in a business setting, I might have different "meta tags" for different business areas like "#Personnel", "#Projects", "#Finance".
Then I can take the sheet's first column value, (in this example, I'll say it is "Personnel"),  and add it to the folder's description. I typically add a # in front of the meta tags, or something else somewhat unique, then when searching G-Drive, I just search for #Personnel. You can use more than one also..  e.g., #Projects, #Finance if I was looking for financial info regarding projects (assuming I used my meta tags correctly!!) :)

You can use this to tag files also.

Nathalie Hechinger

unread,
Jan 14, 2022, 7:40:21 PM1/14/22
to Google Apps Script Community
Hi Laurie,

I was able to get the folder's description but unable to set the description found in the fourth column in the "data" sheet. I was stumped a little bit because other scripts were only giving me only the child folder's description. I pasted this info into a sheet called "data". Forgive me as this is my first time writing code. Do you know why my script isn't working? If not no worries you both have been more than helpful!

let sp = SpreadsheetApp.getActiveSpreadsheet();
let ss = sp.getSheetByName("data");
let folderName = "renameSumFilesSheet"


function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('renameDescriptionFunction')
.addItem('renameDescriptionFunction', 'renameDescriptionFunction')
.addToUi();
}

function renameDescriptionFunction() {
var folders = DriveApp.getFoldersByName(folderName);
var folder = folders.next();
var lastRow = ss.getLastRow();
var olddescription = [];
var newdescription = [];
var od = 4;
var nd = 3;
var url =2;
olddescription = ss.getRange(2, od, lastRow-1).getValues();
newdescription = ss.getRange(2, nd, lastRow-1).getValues();
for(let i = 0; i < olddescription.length ; i++){
var it = folders.getDescription(olddescription[i]);
if (url = folder.getUrl());

while(it.hasNext()){
var newfolder = it.next();
newfolder.setValue(Description)(newdescription[i]);
}
}

}



Laurie Nason

unread,
Jan 16, 2022, 12:16:29 AM1/16/22
to google-apps-sc...@googlegroups.com
Hi,
It looks like your code on this line needs to change
FROM:
newfolder.setValue(Description)(newdescription[i]);
TO
newfolder.setDescription(newdescription[i]);
Laurie

Reply all
Reply to author
Forward
0 new messages