Saturday, 19 January 2019

Tagging documents in Google Drive

I use the Google suite for various types of writing, including D&D. Google Drive will let me organise files by folder, which is very early 1990s. What I'd like is some kind of tagging system, so I can find the docs I have scattered all over the place. While it seems like something Google should probably support, while we are waiting for a miracle I can roll my own system using Google Scripts which is apparently mostly ES3, with some enhancement from ES5. So that's ... late 1990s. I guess that's an improvement?

Anyway, language hell is ahead of me and I'm sure at some point I'll convert it to use a proper API in a proper language. Right now, I need a simple way to do tags. Since I don't use the description metadata field (or indeed really acknowledge its existence until I started this) I think I'll use that. Adding something like #npc #story to the field is the level of work I'm willing to do - what I want is something that will pull these tags and list my files using them.

First things first - how on earth do I start writing Google Scripts? Obviously as a lovingly maintained part of the Google suite it'll be tightly integrated so all I have to do is ... add what it claims is a plugin to Drive and I can start creating script files which open in an in-browser editor (I hate in-browser editors, but here we are). Then I need an index file and something to call it. It seems that when loading, the doGet method in the file is called and that needs to hit the HTML file. This all needs accessing via a URL to the dev or published version. Ok:

Carefully not thinking about what will happen if a user isn't using Javascript, let us push forwards. Templating seems pretty simple so an HTML file can be put together. I can also drag in a CSS file from somewhere else so I can reuse my awesome styling.

All I'm looking to do here is create some lists, so I just need to get the filenames and links and put them on the screen. Specifically, I'm getting a list of tags then creating a list of files per tag.

Google Scripts provides a load of objects to poke and since it's taking care of the authentication (the main reason I'm doing it this way rather than using the api) this should be pretty straightforward. I mean the only way this could go wrong is if the scripting is using Javascript from before the dawn of time and I have to do absolutely everything myself?

To be fair it's not too bad once I've got my head out of Ruby and back into Javascript and then removed all the ES6 goodness I've come to expect in the years since it became commonplace. I can actually craft exactly what I'm looking for:

Under the hood ... eugh. It's pretty ugly - and not just because I've used two different forms of a forEach loop in a single short script. To generate this page, I'm looking at each file to get a list of unique tags then reversing the process - looking at each file to figure out if it has the tag I'm interested in. This is not Efficient CodeTM. On the other hand, it is Working CodeTM so it'll do as a proof of concept. I'll make it less awful when it gets too slow or I translate it all over to the API.

In the meantime, I have a document tagging system, and I can still use my favourite online text editor, even if I had to write the code in my less than favourite online code editor. And here is the code all in one place. One day I'll get tagging and Markdown support from Google itself...