進階簡報服務

進階簡報服務可讓您使用 Apps Script 存取 Slides API。這項服務可讓指令碼讀取及編輯 Google 簡報中的內容。

參考資料

如要進一步瞭解這項服務,請參閱 Slide API 的參考說明文件。如同 Apps Script 的所有進階服務,進階簡報服務會使用與公用 API 相同的物件、方法和參數。詳情請參閱「如何判定方法簽章」一文。

如要回報問題及尋找其他支援,請參閱簡報支援指南

程式碼範例

下方程式碼範例使用第 1 版的 API。

建立新簡報

以下範例說明如何使用簡報進階服務來建立新的簡報。等同於「建立新簡報」的方案範例。

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

最佳做法

批次更新

使用投影片進階服務時,請在陣列中合併多個要求,而不是在迴圈中呼叫 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
});