google-apps-scripttypeerrorgoogle-slides

How does one set the background color of a slide once appended in Apps Script?


I had the idea of making a Google Slideshow in Apps Script, and each appended slide would be a specific color color based on the value of a data array representing a student's grade from a Google Spreadsheet.

function getDataFromSheet() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
   return sheet.getRange("Grades!A2:B9").getValues();
}

function createSlides() {
  var presentation = SlidesApp.create('Student Grades');
  return presentation;
}

I tried using an array to store the hexadecimal codes for the colors and a switch statement to determine which color would be applied to each slide:

function createSlideWithColor() {
  var data = getDataFromSheet();
  var slides = createSlides();
  
  for (var i = 0; i < data.length; i++) {
    var slide = slides.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);
    var shapes = slide.getShapes();
    var current_slide_bg = SlidesApp.getActivePresentation().getSelection().getCurrentPage().getBackground();
    var grade = data[i][1];
    var hex_colors = ['#209fff', '#1dff00', '#f3ff00', '#ffb100', '#ff2718'];

    shapes[0].getText().setText(data[i][0]);
    shapes[1].getText().setText(data[i][1]);

    switch(grade) 
    {
      case "A":
        current_slide_bg.setSolidFill(hex_colors[0]);
        break;
      case "B":
        current_slide_bg.setSolidFill(hex_colors[1]);
        break;
      case "C":
        current_slide_bg.setSolidFill(hex_colors[2]);
        break;
      case "D":
        current_slide_bg.setSolidFill(hex_colors[3]);
        break;
      case "F":
        current_slide_bg.setSolidFill(hex_colors[4]);
        break;
      default:
        Logger.log("ERROR");
    }
  }
}

And yet, it shows the error message: TypeError: Cannot read properties of null (reading 'getSelection') Is there something different that you type for appended slides? Or am I just doing it completely wrong?


Solution

  • In your showing script, you are using var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();. So, I guessed that your script is the container-bound script of Google Spreadsheet. But, you use SlidesApp.getActivePresentation() at var current_slide_bg = SlidesApp.getActivePresentation().getSelection().getCurrentPage().getBackground();. In this case, SlidesApp.getActivePresentation() is null. I thought that this might be the reason for your current issue of ypeError: Cannot read properties of null (reading 'getSelection').

    In your script, I thought that slide of var slide = slides.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY); can be used. So, how about the following modification?

    From:

    var current_slide_bg = SlidesApp.getActivePresentation().getSelection().getCurrentPage().getBackground();
    

    To:

    var current_slide_bg = slide.getBackground();
    

    The modified whole script is as follows.

    function getDataFromSheet() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      return sheet.getRange("Grades!A2:B9").getValues();
    }
    
    function createSlides() {
      var presentation = SlidesApp.create('Student Grades');
      return presentation;
    }
    
    function createSlideWithColor() {
      var data = getDataFromSheet();
      var slides = createSlides();
      for (var i = 0; i < data.length; i++) {
        var slide = slides.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);
        var shapes = slide.getShapes();
        // var current_slide_bg = SlidesApp.getActivePresentation().getSelection().getCurrentPage().getBackground();
        var current_slide_bg = slide.getBackground();
        var grade = data[i][1];
        var hex_colors = ['#209fff', '#1dff00', '#f3ff00', '#ffb100', '#ff2718'];
        shapes[0].getText().setText(data[i][0]);
        shapes[1].getText().setText(data[i][1]);
        switch (grade) {
          case "A":
            current_slide_bg.setSolidFill(hex_colors[0]);
            break;
          case "B":
            current_slide_bg.setSolidFill(hex_colors[1]);
            break;
          case "C":
            current_slide_bg.setSolidFill(hex_colors[2]);
            break;
          case "D":
            current_slide_bg.setSolidFill(hex_colors[3]);
            break;
          case "F":
            current_slide_bg.setSolidFill(hex_colors[4]);
            break;
          default:
            Logger.log("ERROR");
        }
      }
    }