I’m teaching a class in the fall called “Web App Development with Google Apps Script” that I think I want to write my own book for. I started doing some of that using wikibooks, but I was frustrated at some of the limitations that platform has, namely that you have to click around a lot as you’re going back and forth among sections and it’s really hard to add images, as most are considered to be copyrighted unless you jump through a bunch of hoops.
So I thought it might be fun to see if I could make a book editor in Google Apps Script. That’s pretty meta, huh?
tl;dr? I made a ton of progress. Here’s a vid showing the features.
What features am I looking for?
Here’s the things that I’m interested in having for a book editor:
- Editable anywhere (really I just mean that it should be browser based).
- Simple formatting (Markdown is my usual go-to)
- Ability to add and embed images easily
- Code highlighting (Markdown plugins to the rescue)
- Easy linking to other sections
- Easy mathematical typesetting (maybe not for this class, but still)
- Easy way to build shortcuts (like typing <<GAS>> to produce “Google Apps Script”)
- Available to everyone, but editable by only me
It turns out that a combination of bootstrap, mathjax, markdown-it, and highlighter cdns and built in GAS features enabled me to build all 8 in, so I’m pretty happy for the moment.
Not familiar with Markdown? It’s really just a way to type readable notes that can be rendered into decent html. It doesn’t have every bell and whistle, but it’s got enough for my taste. Here’s a great page describing the typical features.
For my dashboard project I figured out how to upload images to google drive and how to determine the url that can be put into an img tag. So I just reused that code, but augmenting it to let me put in a description that’s the default alt text to be used. I also made it so that you can browse the images you’ve already uploaded if you want to reuse them elsewhere in the book.
The other cool thing I learned how to do was to populate something in the browser’s clipboard on demand. Very cool.
I used to love the shortcuts I could create in documents. Things like \qm for “Quantum Mechanics” etc. I realized I could do that in this project too, but at first I just hard coded them into the rendering portion of the code. Basically I did a bunch of:
displayText = displayText.replace(/<<qm>>/g, "Quantum Mechanics")
However, I realized I could just put the pattern (/<<gm>>/g) and the replacement (Quantum Mechanics) into spreadsheet columns and then just run through as many as the user wants to add.
So now I just edit my spreadsheet with new shortcuts (I called them filters but the idea is the same) and the next time I load the editor those shortcuts are available.
GAS is really great for user authentication. This command gets you the email of the current user:
var email = Session.getActiveUser().getEmail();
and you can do whatever you want with that, including allowing editing vs limiting the user to viewing your book. When you deploy a Web App you can say that it executes as you but that it’s available to the world. When someone outside of your domain goes to your web site, the command above returns an empty string. But when someone in your domain goes to it, you get their email. So you could imagine lots of editors, for example.
GAS has lots of limitations. It’s not particularly fast, though once you pass your data to the user it’s really fast then. Sending data back and forth to the google spreadsheet usually takes a couple of seconds, which isn’t the end of the world given all the other features you get (for free!).
I pass to the user all the chapter and section names but not the detailed text of the sections, only sending that for the chosen section. So each time you go to a new section, it has to send an asynchronous request to google to get it. Again, ~2 seconds.
I’m assuming my book will get long enough that sending the whole text of the book will be problematic, so that’s why I only ever have the text from one section in memory.
Since it’s all being saved in a google spreadsheet, you have some fundamental limits to length. There are some conflicting sources out there, but there’s agreement that you can’t have more than 5,000,000 cells in the spreadsheet. That’s a lot of chapters and sections. There’s some sources that say no single cell can have more than 50,000 characters, but it seems that not everyone agrees. Assuming an average word length of, say, 8 characters that would mean that sections of the book would have to be less than 6,000 words. Since none of my blog posts have ever been that long, I don’t think I’m worried about that.
What’s it good for?
Of course I built this to write a book for my class, but since it’s all contained in a spreadsheet, it’s super easy to make copies! If you go to this spreadsheet and make your own copy, you too can write a book. All you’d have to do is:
- Clear out all the data (but not the top rows) in each tab. Note that column E in the “sections” tab is hidden, you’ll want to delete those too.
- Update your chapters and section numbers (watch the end of the vid linked above to see how that works)
- Go to tools->script editor
- In the script editor, update the top 2 lines of the globals.gs file
- Note that you’ll want to make a new folder for your images and set it to viewed by anyone
- Go to Deploy in the upper right
- Click on “New Deployment”
- Choose “Web App”
- Follow the instructions and deploy. You’ll be shown your new URL
I think I’ll probably use it for a lot of things. Even in classes where I’m not writing a book, I could still use it for organizing additional resources for my students.
I also think it might be really great for making manuals on how to do things.
Here are some starters for you:
- I love this, especially the part about . . .
- I hate this. Why don’t you just use . . .
- Wait, this is for the fall term? Don’t you have some final projects to grade?
- I think you should teach a class that teaches people how to make this tool that lets them write a book for a class on how to teach the class. That would be more meta.
- I think it would be cool if you could add . . .
- Markdown is just watered down . Why not use that instead?
- I’ve got a great idea for what I’d use this for . . .
- I’ve got a great idea for how you should never use this . . .