Google Apps Script for Google Slides: Comprehensive Guide
Google Apps Script for Google Slides allows you to automate and customize presentations. This guide covers the basics, key methods, and advanced examples, with exercises and multiple-choice questions to help you master Google Slides automation.
What is Google Apps Script for Slides?
Google Apps Script enables you to programmatically create, modify, and manage Google Slides presentations using JavaScript. It can automate tasks like creating slides, adding content, and applying styles.
How to Use Google Apps Script with Slides
- Open the Apps Script Editor.
- Create a new script.
- Save and authorize the script to access Google Slides.
Key SlidesApp Methods
- SlidesApp.getActivePresentation(): Access the active presentation.
- createSlide(): Add new slides.
- getSlides(): Retrieve all slides in a presentation.
- insertTextBox(): Add text boxes.
- insertImage(): Add images.
Basic Examples
Example 1: Create a New Slide
function createSlide() {
const presentation = SlidesApp.getActivePresentation();
const slide = presentation.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);
slide.getPlaceholder(SlidesApp.PlaceholderType.TITLE).asShape().setText(“New Slide”);
slide.getPlaceholder(SlidesApp.PlaceholderType.BODY).asShape().setText(“This is the body content.”);
}
Explanation:
- appendSlide(layout): Adds a new slide with a predefined layout.
- getPlaceholder(type): Accesses placeholders (e.g., title, body).
- setText(): Sets text for placeholders.
Example 2: Add a Text Box
function addTextBox() {
const slide = SlidesApp.getActivePresentation().getSlides()[0];
slide.insertTextBox(“Hello, Apps Script!”, 100, 100, 400, 50);
}
Explanation:
- insertTextBox(text, left, top, width, height): Adds a text box at specified coordinates with dimensions.
Example 3: Insert an Image
function addImage() {
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const imageUrl = “https://via.placeholder.com/150”;
slide.insertImage(imageUrl, 50, 50, 200, 150);
}
Explanation:
- insertImage(url, left, top, width, height): Adds an image at specified coordinates with dimensions.
Advanced Examples
Example 4: Apply Text Styles
function styleText() {
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const textBox = slide.insertTextBox(“Styled Text”, 100, 100, 400, 50);
const textRange = textBox.getText().getRange(0, 11);
textRange.setFontFamily(“Arial”).setFontSize(24).setForegroundColor(“#ff0000”);
}
Explanation:
- getRange(start, end): Selects a range of text.
- setFontFamily(), setFontSize(), setForegroundColor(): Apply styles to text.
Example 5: Create a Custom Slide Deck
function createSlideDeck() {
const presentation = SlidesApp.create(“My Slide Deck”);
const slide = presentation.appendSlide(SlidesApp.PredefinedLayout.TITLE);
slide.getPlaceholder(SlidesApp.PlaceholderType.TITLE).asShape().setText(“Welcome to My Slide Deck”);
slide.getNotesPage().getSpeakerNotesShape().setText(“Remember to greet the audience.”);
}
Explanation:
- SlidesApp.create(name): Creates a new presentation.
- getNotesPage().getSpeakerNotesShape(): Adds speaker notes to a slide.
Exercises
Exercise 1: Add a Bullet List
Write a script to add a slide with a bullet list containing three items: “Item 1”, “Item 2”, “Item 3”.
Solution:
function addBulletList() {
const slide = SlidesApp.getActivePresentation().appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);
slide.getPlaceholder(SlidesApp.PlaceholderType.TITLE).asShape().setText(“Bullet List”);
const body = slide.getPlaceholder(SlidesApp.PlaceholderType.BODY).asShape();
body.setText(“Item 1\nItem 2\nItem 3”);
body.getText().getParagraphs().forEach((paragraph) => paragraph.setBullet(true));
}
Exercise 2: Duplicate a Slide
Write a script to duplicate the first slide in the presentation.
Solution:
function duplicateSlide() {
const presentation = SlidesApp.getActivePresentation();
const firstSlide = presentation.getSlides()[0];
presentation.appendSlide(firstSlide);
}
Exercise 3: Highlight a Specific Word
Write a script to find and highlight the word “important” in the text on the first slide.
Solution:
function highlightWord() {
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const textElements = slide.getShapes();
textElements.forEach((shape) => {
const text = shape.getText();
if (text) {
const indices = [];
let index = text.asString().indexOf(“important”);
while (index !== -1) {
indices.push(index);
index = text.asString().indexOf(“important”, index + 1);
}
indices.forEach((start) => {
const range = text.getRange(start, start + “important”.length);
range.setForegroundColor(“#FFFF00”); // Highlight in yellow
});
}
});
}
Multiple-Choice Questions
Question 1:
Which method is used to add a new slide to a presentation?
- SlidesApp.addSlide()
- SlidesApp.appendSlide()
- SlidesApp.createSlide()
- SlidesApp.newSlide()
Answer: 2. SlidesApp.appendSlide()
Question 2:
What does setText(“Welcome”) do?
- Adds a new slide with the text “Welcome”.
- Sets the title of the presentation to “Welcome”.
- Sets the content of a text box or placeholder to “Welcome”.
- Highlights the text “Welcome”.
Answer: 3. Sets the content of a text box or placeholder to “Welcome”.
Question 3:
Which method is used to add an image to a slide?
- addImage(url)
- insertImage(url)
- appendImage(url)
- addPicture(url)
Answer: 2. insertImage(url)
Advanced Example: Generate a Presentation with Data
function generatePresentation() {
const presentation = SlidesApp.create(“Sales Report”);
const data = [
[“Q1”, “$10,000”],
[“Q2”, “$15,000”],
[“Q3”, “$12,000”],
[“Q4”, “$18,000”]
];
data.forEach((row) => {
const slide = presentation.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);
slide.getPlaceholder(SlidesApp.PlaceholderType.TITLE).asShape().setText(`Sales: ${row[0]}`);
slide.getPlaceholder(SlidesApp.PlaceholderType.BODY).asShape().setText(`Revenue: ${row[1]}`);
});
}
Explanation:
- Iterates over data to create slides dynamically.
- Adds quarterly sales data to each slide.
Best Practices
- Use placeholders: Utilize predefined layouts for structured slides.
- Optimize scripts: Avoid unnecessary API calls to reduce execution time.
- Test thoroughly: Verify scripts on sample presentations before deploying them.