· google-docs

Google Docs: Find and replace script

I keep track of the podcasts that I’ve listened to in a Google Doc, having pasted the episode title and podcast name from Player.FM. The format isn’t exactly what I want so I’ve been running the Find and Replace command to update each entry. This is obviously a very boring task, so I wanted to see if I could automate it.

An example entry in the Google Doc reads like this:

* Listened to Worst of the Big 3 by The Tennis Podcast
https://player.fm/1BBKmKQ #nowplaying

And I want it to read like this:

* Listened to Worst of the Big 3 by The Tennis Podcast - https://player.fm/1BBKmKQ

We can do this by doing by replacing:

  • \nht with  - ht 

  • #nowplaying with nothing

We can create a Google script by clicking Tools > Script editor, which will take us to an editor with the following text:

function myFunction() {

}

We can write the following code to update our Google Doc:

function myFunction() {
  // https://docs.google.com/document/d/<document-id>/edit
  let documentId = "<document-id>"
  let document = DocumentApp.openById(documentId);
  let body = document.getBody().getText()
  document.getBody().setText(body.replace(/#nowplaying/g, "").replace(/\nht/g, " - ht"))
}

I tried using the replaceText function that can be run against the body of the document, but I couldn’t figure out how to get that to work with the new line character.

We can then run that manually to update our document, but we can go one better and setup a trigger. We can setup a trigger via the Edit menu:

triggers
Figure 1. Triggers

Once the next screen loads, we can then click on Add Trigger to set everything up:

add trigger
Figure 2. Add Trigger

This trigger will run our script once an hour, removing new lines and the #nowplaying phrase.

Boredom removed!

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket