3

(Sorry for my english)

I have a problem when using Google Slides API. How can I choose a theme when create new presentation with Google Slides API?

Thanks.

enter image description here


1. create a presentation

    function createPresentation($title) {
        $presentation = new Google_Service_Slides_Presentation(array(
            'title' => $title
        ));
        $presentation = $this->slidesService->presentations->create($presentation);
        $presentationLastId = $presentation->presentationId;

        return $presentationLastId;
    }

2. append a slide

    protected function createSlideRequest($presentationId, $slideId, $data = array())
    {
        $requests = array();
        $requests[] = new Google_Service_Slides_Request(array(
            'createSlide' => array (
                'objectId' => $slideId,
                'slideLayoutReference' => array ('predefinedLayout' => 'BLANK')
             ),
        ));

        $batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
            'requests' => $requests
        ));

        $response = $this->slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);

        return $response;
    }
  • The result like image below after call two requests: enter image description here

  • But i want create a presentation like: enter image description here

T.Thuận
  • 133
  • 7
  • I suggest looking at the documentation for [presentations.create](https://developers.google.com/slides/reference/rest/v1/presentations/create) where you can specify `masters`. Please post your [minimal code example](https://stackoverflow.com/help/mcve) of what you have tried – Kos Oct 18 '18 at 09:40
  • i updated source code, Could you help me ? – T.Thuận Oct 26 '18 at 03:44
  • appendSlide(layout) appendSlide(predefinedLayout) – aNewb Aug 21 '21 at 00:19

1 Answers1

-1

You can create a template presentation with the desired theme, then import a slide from it to the current presentation. You will have 2 masters in the current presentation. Then remove the initial master

function applyTheme() {
  var templateId = your_templateId_here;
  var currentId = your_currentId_here;
  var templatePres = SlidesApp.openById(templateId);
  var currentPres = SlidesApp.openById(currentId);
  var templateSlide = templatePres.getSlides()[0];
  currentPres.appendSlide(templateSlide);
  currentPres.getMasters()[0].remove();
}

https://developers.google.com/apps-script/reference/slides/master

vstepaniuk
  • 667
  • 6
  • 14