Advanced Slides Service

The Advanced Slides service lets you access the Slides API using Apps Script. This service allows scripts to read and edit content in Google Slides.

Reference

For detailed information on this service, see the reference documentation for the Slides API. Like all advanced services in Apps Script, the advanced Slides service uses the same objects, methods, and parameters as the public API.

To report issues and find other support, see the Slides support guide.

Sample code

The sample code below uses version 1 of the API.

Create a new presentation

The following example demonstrates how to create a new presentation using the Slides advanced service. It is equivalent to the Create a new presentation recipe sample.

advanced/slides.gs
/**
 * Create a new presentation.
 */
function createPresentation() {
  var presentation =
      Slides.Presentations.create({'title': 'MyNewPresentation'});
  Logger.log('Created presentation with ID: ' + presentation.presentationId);
}

Create a new slide

The following example demonstrates how to create a new slide in a presentation, at a specific index and with predefined layout. It is equivalent to the Create a new slide recipe sample.

advanced/slides.gs
/**
 * Create a new slide.
 * @param {string} presentationId The presentation to add the slide to.
 */
function createSlide(presentationId) {
  // You can specify the ID to use for the slide, as long as it's unique.
  var pageId = Utilities.getUuid();

  var requests = [{
    'createSlide': {
      'objectId': pageId,
      'insertionIndex': 1,
      'slideLayoutReference': {
        'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
      }
    }
  }];
  var slide =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
  Logger.log('Created Slide with ID: ' + slide.replies[0].createSlide.objectId);
}

Read page element object IDs

The following example demonstrates how to retrieve the object IDs for every page element on a specific slide using a field mask. It is equivalent to the Read element object IDs from a page recipe sample.

advanced/slides.gs
/**
 * Read page element IDs.
 * @param {string} presentationId The presentation to read from.
 * @param {string} pageId The page to read from.
 */
function readPageElementIds(presentationId, pageId) {
  // You can use a field mask to limit the data the API retrieves
  // in a get request, or what fields are updated in an batchUpdate.
  var response = Slides.Presentations.Pages.get(
      presentationId, pageId, {'fields': 'pageElements.objectId'});
  Logger.log(response);
}

Add a new text box

The following example demonstrates how to add a new text box to a slide and add text to it. It is equivalent to the Add a text box to a slide recipe sample.

advanced/slides.gs
/**
 * Add a new text box with text to a page.
 * @param {string} presentationId The presentation ID.
 * @param {string} pageId The page ID.
 */
function addTextBox(presentationId, pageId) {
  // You can specify the ID to use for elements you create,
  // as long as the ID is unique.
  var pageElementId = Utilities.getUuid();

  var requests = [{
    'createShape': {
      'objectId': pageElementId,
      'shapeType': 'TEXT_BOX',
      'elementProperties': {
        'pageObjectId': pageId,
        'size': {
          'width': {
            'magnitude': 150,
            'unit': 'PT'
          },
          'height': {
            'magnitude': 50,
            'unit': 'PT'
          }
        },
        'transform': {
          'scaleX': 1,
          'scaleY': 1,
          'translateX': 200,
          'translateY': 100,
          'unit': 'PT'
        }
      }
    }
  }, {
    'insertText': {
      'objectId': pageElementId,
      'text': 'My Added Text Box',
      'insertionIndex': 0
    }
  }];
  var response =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
  Logger.log('Created Textbox with ID: ' +
      response.replies[0].createShape.objectId);
}

Format shape text

The following example demonstrates how to format a shape's text, updating its color, font and underlining its text. It is equivalent to the Format text in a shape or textbox recipe sample.

advanced/slides.gs
/**
 * Format the text in a shape.
 * @param {string} presentationId The presentation ID.
 * @param {string} shapeId The shape ID.
 */
function formatShapeText(presentationId, shapeId) {
  var requests = [{
    'updateTextStyle': {
      'objectId': shapeId,
      'fields': 'foregroundColor,bold,italic,fontFamily,fontSize,underline',
      'style': {
        'foregroundColor': {
          'opaqueColor': {
            'themeColor': 'ACCENT5'
          }
        },
        'bold': true,
        'italic': true,
        'underline': true,
        'fontFamily': 'Corsiva',
        'fontSize': {
          'magnitude': 18,
          'unit': 'PT'
        }
      },
      'textRange': {
        'type': 'ALL'
      }
    }
  }];
  var response =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
}

Best Practices

Batch Updates

When using the Slides Advanced Service, combine multiple requests in an array rather than caling batchUpdate in a loop.

Don't — Call batchUpdate in a loop.

var titles = ["slide 1", "slide 2"];
for (var i = 0; i < titles.length; i++) {
  Slides.Presentation.batchUpdate(preso, {
    requests: [{
      createSlide: ...
    }]
  });
}

Do — Call batchUpdate with an array of updates.

var requests = [];
var titles = ["slide 1", "slide 2"];
for (var i = 0; i < titles.length; i++) {
  requests.push({ createSlide: ... });
}

Slides.Presentation.batchUpdate(preso, {
  requests: requests
});

フィードバックを送信...

ご不明な点がありましたら、Google のサポートページをご覧ください。