고급 프레젠테이션 서비스

고급 Slides 서비스를 사용하면 다음을 사용하여 Slides API에 액세스할 수 있습니다. Apps Script 이 서비스를 사용하면 스크립트로 Google Slides의 콘텐츠를 읽고 수정할 수 있습니다.

참조

이 서비스에 대한 자세한 내용은 Slides API 참조 문서 Apps Script의 모든 고급 서비스와 마찬가지로 고급 프레젠테이션 서비스에서는 공개 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
});