Search code examples
google-apps-scriptgoogle-slides-api

Predefined layout not being used by advance slides api on slide create


This is my first almost successful addon. It is a standalone script. I have tested it on several slide decks created by others. In the latest test the Table of Contents which my code creates used a master/layout from the deck rather than the BLANK predefined one I called for in my code?

/**
    not using blank predefined blank master
    MINI TEST
 */
function miniTest()  {
  const pres = SlidesApp.getActivePresentation();
  const presId = pres.getId();
  let updtReqArr = [];
  // create request for a Slide to hold the table of contents
  let insertIdx = 0;
  pageId = miniPage(updtReqArr, insertIdx); 
  if (updtReqArr.length > 0) {
    response = Slides.Presentations.batchUpdate({ 'requests': updtReqArr }, presId);
  }
}

/**
 * create request for a Slide to hold the table of contents
 */
function miniPage(updtReqArr, insertIdx) {
  //  console.log('Begin createTocPage - insertIdx: ', insertIdx ); 
  pageId = Utilities.getUuid();
  //  console.log('pageId: ', pageId);

  //  base slide object request
  slideObj = [{
    'createSlide': {
      'objectId': pageId,
      'insertionIndex': insertIdx,
      'slideLayoutReference': {
        'predefinedLayout': 'BLANK'   // name of master
      }
    }
  }];
  //  console.log('slideObj: ', JSON.stringify(slideObj));
    updtReqArr.push(slideObj);
  //  console.log('updtReqArr.length: ', updtReqArr.length);
//  code that creates page elments, background, etc. removed for simplicity     
  return pageId;
}

The presentation upon which I encountered this problem was a copy of the GEG Eduprotocols presentation. This is the link to my copy. https://docs.google.com/presentation/d/1EEUDz0fBXnI4IBT8xlcKJrfPs_KMr5HIY2YfCjDLiuQ/edit?usp=sharing This is the publicly available source https://docs.google.com/presentation/d/1i5Hhod8ERu8dfmMk5f-pcuFGHZ6bc96JAe3Pca_rbxY/edit#slide=id.p

The creators used Slidesmania masters and those masters are showing up in the Table of Contents which I added even though I said use BLANK. What am I doing wrong?

                • Trying to use answer March 31, 2021 - - - - - - - - Still no luck using an existing BLANK layout in the slidedeck. Really confused by getMasters() and getLayouts(). The following two sets of code find the same 'objectId' which does not work in batch update.

      let layoutsArr = pres.getLayouts();

      let lArrSize = layoutsArr.length;

      for ( i = 0 ; i < lArrSize ; i++ )    {

        console.log('layoutsArr[i].getLayoutName(): ', layoutsArr[i].getLayoutName() );

        if ( layoutsArr[i].getLayoutName() === 'BLANK' )  {

          blankObjId = layoutsArr[i].getObjectId();
          blankIdx = i;

          console.log('layoutsArr[i] blankIdx: ', blankIdx );

          i = presSize + 1; 

        }

      }
      
      let mastersArr = pres.getMasters();

      let mArrSize = mastersArr.length;

      for ( i = 0 ; i < mArrSize ; i++ )    {

       console.log('i: ', i );

       layoutsArr = mastersArr[i].getLayouts();   

        let lArrSize = layoutsArr.length;

        for ( j = 0 ; j < lArrSize ; j++ )    {

          console.log('j: ', j, '  layoutsArr[j].getLayoutName(): ', layoutsArr[j].getLayoutName() );

          if ( layoutsArr[j].getLayoutName() === 'BLANK' )  {

            blankObjId = layoutsArr[j].getObjectId();

            console.log(' blankObjId: ',  blankObjId );

            blankIdx = j;

            console.log('layoutsArr[i] blankIdx: ', blankIdx );

            j = lArrSize + 1; 

            i = mArrSize + 1;

          }
        }
      }


      pageId = Utilities.getUuid();

      console.log('pageId: ', pageId);

      slideObj = [{

        'createSlide': {

          'objectId': pageId,

          'insertionIndex': insertIdx,

          'slideLayoutReference': {

            "layoutId": blankObjId   

          }
        }
      }];

The batch update code generated:

slideObj:  [{"createSlide":{"objectId":"c5f67b57-3318-4e4a-9aff-7d1a09f57464","insertionIndex":0,"slideLayoutReference":{"layoutId":"p12"}}}]
error
// Invalid requests[0].createSlide: The layout (p12) is not present in the current master (gc92c1a471f_0_19804).  

I had to add the extra lines as the code was shown as a mashed together paragraph???


Solution

  • Explanation:

    Since your slides have a custom template, the predefined layouts have been overwritten by the new template. You need to specify the object ID from the specific layout you want from the slide-mania master.

    Sample Code:

    /**
        not using blank predefined blank master
        MINI TEST
     */
    function miniTest()  {
      const pres = SlidesApp.getActivePresentation();
      const presId = pres.getId();
      const master = pres.getMasters()[1]; // slide-mania
      const layout = master.getLayouts()[16].getObjectId(); // blank
    
      let updtReqArr = [];
      // create request for a Slide to hold the table of contents
      let insertIdx = 0;
      pageId = miniPage(updtReqArr, insertIdx, layout); // blank
      if (updtReqArr.length > 0) {
        response = Slides.Presentations.batchUpdate({ 'requests': updtReqArr }, presId);
      }
    
    }
    
    /**
     * create request for a Slide to hold the table of contents
     */
    function miniPage(updtReqArr, insertIdx, layout) {
      //  console.log('Begin createTocPage - insertIdx: ', insertIdx ); 
      pageId = Utilities.getUuid();
      //  console.log('pageId: ', pageId);
    
      //  base slide object request
      slideObj = [{
        'createSlide': {
          'objectId': pageId,
          'insertionIndex': insertIdx,
          'slideLayoutReference': {
            'layoutId': layout   // object ID of layout
          }
        }
      }];
      //  console.log('slideObj: ', JSON.stringify(slideObj));
        updtReqArr.push(slideObj);
      //  console.log('updtReqArr.length: ', updtReqArr.length);
    //  code that creates page elments, background, etc. removed for simplicity     
      return pageId;
    }
    

    To change the created layout, please change the index of master.getLayouts() depending on the order of the layouts in Slide Mania master. You can view them using View menu -> Master.

    References:

    Predefined Layout