Skip to content

Instantly share code, notes, and snippets.

@JayGoldberg
Created December 23, 2022 14:47
Show Gist options
  • Save JayGoldberg/57656901273dedb1e71867a906202253 to your computer and use it in GitHub Desktop.
Save JayGoldberg/57656901273dedb1e71867a906202253 to your computer and use it in GitHub Desktop.
Writes all the folder names of a file into the file description, so that the file can be found by searching the folder names.
// https://www.labnol.org/code/19721-add-tags-google-drive-files
/*
setDescriptionToFolderNames
Workaround to fake Tags in Google Drive.
Writes all the folder names of a file into the file description, so that the file can be found by searching the folder names.
*/
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment