Skip to content

Instantly share code, notes, and snippets.

@tomnatt
Last active January 12, 2019 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomnatt/5d8b730ab3b86dcc3dc12a7ab7ac914f to your computer and use it in GitHub Desktop.
Save tomnatt/5d8b730ab3b86dcc3dc12a7ab7ac914f to your computer and use it in GitHub Desktop.
Document tagging with Google Scripts (fragments are duplicated for embed in post)
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function doGet() {
return HtmlService
.createTemplateFromFile('index')
.evaluate();
}
function getFiles() {
var files = []
var folder = DriveApp.getFoldersByName("Elsewhere").next();
// get files from primary folder
files = files.concat(getFilesFromFolder(folder));
// get files from subfolders (only one level deep)
var subfolders = folder.getFolders();
while (subfolders.hasNext()) {
files = files.concat(getFilesFromFolder(subfolders.next()));
}
return files;
}
function getFilesFromFolder(folder) {
var files = [];
var f = folder.getFiles();
while (f.hasNext()) {
files.push(f.next());
}
return files;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getTags() {
var tags = [];
var files = getFiles();
// get all the tags from the files to compose a list
files.forEach(function(file) {
// tags are in the description in the form "#story #world"
var description = file.getDescription();
// if there is no description, move on
if (description != null) {
// split out the tags, remove the empty ones and trim
var t = description.split("#");
t = t.filter(function(tag) { return tag; });
t = t.map(function(tag) { return tag.trim() });
// add to the tags array if not already present
for (var i = 0; i < t.length; i++) {
tags.indexOf(t[i]) === -1 ? tags.push(t[i]) : "";
}
}
});
tags = tags.sort();
return tags;
}
function getDocsForTag(tag) {
tag = "#" + tag;
var docs = [];
var files = getFiles();
files.forEach(function(file) {
// push the file onto docs if the description exists and contains #tag
if (file.getDescription() != null) {
if (file.getDescription().indexOf(tag) !== -1) {
docs.push(file);
}
}
});
// sort the docs by name
docs.sort(function(a, b){
if(a.getName() < b.getName()) { return -1; }
if(a.getName() > b.getName()) { return 1; }
return 0;
});
return docs;
}
function doGet() {
return HtmlService
.createTemplateFromFile('index')
.evaluate();
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" media="screen" type="text/css" href="https://www.tomnatt.com/css/tomnatt.css" title="Default">
<style>
h1 {
font-size: 36px;
-webkit-transform: none;
transform: none;
}
</style>
</head>
<body>
<div class="content">
<h1>Elsewhere docs</h1>
<? var tags = getTags();
for (var i = 0; i < tags.length; i++) {
var docs = getDocsForTag(tags[i]); ?>
<h2><?= capitalizeFirstLetter(tags[i]) ?></h2>
<ul>
<? for (var j = 0; j < docs.length; j++) { ?>
<li><a target="_blank" href="<?= docs[j].getUrl(); ?>"><?= docs[j].getName(); ?></a></li>
<? } ?>
</ul>
<? } ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment