고급 프레젠테이션 서비스

고급 프레젠테이션 서비스를 사용하면 Apps Script를 통해 Slides API에 액세스할 수 있습니다. 이 서비스를 사용하면 스크립트가 Google Slides의 콘텐츠를 읽고 수정할 수 있습니다.

참조

이 서비스에 관한 자세한 내용은 Slides API의 참조 문서를 확인하세요. Apps Script의 모든 고급 서비스와 마찬가지로 고급 Slides 서비스는 공개 API와 동일한 객체, 메서드, 매개변수를 사용합니다. 자세한 내용은 메서드 서명 확인 방법을 참조하세요.

문제를 신고하고 기타 지원을 받으려면 Slides 지원 가이드를 참조하세요.

샘플 코드

아래 샘플 코드는 API의 버전 1을 사용합니다.

새 프레젠테이션을 만듭니다.

다음 예는 Slides 고급 서비스를 사용하여 새 프레젠테이션을 만드는 방법을 보여줍니다. 이는 새 프레젠테이션 만들기 레시피 샘플과 동일합니다.

advanced/slides.gs
/**
 * Create a new presentation.
 * @return {string} presentation Id.
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/create
 */
function createPresentation() {
  try {
    const presentation =
      Slides.Presentations.create({'title': 'MyNewPresentation'});
    console.log('Created presentation with ID: ' + presentation.presentationId);
    return presentation.presentationId;
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', e.message);
  }
}

새 슬라이드 만들기

다음 예에서는 프레젠테이션에서 특정 색인으로 사전 정의된 레이아웃을 사용하여 새 슬라이드를 만드는 방법을 보여줍니다. 이는 새 슬라이드 만들기 레시피 샘플과 동일합니다.

advanced/slides.gs
/**
 * Create a new slide.
 * @param {string} presentationId The presentation to add the slide to.
 * @return {Object} slide
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
 */
function createSlide(presentationId) {
  // You can specify the ID to use for the slide, as long as it's unique.
  const pageId = Utilities.getUuid();

  const requests = [{
    'createSlide': {
      'objectId': pageId,
      'insertionIndex': 1,
      'slideLayoutReference': {
        'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
      }
    }
  }];
  try {
    const slide =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    console.log('Created Slide with ID: ' + slide.replies[0].createSlide.objectId);
    return slide;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

페이지 요소 객체 ID 읽기

다음 예시에서는 필드 마스크를 사용하여 특정 슬라이드의 모든 페이지 요소의 객체 ID를 검색하는 방법을 보여줍니다. 이 코드는 페이지에서 요소 객체 ID 읽기 레시피 샘플과 동일합니다.

advanced/slides.gs
/**
 * Read page element IDs.
 * @param {string} presentationId The presentation to read from.
 * @param {string} pageId The page to read from.
 * @return {Object} response
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations.pages/get
 */
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.
  try {
    const response = Slides.Presentations.Pages.get(
        presentationId, pageId, {'fields': 'pageElements.objectId'});
    console.log(response);
    return response;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

새 텍스트 상자 추가

다음 예에서는 슬라이드에 새 텍스트 상자를 추가하고 텍스트를 추가하는 방법을 보여줍니다. 이는 슬라이드에 텍스트 상자 추가 레시피 샘플과 동일합니다.

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.
 * @return {Object} response
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
 */
function addTextBox(presentationId, pageId) {
  // You can specify the ID to use for elements you create,
  // as long as the ID is unique.
  const pageElementId = Utilities.getUuid();

  const 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
    }
  }];
  try {
    const response =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    console.log('Created Textbox with ID: ' +
      response.replies[0].createShape.objectId);
    return response;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

도형 텍스트 형식 설정

다음 예에서는 도형의 텍스트에 서식을 지정하고 색상과 글꼴을 업데이트하고 텍스트에 밑줄을 긋는 방법을 보여줍니다. 이는 도형 또는 텍스트 상자에서 텍스트 서식 지정 레시피 샘플과 동일합니다.

advanced/slides.gs
/**
 * Format the text in a shape.
 * @param {string} presentationId The presentation ID.
 * @param {string} shapeId The shape ID.
 * @return {Object} replies
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
 */
function formatShapeText(presentationId, shapeId) {
  const 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'
      }
    }
  }];
  try {
    const response =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    return response.replies;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

권장사항

일괄 업데이트

Slides 고급 서비스를 사용하는 경우 루프에서 batchUpdate를 호출하는 대신 여러 요청을 배열에 결합합니다.

금지: batchUpdate를 루프로 호출합니다.

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

실행 - 업데이트 배열과 함께 batchUpdate를 호출합니다.

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

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