将图像插入现有的Google幻灯片模板

时间:2018-03-20 14:56:46

标签: google-apps-script

这是我目前的代码:

var NAME = "My favorite images";
var deck = SlidesApp.openById("1DKsiDGX9wwRbgP9WH2IJL2- 
    b1u1Q6jjz0JbrrKd_Fl4 ");

    /**
      * Creates a single slide using the image from the given link;
      * used directly by foreach(), hence the parameters are fixed.
      *
      * @param {Date} link A String object representing an image URL
      * @param {Date} index The index into the array; unused (req'd by 
        forEach)
      */
    function addImageSlide(imageUrl, index) {
      var slide = deck.appendSlide(SlidesApp.PredefinedLayout.BODY);
      var image = slide.insertImage(imageUrl);
      var imgWidth = image.getWidth();
      var imgHeight = image.getHeight();
      var pageWidth = deck.getPageWidth();
      var pageHeight = deck.getPageHeight();
      var newX = pageWidth / 2. - imgWidth / 2.;
      var newY = pageHeight / 2. - imgHeight / 2.;
      image.setLeft(newX).setTop(newY);
    }

    /**
      * The driver application features an array of image URLs, setting 
        of the
      * main title & subtitle, and creation of individual slides for each 
        image.
      */
    function main() {
      var images = [
        "http://www.google.com/services/images/phone-animation- 
        results_2x.png ",
        "http://www.google.com/services/images/section-work-card- 
        img_2x.jpg ",
        "http://gsuite.google.com/img/icons/product-lockup.png",
        "http://gsuite.google.com/img/home-hero_2x.jpg"
      ];
      var [title, subtitle] = deck.getSlides()[0].getPageElements();
      title.asShape().getText().setText(NAME);
      subtitle.asShape().getText().setText("");
      images.forEach(addImageSlide);
    }

我一直收到错误无法找到方法appendSlide((class))。 (第12行,文件“代码”) 我应该做些什么更改才能插入图片的网址,以便将它们插入到现有的Google幻灯片模板中?

1 个答案:

答案 0 :(得分:0)

根据文档here,没有名为SlidesApp.PredefinedLayout.BODY的预定义布局。因此你得到了那个错误。例如,如果将布局更改为空白,则此代码可以正常工作。

请修改此第12行,如下所示:

var slide = deck.appendSlide(SlidesApp.PredefinedLayout.BODY);

<击>

var slide = deck.appendSlide(SlidesApp.PredefinedLayout.BLANK);