Dịch vụ Trang trình bày nâng cao

Dịch vụ Trang trình bày nâng cao cho phép bạn truy cập API Trang trình bày bằng Apps Script. Dịch vụ này cho phép tập lệnh đọc và chỉnh sửa nội dung trong Google Trang trình bày.

Tài liệu tham khảo

Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo về API Trang trình bày. Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Trang trình bày nâng cao sử dụng các đối tượng, phương thức và tham số giống như API công khai. Để biết thêm thông tin, hãy xem bài viết Cách xác định chữ ký phương thức.

Để báo cáo sự cố và tìm hỗ trợ khác, hãy xem Hướng dẫn hỗ trợ Trang trình bày.

Mã mẫu

Mã mẫu bên dưới sử dụng phiên bản 1 của API.

Tạo bản trình bày mới

Ví dụ sau đây minh hoạ cách tạo bản trình bày mới bằng Dịch vụ nâng cao của Trang trình bày. Nó tương đương với Tạo bản trình bày mới mẫu công thức nấu ăn.

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);
  }
}

Tạo trang trình bày mới

Ví dụ sau đây minh hoạ cách tạo một trang trình bày mới trong bản trình bày, theo chỉ mục cụ thể và có bố cục định sẵn. Nó tương đương với Tạo trang trình bày mới mẫu công thức nấu ăn.

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);
  }
}

Đọc ID đối tượng phần tử trang

Ví dụ sau minh hoạ cách truy xuất mã đối tượng cho mỗi trang trên một trang trình bày cụ thể bằng cách sử dụng mặt nạ trường. Nó tương đương với Đọc mã đối tượng phần tử trên một trang mẫu công thức nấu ăn.

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);
  }
}

Thêm hộp văn bản mới

Ví dụ sau đây minh hoạ cách thêm một hộp văn bản mới vào trang trình bày và thêm tin nhắn văn bản vào đó. Nó tương đương với Thêm hộp văn bản vào trang trình bày mẫu công thức nấu ăn.

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);
  }
}

Định dạng văn bản cho hình dạng

Ví dụ sau minh hoạ cách định dạng văn bản của một hình dạng, cập nhật văn bản màu sắc, phông chữ và gạch chân văn bản. Nó tương đương với Định dạng văn bản trong một hình dạng hoặc hộp văn bản mẫu công thức nấu ăn.

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);
  }
}

Các phương pháp hay nhất

Cập nhật theo lô

Khi sử dụng Dịch vụ nâng cao của Trang trình bày, hãy kết hợp nhiều yêu cầu trong một mảng thay vì gọi batchUpdate trong một vòng lặp.

Không nên — Gọi batchUpdate trong vòng lặp.

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

Nên — Gọi batchUpdate bằng một mảng bản cập nhật.

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
});