首页 > 解决方案 > Google Apps Script: Color a table cell on the Google Slide, based on the value of a cell on the Google Sheets

问题描述

I'm reading rows from a google sheet and creating a slide for each of them on the google slides. To do this, I use a template slide which is the first slide, which has placeholders for each column's value on the slide.

While I've been able to achieve getting the data on the sheet, I want to dynamically set the background color of a table I have on my slide, based on the value it gets populated with.

For instance, I have a table column named "Priority" and it can have values "Low", "Medium", and "High". I want the background color to be Green, Orange and Red based on these values. I can't set this background color in the template slide, as that would make all the values have the same color. I'm looking for a way to dynamically color this specific cell of this specific table on my slide, and I have multiple tables on my slide. I also need a way to identify my target table.

I'm looking through the documentation Google Apps Script provides, but I haven't quite been able to narrow down what API it is exactly, that I need to achieve this. I've looked into Slide>Table Class, and while there are some APIs that could like get the table, get the cell, etc., I don't see anything that could help me achieve my use case.

//Ideally, this is how it would work if I had the API:
//Pseudocode below:
let myTableIdentifier = "FooBar"; //How do I get this identifier which tells one table apart from another on your slide?
let tables = mySlide.getTables();
let myTargetTable = tables.get(myTableIdentifier); //Not sure if this is possible, getting a table based on some identifier.
let incomingValue = "High";
let backgroundColor = _colorPicker(incomingValue); //Assume this function gets me the color name, like "Red".
myTargetTable.getCell("Priority").setBackgroundColor(backgroundColor);

What am I missing here? Can this even be done with the APIs already available?

标签: google-apps-scriptgoogle-sheets-apigoogle-slides-api

解决方案


推荐阅读