7

The following is what I did:

  1. Created a new presentation on Google Slides,

  2. Edited one of the predefined layouts on the Master Layout view in order to have a new layout that I want to use,

  3. Edited the name of the Master Layout to "Meeting",

  4. Edited the name of the predefined layout that I want to use to "Office".

My problem is that on Google Script I am not being able to reference this specific predefined layout that I want to use.

So far, my code is the following:

function AddSlideToPresentatio() {

// The following line opens my presentation
var presentation = SlidesApp.openById('PresentationID');

//Now, I try to use my new layout
  presentation.appendSlide("Office");
}

I have absolutely no idea on why this doesn't work. When I try to run it, I get the error:

"Cannot find method appendSlide(string). (line 6, file "Office").

The following are some of the combinations that I tried, and they get me similar errors:

presentation.appendSlide('Office');
presentation.appendSlide(Office);
presentation.appendSlide("Meeting - Office");
presentation.appendSlide('Meeting - Office');
presentation.appendSlide(Meeting - Office);

If I just use presentation.appendSlide() it creates a new slide, but not with the layout that I want to use.

In the Google Apps Script Reference there are three methods:

  1. appendSlide(),
  2. appendSlide(layout),
  3. appendSlide(predefinedLayout)

However, I can't seem to understand what is the difference between that last two, because when I try to use them they seem to do the same thing.

Rafael Pinheiro
  • 393
  • 4
  • 16

2 Answers2

4

You are passing layout object's name for the appendSlide method, but you should pass LayoutObject parameter.

appendSlide(LayoutObject)

// The following line opens my presentation
var presentation = SlidesApp.openById('PresentationID');
// this will return an array of all the layouts
var layouts = presentation.getLayouts();

//if your first array item is the office layout
var newSlide = presentation.appendSlide(layouts[0]);

//Orelse you can search for your layout
var selectedLayout;
for(var item in layouts)
{
   //Logger.log(layouts[item].getLayoutName());
   if(layouts[item].getLayoutName() =='CUSTOM_1')
   {
     selectedLayout = layouts[item];
   }
}
var newSlide = presentation.appendSlide(selectedLayout);

PreDefinedLayout is an enum. It contains layouts that commonly found in presentations. Read all the available predefined layouts

use them as below;

presentation.appendSlide(SlidesApp.PredefinedLayout.SECTION_TITLE_AND_DESCRIPTION);
Kos
  • 4,890
  • 9
  • 38
  • 42
iJay
  • 4,205
  • 5
  • 35
  • 63
  • Thanks for the help! I would prefer to use the second option (searching for the name). I am having two problems with that one: 1) I changed the 'CUSTOM_1' to 'Office' but I get the following error on the _var newSlide = presentation.appendslide(selectedLayout);_ line: _Cannot find method appendSlide((class)). (line 23, file "Slides")_. 2) I used the Logger.log line to see what are the names of the slides' layouts. However, even though I changed the names of it, I am getting the names as "TITLE", "SECTION_HEADER", etc. Maybe I am not changing the names in the correct place? – Rafael Pinheiro Nov 20 '17 at 11:38
  • You might have edited an existing layout. which means it has a name already. Check the name again from your layout list view and compare it with names available in the log. I have not seen a way to find a layout by name we give. When we add new layout, it apear as Custom_1..Custom_2 etc – iJay Nov 20 '17 at 11:56
  • @RafaelPinheiro I asked it in [**here**](https://stackoverflow.com/questions/47392442/get-layouts-display-name-of-a-google-presentation-slide-via-google-apps-scripts). Hope some expert will help us. – iJay Nov 20 '17 at 12:53
  • You are correct, I actually changed some pre existing layout and tried to alter its name. Thank you for asking it in the other page, I will follow it there. The answer below by @Tanaike also seem to work, but I am still trying to understand what each step does in it. – Rafael Pinheiro Nov 20 '17 at 14:00
2

How about this answer? Please think of this as one of several answers.

When you want to correspond the layout displayname of "Office" and "Meeting" to the layout object, you can use following 2 patterns.

  1. Append layout using slide IDs.
  2. Append layout using slide objects.

In order to use these sample scripts, please enable Slides API at Advanced Google Services and API console.

Pattern 1 : Append layout using slide IDs

var displayName = "Office"; // Office or Meeting you gave

var presentationId = SlidesApp.getActivePresentation().getId();
var layouts = Slides.Presentations.get(presentationId).layouts;
var layout = {};
for (i in layouts) {
  layout[layouts[i].layoutProperties.displayName] = layouts[i].objectId;
}
var resource = {"requests": [{"createSlide": {"slideLayoutReference": {"layoutId": layout[displayName]}}}]};
Slides.Presentations.batchUpdate(resource, presentationId);

Pattern 2 : Append layout using slide objects

There are 2 names for the layout. They are "displayName" which can be seen at the browser and "name" which is retrieved by getLayoutName(), respectively. I thought that your layout names of "Office" and "Meeting" may be "displayName". I couldn't find "displayName" using SlidesApp. So I always retrieve "displayName" using Slides API as follows.

var displayName = "Office"; // Office or Meeting you gave

var s = SlidesApp.getActivePresentation();
var layouts = Slides.Presentations.get(s.getId()).layouts;
var layout = {};
for (i in layouts) {
  layout[layouts[i].layoutProperties.displayName] = layouts[i].layoutProperties.name;
}
var layoutObj = s.getLayouts().filter(function(e){return e.getLayoutName() == layout[displayName]})[0];
s.appendSlide(layoutObj);

References :

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

Edit :

  • Q1. About batchUpdate
    • A1. This is used to update slides using Slides API. You can see the detail information at here. Before the SlidesApp is published, users have used Slides API using Advanced Google Services. Even after SlidesApp was published, there are some works that only this can do. So I use this.
  • Q2. About var layout = {}
    • A2. This is used to import the dictionary data created at for loop.
  • Q3. About filter()
    • A3. About this, you can see the document at here. You can see the some sample scripts there.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks, Tanaike! Indeed, this works. I am still trying to understand what each line of your code does. In Pattern 1 I am having a bit of difficulty understanding the batchUpdate and the request (I read the references that you recommended and some other too). Besides, what type is the variable layout? I do not understand what it creates when you set it as _var layout = {}_. – Rafael Pinheiro Nov 20 '17 at 14:23
  • And on Pattern 2, I do not understand which function is the function(e). Could you please try to show me where it comes from? Is it somewhere else in the Slides API that I had to enable? – Rafael Pinheiro Nov 20 '17 at 14:29
  • 1
    @Rafael Pinheiro I'm sorry for the inconvenience. I updated my answer. Please confirm it. – Tanaike Nov 20 '17 at 22:25