Home > Net >  Having trouble setting Theme Colors in Google Slides via the Slides API batch update
Having trouble setting Theme Colors in Google Slides via the Slides API batch update

Time:08-18

I am creating a new presentation and would like to set the theme colors. When I run the following code, I get the error:

"GoogleJsonResponseException: API call to slides.presentations.batchUpdate failed with error: Invalid requests[0].updatePageProperties: Color scheme cannot have duplicate theme color types"

Below is my code...

var presentation = Slides.Presentations.create({
    title: 'colorSchemeTest'
});

var master = presentation.masters[0];
var requests = [{
  updatePageProperties: {
    objectId: master.objectId,
    pageProperties: {
      colorScheme: COLOR_SCHEME
    },
    fields: "colorScheme.colors"
  }
}];
  Slides.Presentations.batchUpdate( { requests: requests }, presentation.presentationId );

...and here is the enum COLOR_SCHEME

var COLOR_SCHEME = {
  colors: [
    {//Black
      type: "DARK1",
      color: {
        red: 0,
        green: 0,
        blue: 0
      }
    },
    {//Dark Gray
      type: "DARK2",
      color: {
        red: .263,
        green: .263,
        blue: .263
      }
    },
    {//White
      type: "LIGHT1",
      color: {
        red: 1,
        green: 1,
        blue: 1
      }
    },
    {//Whisper Gray
      type: "LIGHT2",
      color: {
        red: .953,
        green: .953,
        blue: .953
      }
    },
    {// Blue 
      type: "ACCENT1",
      color: {
        red: .259,
        green: .522,
        blue: .957
      }
    },
    {// Green 
      type: "ACCENT2",
      color: {                   
        red: .204,
        green: .659,
        blue: .325
      }
    },
    {// Yellow 
      type: "ACCENT3",
      color: {           
        red: .984,
        green: .737,
        blue: .020
      }
    },
    {// Red 
      type: "ACCENT4",
      color: {                   
        red: .918,
        green: .263,
        blue: .208
      }
    },
    {//Purple
      type: "ACCENT5",
      color: {                   
        red: .612,           
        green: .153,
        blue: .690
      }
    },
    {//Gray
      type: "ACCENT6",
      color: {                   
        red: .800,
        green: .800,
        blue: .800
      }
    },
    {//Primary Gray
      type: "TEXT1",
      color: {
        red: .400,
        green: .400,
        blue: .400
      }
    },
    {//Another Gray
      type: "TEXT2",
      color: {
        red: .600,
        green: .600,
        blue: .600
      }
    }
  ]
}

CodePudding user response:

This is my first time addressing a Google Slides API problem, so the accuracy of my reasoning may be a bit off.

I fixed the error. I noticed that although you have listed 12 colors to complete the ColorScheme array (as per this documentation https://developers.google.com/slides/api/reference/rest/v1/presentations.pages/other#themecolortype) you are required to follow the ThemeColorType order as outlined in that documentation. In your case, switch positions for DARK2 & LIGHT1 as it does not follow the right order:

  • DARK1
  • LIGHT1
  • DARK2
  • LIGHT2
  • ACCENT1
  • ...
  • ACCENT6
  • HYPERLINK
  • FOLLOWED_HYPERLINK
  • TEXT1
  • BACKGROUND1
  • TEXT2
  • BACKGROUND2

This will correct the Color scheme cannot have duplicate theme color types error message.

In addition to that, it seems that you are required to enter a HYPERLINK & FOLLOWED_HYPERLINK ThemeColorPairs to complete the theme.

You need to add this to the enum COLOR_SCHEME (always following the order, additional ThemeColorPairs are optional):

{
  "type": "HYPERLINK",
  "color": {
    "red": 0.6,
    "green": 0.6,
    "blue": 0.6
  }
},
{
  "type": "FOLLOWED_HYPERLINK",
  "color": {
    "red": 0.9,
    "green": 0.9,
    "blue": 0.9
  }
}

Not adding these 2 ThemeColorPairs will result in the following error: Please provide the appropriate 12 theme colors to complete the color scheme

  • Related