## Book editor in Google Apps Script

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:

1. Editable anywhere (really I just mean that it should be browser based).
2. Simple formatting (Markdown is my usual go-to)
3. Ability to add and embed images easily
4. Code highlighting (Markdown plugins to the rescue)
5. Easy linking to other sections
6. Easy mathematical typesetting (maybe not for this class, but still)
7. Easy way to build shortcuts (like typing <<GAS>> to produce “Google Apps Script”)
8. 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.

## Markdown

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.

## Image management

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.

## Shortcuts

I used to love the shortcuts I could create in $\LaTeX$ 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.

## User Authentication

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.

## Limitations

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:

1. 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.
2. Update your chapters and section numbers (watch the end of the vid linked above to see how that works)
3. Go to tools->script editor
4. In the script editor, update the top 2 lines of the globals.gs file
1. Note that you’ll want to make a new folder for your images and set it to viewed by anyone
5. Go to Deploy in the upper right
6. Click on “New Deployment”
7. Choose “Web App”
8. 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 $\LaTeX$. 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 . . .

Professor of physics at Hamline University in St. Paul, MN
This entry was posted in Google Apps Script, syllabus creation, teaching, technology. Bookmark the permalink.

### 2 Responses to Book editor in Google Apps Script

1. bretbenesh says:

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.

Did you look at PreTeXt? This is some combination of LaTeX and XML for books. https://pretextbook.org/

• Andy Rundquist says:

I’ve looked at them before (when they were called something else) and I really like the idea of user-friendly latex with good organization, but I’m currently stuck on needing to have a local repository. I’m so wedded to my chromebook for everything so I’m constantly looking for full web implementations. I know that’s a weird requirement, but it’s where I’m stuck currently.