In our previous article (Part 64), we learned how to use Slide.insertShape() to automatically draw topic cards for highlighting key business achievements directly on Google Slides!
Once you know how to place shapes on your slides with Google Apps Script, the next step is automating their design to match your data and make your presentation look polished!
For example, when creating a monthly business review…
- When a performance target is met (100% or higher) ➔ Turn the card background into an easy-on-the-eyes pastel green and frame it with a thick, solid green border!
- When a target is missed (under 100%) ➔ Turn the card background into an alert pastel red and add a dashed red border to draw immediate attention!
Recoloring shapes manually every month can be tedious. With GAS, you can instantly transform the design of your slides based purely on achievement rates.
In this 65th installment of the GAS Beginner Series, we will cover everything from the fundamentals to advanced techniques for controlling shape background colors with Shape.getFill() and styling borders (color, thickness, and line style) with Shape.getBorder().
Using a “Sapporo Office Monthly Performance Report” as our practical project, we will also dive into the two-tier object hierarchy that often trips up beginners!
1. What Are Shape.getFill() and Shape.getBorder()?
A shape in Google Slides is not just a single monolithic element; it consists of multiple specialized child objects working together.
When updating the fill color or border of a shape using GAS, you do not directly tell the shape itself to “turn red.” Instead, you retrieve the relevant component object and apply your styling commands to it.
[Shape]
├── 1. Background Fill Handler ➔ Fill object (retrieved via getFill())
└── 2. Border Styling Handler ➔ Border object (retrieved via getBorder())
Think of It as “Rooms, Furniture, and Specialized Staff”
Let’s review the object hierarchy analogy we have used throughout this series (such as Parts 2 to 4):
- [House] = The entire presentation (
SlidesApp.getActivePresentation()) - [Room] = A single slide (
presentation.getSlides()[0]) - [Furniture] = A placed shape (
slide.getShapes()[0])
Inside this piece of furniture (the shape), there are two dedicated specialists ready to help:
getFill()- Role: Calls the fill specialist.
- Ask this specialist (the
Fillobject) when you want to change the background to pastel green or make it transparent.
getBorder()- Role: Calls the border specialist.
- Ask this specialist (the
Borderobject) when you want to set the line weight to 3 points, switch the style to dashed, or change the border color to dark green.
Following this two-step process—first retrieving the specialized object from the shape, then calling the styling method—is the core rule of the Google Slides API!
2. STEP 1: Set Up the “Sapporo Office Monthly Performance Report” Slide
First, let’s create a test slide where we can experiment with GAS styling.
Setup Steps
- Create a new Google Slides presentation in Google Drive.
- Name the presentation “Sapporo Office Monthly Performance Report”.
- Add a single rectangle (card) on the first slide:
- In the menu bar, go to Insert > Shape > Shapes > Rectangle and draw a box in the center of the slide.
- (Tip: You can also reuse the shape created automatically with the
Slide.insertShape()script from Part 64!)
- With the slide open, click Extensions > Apps Script from the top menu to open the script editor.
You are now ready to code!
3. STEP 2: Hands-On Practice with Fill Colors and Borders
Let’s run a basic script that turns the rectangle’s background to pastel green (#D4EDDA) and applies a 3pt solid dark green border (#155724).
1. Enter the Code
Clear any existing code in the Apps Script editor, then copy and paste the following script:
function formatCardDesign() {
// 1. Get the "House" (presentation) and "Room" (first slide)
var presentation = SlidesApp.getActivePresentation();
var slide = presentation.getSlides()[0];
// 2. Get the first shape ("furniture") on the slide
var shapes = slide.getShapes();
if (shapes.length === 0) {
Logger.log("No shapes found on the slide! Please add a shape first.");
return;
}
var card = shapes[0];
// ----------------------------------------------------
// 3. Configure the Fill
// ----------------------------------------------------
var fill = card.getFill();
fill.setSolidFill("#D4EDDA"); // Set to pastel green
// ----------------------------------------------------
// 4. Configure the Border
// ----------------------------------------------------
var border = card.getBorder();
border.setWeight(3); // Set border thickness to 3 points
border.getLineFill().setSolidFill("#155724"); // Set border color to dark green
border.setDashStyle(SlidesApp.DashStyle.SOLID); // Set border style to solid
// 5. Completion log
Logger.log("Updated card design for the Sapporo Office!");
}
2. Step-by-Step Code Explanation
var fill = card.getFill();- Retrieves the
Fillobject responsible for the background of thecardshape.
- Retrieves the
fill.setSolidFill("#D4EDDA");- Applies a solid fill color. Here, we specify a gentle light green using the hex color code
"#D4EDDA".
- Applies a solid fill color. Here, we specify a gentle light green using the hex color code
var border = card.getBorder();- Retrieves the
Borderobject responsible for outline styling.
- Retrieves the
border.setWeight(3);- Sets the border thickness in points. We use
3pt here so the border stands out clearly.
- Sets the border thickness in points. We use
border.getLineFill().setSolidFill("#155724");- Key Takeaway: To change the border color, you must call
border.getLineFill()first, then runsetSolidFill()on that line fill object.
- Key Takeaway: To change the border color, you must call
border.setDashStyle(SlidesApp.DashStyle.SOLID);- Configures the border line style as a continuous solid line.
3. Save and Run the Script
- Click the Save (disk icon) at the top of the editor.
- Click Run. (If running for the first time, grant the required permissions when prompted, as covered in Part 2).
- Switch back to your Google Slides tab to view the update!
[Verification]
If the rectangle on your slide has instantly updated with a clean pastel green fill and a bold dark green border, the script executed successfully!
4. STEP 3: Automatically Toggle Green or Red Based on Achievement Rates
Now that you understand the basics, let’s build dynamic conditional logic using an if statement—a pattern you will use often in production reporting workflows.
We will evaluate the Sapporo Office monthly sales achievement rate and toggle the card’s fill and border styling automatically.
Practical Script: Updating Cards Based on Achievement Rate
function updateCardByAchievement() {
var presentation = SlidesApp.getActivePresentation();
var slide = presentation.getSlides()[0];
var card = slide.getShapes()[0]; // Get the first shape
// Sapporo Office achievement rate (in production, fetch this from Sheets or an API)
var achievementRate = 120; // Example: 120% target achieved!
// var achievementRate = 85; // Example: Uncomment to test 85% (target missed)
// Retrieve Fill and Border objects
var fill = card.getFill();
var border = card.getBorder();
if (achievementRate >= 100) {
// ====================================================
// [Target Achieved (>= 100%)] Green Success Theme
// ====================================================
fill.setSolidFill("#D4EDDA"); // Light green fill
border.getLineFill().setSolidFill("#28A745"); // Vibrant green border
border.setWeight(4); // Bold 4pt weight
border.setDashStyle(SlidesApp.DashStyle.SOLID); // Solid line
Logger.log("[Achieved] Rate: " + achievementRate + "% -> Applied green card style.");
} else {
// ====================================================
// [Target Missed (< 100%)] Red Warning Theme
// ====================================================
fill.setSolidFill("#F8D7DA"); // Light red fill
border.getLineFill().setSolidFill("#DC3545"); // Alert red border
border.setWeight(3); // 3pt weight
border.setDashStyle(SlidesApp.DashStyle.DASH); // Dashed border to draw attention
Logger.log("[Missed] Rate: " + achievementRate + "% -> Applied red dashed card style.");
}
}
How to Test the Logic
- Running the script with
var achievementRate = 120;results in a green card with a solid border. - Changing the value to
var achievementRate = 85;and running it again flips the styling to a red card with a dashed border!
By combining this with Google Sheets integration (such as getValues() from Part 13), you can build an automated reporting pipeline that pulls metrics and styles slide decks without any manual edits!
5. Key Methods for Fill and Border Objects
Here is a handy reference table of frequently used methods for styling shapes in production scripts:
1. Core Fill Methods
| Method | Description | Example |
setSolidFill(color) |
Sets a solid color (HEX string, RGB, or named color) | fill.setSolidFill("#FF0000"); |
setSolidFill(r, g, b) |
Sets a solid color using RGB values (0–255) | fill.setSolidFill(212, 237, 218); |
setTransparent() |
Removes the fill, making the background transparent | fill.setTransparent(); |
getFillType() |
Retrieves the current fill type | var type = fill.getFillType(); |
2. Core Border Methods
| Method | Description | Example |
setWeight(points) |
Sets the border thickness in points (pt) | border.setWeight(3); |
getLineFill() |
Returns the LineFill object used to set line color |
border.getLineFill().setSolidFill("#155724"); |
setDashStyle(style) |
Sets the stroke style (solid, dotted, dashed) | border.setDashStyle(SlidesApp.DashStyle.DASH); |
setTransparent() |
Hides the border completely (transparent) | border.setTransparent(); |
Border Style Options (SlidesApp.DashStyle)
SlidesApp.DashStyle.SOLID➔ Continuous solid line (default)SlidesApp.DashStyle.DASH➔ Dashed line (—-)SlidesApp.DashStyle.DOT➔ Dotted line (••••)SlidesApp.DashStyle.DASH_DOT➔ Alternating dash-and-dot line (-•-•-)
6. 5 Common Traps Beginners Face and How to Fix Them
Here are the most common pitfalls and errors developers encounter when working with shape styling in GAS.
Trap 1: Calling shape.setFillColor() Causes a TypeError
// Incorrect: Calling setFillColor directly on the Shape
card.setFillColor("#D4EDDA");
// -> Error: card.setFillColor is not a function
Cause and Solution:
The Shape object itself does not have a setFillColor method.
Always retrieve the fill handler first via card.getFill().setSolidFill("#D4EDDA").
Trap 2: Calling border.setColor() Causes a TypeError
// Incorrect
border.setColor("#155724");
// -> Error: border.setColor is not a function
Cause and Solution:
The Border object does not have a direct setColor method either.
Because border colors are handled as line fills, you must chain down into the line fill object: border.getLineFill().setSolidFill("#155724").
Trap 3: Border Stays Invisible Even After Setting Weight
// Border weight is set, but no line shows up on the slide...
border.setWeight(3);
Cause and Solution:
If the original shape border was set to transparent (no outline), adjusting the weight alone may not make it visible.
Ensure visibility by explicitly assigning a color with border.getLineFill().setSolidFill(...) or specifying a dash style with border.setDashStyle(...).
Trap 4: Forgetting the Leading Hash Symbol (#) in Hex Color Codes
// Incorrect: Causes an error or applies an unexpected color
fill.setSolidFill("D4EDDA");
// Correct: Include the leading # symbol
fill.setSolidFill("#D4EDDA");
When supplying hex color codes as strings, always include the leading #. Without it, Apps Script will not parse the color correctly. (Feel free to revisit the color format rules from Part 9 on setBackground()!)
Trap 5: Accidentally Affecting Text Boxes or Images on the Slide
// When fetching all slide elements at once
var elements = slide.getPageElements();
Cause and Solution:
A slide can contain diverse element types such as shapes, text boxes, images, and tables.
Calling getFill() on an element that does not support it (like certain images or tables) will throw an error. When you only want to adjust shape styling, it is safer to use slide.getShapes() to retrieve shapes exclusively.
7. Summary and What’s Coming Next
In this lesson, we explored how to use Shape.getFill() and Shape.getBorder() to customize Google Slides shape styling programmatically!
Key Takeaways
- Shape styling follows a two-tier object model:
- Fill ➔
card.getFill().setSolidFill(...) - Border Color ➔
card.getBorder().getLineFill().setSolidFill(...)
- Fill ➔
- Control line thickness with
Border.setWeight()and style lines withsetDashStyle()! - Combine styling with conditional logic (
ifstatements) to automatically switch card themes between success (green) and warning (red) based on metrics!
Mastering fill and border customization makes your automated slide presentations look clean, professional, and visually engaging.
Now that your summary cards have dynamic styling, the natural next step is populating them with summary text and monthly highlights automatically!
In Part 66, we will look into Slide.insertTextBox() to insert and format text boxes anywhere on your slide via script!
