6

I searched for a decent answer to this question but I couldn't come up with anything on SO so I thought I would post a new thread.

I am trying to copy a single slide from Google Slides into another using the advanced Slides service with google apps script.

function myFunction() {
  var originalPresentation = Slides.Presentations.get('1Lqtwb5z8NcU4VVj8OOR11AJyET70tlRRj6QIhxsEZZg');

  var slideToCopy = originalPresentation.slides[3];

  var newSlides = Slides.Presentations.create({
    title: 'New Slidedeck',
    slides: [slideToCopy]
  })
}

This creates the slide deck with the title, but the slide isn't copied. The documentation indicates that you can pass an array of slides with the title, but nothing is copying. Does it also need the masters and layouts? What am I doing wrong? Thanks in advance.

Jordan Rhea
  • 1,186
  • 15
  • 34

2 Answers2

12

Update at February 16, 2018

At February 13, 2018, the SlidesApp service was updated. Now, copying slides can be achieved by the native methods.

This sample script copies page 1 of srcPresentationId and inserts it as page 1 of the active presentation.

Sample script :

var srcPresentationId = "### source fileId ###";
var copysrcSlideIndex = 0; // 0 means page 1.

var copydstSlideIndex = 0; // 0 means page 1.

var src = SlidesApp.openById(srcPresentationId).getSlides()[copysrcSlideIndex];
SlidesApp.getActivePresentation().insertSlide(copydstSlideIndex, src);

Reference :

Original Answer

Do you still look for the method for copying slides? Unfortunately, I confirmed that copying using the Slides API cannot be done yet. But I thought of a workaround.

How about the following workaround? In a recent Google update, I noticed that Class SlidesApp was added. I used this. Since I didn't find the method for copying a slide directly to a new presentation, I used the following flow.

Flow :

  1. Copy the presentation using DriveApp.
  2. Open the copied presentation.
  3. Remove slides except for a slide you want to copy using remove().

Sample script :

function myFunction() {
  var srcSlides = 3; // A page number of slide that you want to copy. In this case, the top number is 1.

  var srcid = "1Lqtwb5z8NcU4VVj8OOR11AJyET70tlRRj6QIhxsEZZg";
  var dstid = DriveApp.getFileById(srcid).makeCopy().getId();
  var dstSlides = SlidesApp.openById(dstid).getSlides();
  dstSlides.splice(srcSlides - 1, 1);
  for (var i in dstSlides) {
    dstSlides[i].remove();
  }
}

References :

If this was not useful for you, I'm sorry.

Community
  • 1
  • 1
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for the workaround! I am going to mark this correct even though not the exact solution I was looking for. If you don't mind, star the feature request and hopefully they will add a method to make this possible. https://issuetracker.google.com/issues/68126064 – Jordan Rhea Nov 13 '17 at 07:20
  • @Jordan Rhea Thank you for quick reply. I also want the simple way like your script using Slides API. Now I checked the star. I want to use this workaround until the issue is solved. – Tanaike Nov 13 '17 at 07:28
  • @Jordan Rhea Thanks! – Tanaike Nov 13 '17 at 07:30
  • @Jordan Rhea I noticed that my added script copies only one element. So I modified it. I'm sorry for the inconvenience. – Tanaike Feb 19 '18 at 03:13
  • @Jordan Rhea I found a more simple method for copying slides. So I updated my answer. Please confirm it. – Tanaike Feb 26 '18 at 00:57
  • years later still no copy possible. I'm still really waiting for this as well, anybody found another solution ? I need to copy a slide in an existing presentation, not create a new one (I already use the copy_file from Google Drive api method, then remove slides, but that doesn't allow me to insert a slide into an already existing presentation :() – Alb Dum Apr 11 '20 at 11:30
3

Maybe you're looking for this ?

  var PresentationTEST = SlidesApp.openById(TEMPLATE_TEST);
  var PresentationTemplate = SlidesApp.openById(TEMPLATE_DEV);
  var slides = PresentationTemplate.getSlides();

  var slide = slides[0];
  var slide = PresentationTEST.appendSlide(slide);
ship
  • 31
  • 2
  • Tested this solution, works like a charm. Too bad we have to move to App Scripts and can't do it in the API, but at least it works. – Alb Dum Apr 14 '20 at 18:25
  • 1
    Edit a year later : performance is terrible, if you work with a lot of slides and want to do this, it ends up not working anymore. – Alb Dum Apr 23 '21 at 15:31