拡張スライド サービス

Slides API と Apps Script でプレゼンテーションを管理する

高度な Slides サービスを使用すると、Google Apps Script を使用して Slides APIにアクセスできます。このサービスを使用すると、スクリプトで Google スライドのコンテンツを読み取り、編集できます。

リファレンス

このサービスの詳細については、 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
});