Have you ever wanted to add a Random Quote to a Google Doc, or maybe some other piece of random content like a tip or funny joke? You can use “Google Apps Scripts” to create a script that will display a random piece of text from an array, or a Google Sheet, at the top of your Google Doc every time it’s opened. This is a fun and interesting way to make sure that all of those tips, tricks, inspitrational quotes, etc don’t just sit buried in some folder where you have saved them. Here’s a step-by-step guide to help you get started:
- Open Google Docs and access the script editor:
- Open your Google Doc.
- Click on
Extensions
>Apps Script
.
Write the script:
- In the script editor, you can write a script that fetches a random piece of text from an array or a Google Sheet and inserts it at the top of the document. Here’s an example script that uses an array of quotations:
function onOpen() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// Array of random texts
var texts = [
"Quote 1: The best way to predict the future is to invent it.",
"Quote 2: Life is 10% what happens to us and 90% how we react to it.",
"Quote 3: The only limit to our realization of tomorrow is our doubts of today."
];
// Get a random text
var randomText = texts[Math.floor(Math.random() * texts.length)];
// Clear the first paragraph and insert the random text
var firstParagraph = body.getParagraphs()[0];
firstParagraph.setText(randomText);
}
- Set up the trigger:
- To ensure the script runs every time the document is opened, you need to set up a trigger.
- In the script editor, click on the clock icon (Triggers) in the left sidebar.
- Click on
+ Add Trigger
. - Set the function to
onOpen
, the deployment toHead
, the event source toFrom document
, and the event type toOn open
. - Click
Save
.
- Save and authorize the script:
- Save your script by clicking the disk icon or pressing
Ctrl + S
. - You may need to authorize the script to access your document. Follow the prompts to grant the necessary permissions.
- Save your script by clicking the disk icon or pressing
It Works! A Random Quote Appears
Now, every time you open the Google Doc, a random piece of text from the array will be displayed at the top. Sometimes it takes a second while the the full document loads, but generally happens pretty quickly. Now you can see a Random Quote in your Google Doc each time you open the document.
If you want to fetch the text from a Google Sheet instead, you can modify the script to read from a specific range in your sheet. You could set the quotes to be all in the “A” column, one quote per line.
Here’s an example of how to fetch random text from a Google Sheet:
function onOpen() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// Open the Google Sheet and get the data
var sheet = SpreadsheetApp.openById('YOUR_SHEET_ID').getSheetByName('Sheet1');
var data = sheet.getRange('A1:A').getValues();
// Flatten the array and remove empty values
var texts = data.flat().filter(String);
// Get a random text
var randomText = texts[Math.floor(Math.random() * texts.length)];
// Clear the first paragraph and insert the random text
var firstParagraph = body.getParagraphs()[0];
firstParagraph.setText(randomText);
}
Replace 'YOUR_SHEET_ID'
with the actual ID of your Google Sheet. You can get this in a variety of ways, one of which is from the URL for the sheet. This script will fetch random text from the first column of the specified sheet and display it at the top of your Google Doc.
Styling Your Special Box
What if you want this little feature area to stand out a bit, with some nice colours and spacing, and maybe a border? We can apply styles to text right from within our script. That is what we will do next.
Happy scripting!