My Google Sheets Script Keeps Duplicating Unwanted Google Slides

69 views
Skip to first unread message

Karim Ballout

unread,
Mar 7, 2022, 5:07:08 AM3/7/22
to Google Apps Script Community
Dear Members,

I have a script that allows the Data in Google Sheets to be presented in Google Slides using placeholders.

The script does the following:

  • Duplicates a Google Slides Presentation Template that has a title slide and placeholders slide, to a new Presentation and places it in my Google Drive.

  • Replaces the Placeholder Slide from the New created Presentation with the Data in Google Sheets. Every Google Sheets row is presented in a slide.

The problem is that, say I have 12 rows, the placeholder slide is replaced with 12 slides, but so is the first Title slide. I get 12 Title Slides. 


What I would like to do is keep any duplicated slide from the Template Presentation to be intact except the slide where placeholders can be replaced.

Could someone please help me fix this ?

This is the script:

 function generateLandingPagesReport() {
  var templateId = "1bXAYGCKkpZhksXz8gTCgFYbNoI1BIhAZakd68VlXHeo";
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var deck = SlidesApp.openById(templateId);
  var sheet = ss.getSheetByName('Copy of Overall Performance 1');
  var values = sheet.getRange('A2:J23').getValues();
  var getslides = deck.getSlides();
  var presLength = getslides.length;

  // Create a Slides presentation, removing the default
  // title slide.
  var presentationTitle =
    ss.getName() + " Presentation";
  var slides = SlidesApp.create(presentationTitle);
  var defaultSlides = slides.getSlides();
   defaultSlides.forEach(function(slide) {
   slide.remove();
  });

  values.forEach(function(page){
  if(page[0]){
    
   var landingPage = page[0];
   var sessions = page[1];
   var newSessions = page[2];
   var pagesPer = page[5];
   var goalRate = page[7];
   var goalValue = page[9];

    // Access the template presentation
   var template = SlidesApp.openById(templateId);
   var templateSlides = template.getSlides();

   // Insert slides from template
   var index = 0;
   templateSlides.forEach(function(slide) {
     var newSlide = slides.insertSlide(index);
     var elements = slide.getPageElements();
     elements.forEach(function(element) {
       newSlide.insertPageElement(element);
   });
   index++;
   });

   defaultSlides = slides.getSlides (); //update the slides array for indexes and length
   var newSlide = defaultSlides [1];
   var shapes = (newSlide.getShapes());
     shapes.forEach(function(shape){
       shape.getText().replaceAllText('{{landing page}}',landingPage);
       shape.getText().replaceAllText('{{sessions}}',sessions);
       shape.getText().replaceAllText('{{new sessions}}',newSessions);
       shape.getText().replaceAllText('{{pages per session}}',pagesPer);
       shape.getText().replaceAllText('{{goal rate}}',goalRate);
       shape.getText().replaceAllText('{{goal value}}',goalValue);
    }); 
   presLength = defaultSlides.length; 
   newSlide.move(presLength); 



  } // end our conditional statement
  }); 


Reply all
Reply to author
Forward
0 new messages