Google Sheets to Slides w/Apps Script - How do you keep cell text color?

139 Views Asked by At

trying to move data from sheets to auto fill a template in Google Slides. I've gotten it to work, however, I want it to keep the colors of the font of the shapes. The shapes are just the font with color. Any thoughts on what I need to add? (First timer here :)

Sheet Data

Slide Template

Final Output Desired

My code is below:

function createOneSlidePerRow() {
let masterDeckID = "deck ID";
let masterSheetID= "sheet ID"
let deck = SlidesApp.openById(masterDeckID);
let slides = deck.getSlides();
let masterSlide = slides[1];

let dataRange = SpreadsheetApp.openById(masterSheetID).getDataRange();
let sheetContents = dataRange.getValues();
let header = sheetContents.shift();
let updatedContents = [];
sheetContents.reverse();
sheetContents.forEach(function (row) {
let slide = masterSlide.duplicate();

slide.replaceAllText("{{full_name}}", row[0]);
slide.replaceAllText("{{symbol_1}}", row[1]);
slide.replaceAllText("{{symbol_2}}", row[2]);
slide.replaceAllText("{{symbol_3}}", row[3]);
}  

I found this but I have no idea how to adapt: Google Apps Script: Replace all text in slide table with values and formatting from sheets

1

There are 1 best solutions below

5
Tanaike On

From your updated question, how about the following modification?

Modified script:

Before you use this script, please set masterDeckID and masterSheetID.

function createOneSlidePerRow() {
  let masterDeckID = "deck ID";
  let masterSheetID = "sheet ID"
  let deck = SlidesApp.openById(masterDeckID);
  let slides = deck.getSlides();
  let masterSlide = slides[1];
  let dataRange = SpreadsheetApp.openById(masterSheetID).getDataRange();

  // I modified the below script.
  const [header, ...values] = dataRange.getValues();
  const [, ...colors] = dataRange.getFontColorObjects();
  values.forEach((row, i) => {
    let slide = masterSlide.duplicate();
    slide.getShapes().forEach(s => {
      header.forEach((h, j) => {
        const text = s.getText();
        if (text.replaceAllText(h, row[j]) != 0) {
          const c = colors[i][j].asRgbColor().asHexString();
          text.getTextStyle().setForegroundColor(c.length > 7 ? "#000000" : c);
        }
      });
    });
  });
}
  • When this script is run using your showing sample Spreadsheet and Slide, I think that your expected result is obtained.

References:

Added:

From your following reply,

This is so close! It brings in the color, but it makes all symbols the same color as Symbol 3 instead of letting them have their own unique color.

Will this script bring your expected result?

function createOneSlidePerRow() {
  let masterDeckID = "deck ID";
  let masterSheetID = "sheet ID"
  let deck = SlidesApp.openById(masterDeckID);
  let slides = deck.getSlides();
  let masterSlide = slides[1];
  let dataRange = SpreadsheetApp.openById(masterSheetID).getDataRange();

  // I modified the below script.
  const [header, ...values] = dataRange.getValues();
  const headerColors = dataRange.getFontColorObjects()[0];
  values.forEach((row, i) => {
    let slide = masterSlide.duplicate();
    slide.getShapes().forEach(s => {
      header.forEach((h, j) => {
        const text = s.getText();
        if (text.replaceAllText(h, row[j]) != 0) {
          const c = headerColors[j].asRgbColor().asHexString();
          text.getTextStyle().setForegroundColor(c.length > 7 ? "#000000" : c);
        }
      });
    });
  });
}