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 vào 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 dành cho 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ùng đối tượng, phương thức và tham số như API công khai. Để biết thêm thông tin, hãy xem phần Cách xác định chữ ký phương thức.

Để báo cáo vấn đề và tìm nguồn hỗ trợ khác, hãy xem Hướng dẫn hỗ trợ của 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 minh hoạ cách tạo một bản trình bày mới bằng dịch vụ nâng cao của Trang trình bày. Công thức này tương đương với mẫu công thức Tạo bản trình bày mới.

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 minh hoạ cách tạo một trang trình bày mới trong bản trình bày, tại một chỉ mục cụ thể và với bố cục định sẵn. Công thức này tương đương với mẫu công thức Tạo trang trình bày mới.

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 phần tử trang trên một trang trình bày cụ thể bằng cách sử dụng mặt nạ trường (field mask). Giá trị này tương đương với mẫu công thức Đọc mã đối tượng của phần tử từ một trang.

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 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 văn bản vào đó. Cách này tương đương với mẫu công thức Thêm hộp văn bản vào trang trình bày.

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 màu sắc, phông chữ và gạch dưới văn bản đó. Tương đương với mẫu công thức Định dạng văn bản trong một hình dạng hoặc hộp văn bả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);
  }
}

Những phương pháp hay nhất

Cập nhật theo lô

Khi sử dụng Dịch vụ trang trình bày nâng cao, 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 một vòng lặp.

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

Do (Nên làm) – Gọi batchUpdate với một loạt các nội dung 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
});