Generate speaker notes in Google Slides using Gemini 2.0 Flash and Google Apps Script
Welcome!
Creating effective speaker notes for presentations can be a time-consuming task, especially when dealing with multiple slides. But what if you could automate the process? In this tutorial, I’ll walk you through how to build an automation script using Google Apps Script and the Gemini 2.0 model to generate clear, concise speaker notes for Google Slides.
By the end of this guide, you’ll have a fully functional script that extracts content from your slides, sends it to Gemini for AI-generated notes, and inserts the notes directly into each slide. Let’s get started!
If you want a video version of the blog, check out the YouTube Tutorial given below.
Google Slide Template
For this tutorial, I will be using a demo presentation about Google Apps Script. If you want to work with the presentation, click here.
Write the Automation Script
While you are in Google Slides, let’s open up the Script Editor to write some Google Apps Script. To open the Script Editor, follow these steps:
- Click on Extensions and open the Script Editor.
2. This brings up the Script Editor as shown below.
We have reached the script editor, let’s code.
function generateNotesForAllSlides() {
const presentation = SlidesApp.getActivePresentation();
const presentation_name = presentation.getName();
const slides = presentation.getSlides();
const GEMINI_ENDPOINT = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=YOUR_API_KEY'
The generateNotesForAllSlides()
is the main function that initiates the process of extracting data from the Slides, followed by fetching speaker notes from Gemini and inserting them back into the Slides.
We use the SlidesApp.getActivePresentation()
function, followed by the getSlides()
function that accesses the active presentation and retrieves all slides.
We then declare the Gemini endpoint with the API key that is fetched from Google AI Studio. For this tutorial we are using the Gemini 2.0 Flash model. To find out more about the Gemini API and all the models available, check out the link given below.
Now that we have access to the presentation and the slides, the next part of the code looks at identifying available shapes (Text boxes, titles) present on each slide and extract the slide content.
for (let slideIndex = 0; slideIndex < slides.length; slideIndex++) {
const slide = slides[slideIndex];
let content = "";
We start off with a for loop that we will use to iterate through each slide and extract its contents and store it in the content
variable.
const shapes = slide.getShapes();
for (let i = 0; i < shapes.length; i++) {
const shape = shapes[i];
//Add checks for null and if its the right type
if (shape && shape.getText) {
content += shape.getText().asString() + "\n";
}
}
Each slide can contain various shapes with text content. The slide.getShapes()
function retrieves all shapes elements on the slide. We then check to for null
, and ensure the shape exist and has text. Once ensured, we covert the shape’s text into a string and add it to the content
variable.
const tables = slide.getTables();
for (let j = 0; j < tables.length; j++) {
const table = tables[j];
const numRows = table.getNumRows();
const numCols = table.getNumColumns();
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
const cell = table.getCell(row, col);
content += cell.getText().asString() + "\n";
}
}
}
Logger.log(`Content of slide ${slideIndex + 1}:\n${content}`);
The slides could even contain a table and our code handles this by retrieving all the tables on the slide getTables()
function, followed by iterating two for loops. This is to iterate through every row and column to get text from each cell. Once we fetch the available data, we append the cell text to content
and log the text content extracted for the current slide.
const prompt = "Create concise and impactful speaker notes (2-3 bullet points maximum)
for the following Google Slides slide content, targeting a general audience
interested in automation.\n\nCloud-based JavaScript platform\nIntegrate, Automate
and extend\nBuild Custom tools and workflows\nCreate web apps and add-ons \nConnect to external APIs\n
What is Google Apps Script?\n\nSpeaker Notes:" + content;
This is the prompt that I’m going to be using for this tutorial. The prompt instructs Gemini to generate speaker notes based on the slide’s content. It’s designed to produced clear and engaging notes for a general audience interested in Google Apps Script/ Automation.
Note: You can always adjust the prompt based on the contents of your Google Slide presentation. Adjusting the prompt will produce clear and engaging notes.
var headers = {
"Content-Type": "application/json"
};
var requestBody = {
"contents": [
{
"parts": [
{
"text": prompt
}
]
}
]
};
var options = {
"method": "POST",
"headers": headers,
"payload": JSON.stringify(requestBody)
};
let output = null; // Initialize output outside the try/catch block
We know prepare the Gemini API call with necessary headers and payload. We set the content type to JSON and wrap the prompt inside content
-> parts
-> text
.
try {
var response = UrlFetchApp.fetch(GEMINI_ENDPOINT, options);
var datanew = JSON.parse(response.getContentText());
output = datanew.candidates[0].content.parts[0].text;
Logger.log(datanew);
Logger.log(output);
} catch (error) {
Logger.log(`Error calling Gemini API for slide ${slideIndex + 1}: ${error}`);
output = "Error generating notes for this slide.";
}
We then use UrlFetchApp.fetch()
function to call the Gemini API. After which we parse the response and extract the generated text. Incase the API call fails, we log an error and set output
to an error message.
let notesPage = slide.getNotesPage();
let notes_shapes = notesPage.getShapes();
let notesShape = null;
for (let i = 0; i < notes_shapes.length; i++) {
if (notes_shapes[i].getShapeType() == SlidesApp.ShapeType.TEXT_BOX || notes_shapes[i].getShapeType() == SlidesApp.ShapeType.BODY) {
notesShape = notes_shapes[i];
Logger.log("Found a suitable shape (TEXT_BOX or BODY) at index: " + i);
break; // Exit loop once a suitable shape is found
}
Logger.log("Shape at index " + i + " is of type: " + notes_shapes[i].getShapeType()); //Debugging Log
}
Now that the speaker notes are ready, its time to insert them into the slide’s notes section. To do that we start off by accessing the notes section of the slide using the getNotesPage()
function. Once we access the notes we use the getShapes()
function to retrieve shapes from the notes page.
if (!notesShape) {
Logger.log("No suitable notes shape found on notes page.");
continue; // Skip this slide and go to the next
}
let textRange = notesShape.getText(); // Get text from the FOUND shape
textRange.setText(output);
Logger.log(`Notes added successfully to slide ${slideIndex + 1}.`);
} // End loop through slides
Logger.log("Finished processing all slides.");
}
We skip the slide if no suitable shape is found. We the retrieve the text range from the notes shape using the getText()
function. Using the setText()
function we insert the speaker notes generated by Gemini.
Our code is complete and good to go.
Step2: Check the Output
It’s time to see if the code is able to fetch the slide contents, send them to gemini, fetch back speaker notes and then inserting them back into the slides.
You can either run the code directly from the editor or use custom menus. Check out the tutorial give below to know more about different ways to execute your Google Apps Script code.
On running the code you should get an output like this in the Execution Log.
Here you can see on successful execution the code has been able to generate and insert the speaker notes. You can check the results inside the Google Slide too.
The code has inserted the speaker notes successfully. You can always adjust the prompt to your response requirements. If you want your speaker notes to be generated in a certain style, you will have to adjust the prompt to cater for that.
Conclusion
In this tutorial, we successfully, built an automation script that generates speaker notes for Google Slides using the Gemini 2.0 model and Google Apps Script. By extracting content from slides, crafting an effective prompt, and making an API call to Gemini, we automated the often tedious and time consuming task of creating speaker notes.
This approach not only saves time but also ensures clear, concise, and engaging notes for presentations. As a next step, consider experimenting with different prompts or extending the solution to handle larger presentations more effectively.
You can check out the code and various other Google Apps Script automation scripts on the link given below.
Feel free to reach out if you have any issues/feedback at aryanirani123@gmail.com.