如何在 Google 幻灯片上添加具有新布局的新幻灯片?

Raf*_*iro 7 javascript google-apps-script google-slides google-slides-api

以下是我所做的:

  1. 在 Google 幻灯片上创建了一个新的演示文稿,

  2. 编辑了主布局视图上的预定义布局之一,以便拥有我想要使用的新布局,

  3. 将主布局的名称编辑为“会议”,

  4. 编辑了我想用于“Office”的预定义布局的名称。

我的问题是,在 Google Script 上,我无法引用我想要使用的这个特定的预定义布局。

到目前为止,我的代码如下:

function AddSlideToPresentatio() {

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

//Now, I try to use my new layout
  presentation.appendSlide("Office");
}
Run Code Online (Sandbox Code Playgroud)

我完全不知道为什么这不起作用。当我尝试运行它时,出现错误:

“找不到方法 appendSlide(string)。(第 6 行,文件“Office”)。

以下是我尝试过的一些组合,它们给我带来了类似的错误:

presentation.appendSlide('Office');
presentation.appendSlide(Office);
presentation.appendSlide("Meeting - Office");
presentation.appendSlide('Meeting - Office');
presentation.appendSlide(Meeting - Office);
Run Code Online (Sandbox Code Playgroud)

如果我只是使用presentation.appendSlide()它会创建一个新幻灯片,但不会使用我想要使用的布局。

Google Apps Script Reference 中有三种方法:

  1. 追加幻灯片(),
  2. appendSlide(布局),
  3. appendSlide(预定义布局)

但是,我似乎无法理解最后两个之间的区别,因为当我尝试使用它们时,它们似乎在做同样的事情。

iJa*_*Jay 4

您正在为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);
Run Code Online (Sandbox Code Playgroud)

PreDefinedLayout是一个枚举。它包含演示文稿中常见的布局。读取所有可用的预定义布局

按如下方式使用它们;

presentation.appendSlide(SlidesApp.PredefinedLayout.SECTION_TITLE_AND_DESCRIPTION);
Run Code Online (Sandbox Code Playgroud)