PowerPoint.Slide class

表示演示文稿的单张幻灯片。

扩展

注解

API 集:PowerPointApi 1.2

使用方

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/get-set-shapes.yaml

// Reselects shapes that were saved previously.
await PowerPoint.run(async (context) => {
  const slide1: PowerPoint.Slide = context.presentation.slides.getItem(savedSlideSelection[0]);
  await context.sync();
  slide1.setSelectedShapes(savedShapeSelection);
  await context.sync();
});

属性

background

获取幻灯片的背景。

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

customXmlParts

返回幻灯片中自定义 XML 部分的集合。

hyperlinks

返回幻灯片中的超链接集合。

id

获取幻灯片的唯一 ID。

index

返回幻灯片的从零开始的索引,表示其在演示文稿中的位置。

layout

获取幻灯片的版式。

shapes

返回幻灯片中的形状集合。

slideMaster

获取 SlideMaster 表示幻灯片默认内容的对象。

tags

返回幻灯片中的标签集合。

themeColorScheme

返回幻灯片的值 ThemeColorScheme 。

方法

applyLayout(slideLayout)

将指定的布局应用于幻灯片,根据所选布局更改其设计和结构。

delete()

从演示文稿中删除幻灯片。 如果幻灯片不存在,则不执行任何操作。

exportAsBase64()

将幻灯片导出到其自己的演示文稿文件,并以 Base64 编码数据的形式返回。

注意:此方法已针对导出单张幻灯片进行了优化。 导出多张幻灯片可能会影响性能。

getImageAsBase64(options)

呈现幻灯片的图像。 图像被缩放以适应所需的尺寸。 如果未指定宽度和高度,将使用幻灯片的实际大小。 如果仅指定了宽度或高度中的一个,则将计算另一个以保持纵横比。

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

moveTo(slideIndex)

将幻灯片移动到演示文稿中的新位置。

setSelectedShapes(shapeIds)

选择指定的形状。 现有形状选择将替换为新的选择。

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 PowerPoint.Slide 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 PowerPoint.Interfaces.SlideData) ,其中包含从原始对象加载的任何子属性的浅层副本。

属性详细信息

background

获取幻灯片的背景。

readonly background: PowerPoint.SlideBackground;

属性值

注解

API 集:PowerPointApi 1.10

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/get-set-background-fill.yaml

// Gets the background fill type of the first selected slide.
await PowerPoint.run(async (context) => {
  const slide: PowerPoint.Slide = context.presentation.getSelectedSlides().getItemAt(0);
  const fill: PowerPoint.SlideBackgroundFill = slide.background.fill;
  fill.load("type");
  await context.sync();

  const fillType = fill.type as PowerPoint.SlideBackgroundFillType;
  console.log(`Background fill type: ${fillType}`);
});

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

customXmlParts

返回幻灯片中自定义 XML 部分的集合。

readonly customXmlParts: PowerPoint.CustomXmlPartCollection;

属性值

注解

API 集:PowerPointApi 1.7

返回幻灯片中的超链接集合。

readonly hyperlinks: PowerPoint.HyperlinkCollection;

属性值

注解

API 集:PowerPointApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/hyperlinks/manage-hyperlinks.yaml

// Sets a hyperlink on the first selected shape.
await PowerPoint.run(async (context) => {
  const slide: PowerPoint.Slide = context.presentation.getSelectedSlides().getItemAt(0);
  const shape: PowerPoint.Shape = context.presentation.getSelectedShapes().getItemAt(0).load("id,type");
  const hyperlinks: PowerPoint.HyperlinkCollection = slide.hyperlinks;
  const hyperlinkAddOptions: PowerPoint.HyperlinkAddOptions = {
    address: "https://bing.com",
    screenTip: "Screen tip for shape",
  };
  let hyperlink: PowerPoint.Hyperlink = hyperlinks.add(shape, hyperlinkAddOptions).load("address,screenTip");
  try {
    await context.sync();
  } catch {
    console.warn("Confirm that you have at least one slide and you've selected a shape on the active slide.");
    return;
  }

  console.log(
    `Added link "${hyperlink.address}" (screen tip: "${hyperlink.screenTip}") to the first selected shape (ID: ${shape.id}, type: ${shape.type}).`,
  );
});

id

获取幻灯片的唯一 ID。

readonly id: string;

属性值

string

注解

API 集:PowerPointApi 1.2

index

返回幻灯片的从零开始的索引,表示其在演示文稿中的位置。

readonly index: number;

属性值

number

注解

API 集:PowerPointApi 1.8

layout

获取幻灯片的版式。

readonly layout: PowerPoint.SlideLayout;

属性值

注解

API 集:PowerPointApi 1.3

shapes

返回幻灯片中的形状集合。

readonly shapes: PowerPoint.ShapeCollection;

属性值

注解

API 集:PowerPointApi 1.3

slideMaster

获取 SlideMaster 表示幻灯片默认内容的对象。

readonly slideMaster: PowerPoint.SlideMaster;

属性值

注解

API 集:PowerPointApi 1.3

tags

返回幻灯片中的标签集合。

readonly tags: PowerPoint.TagCollection;

属性值

注解

API 集:PowerPointApi 1.3

themeColorScheme

返回幻灯片的值 ThemeColorScheme 。

readonly themeColorScheme: PowerPoint.ThemeColorScheme;

属性值

注解

API 集:PowerPointApi 1.10

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/get-set-theme-color-scheme.yaml

// Gets the theme color scheme for the first slide and logs all theme color values.
await PowerPoint.run(async (context) => {
  const slide = context.presentation.slides.getItemAt(0);
  const colorScheme: PowerPoint.ThemeColorScheme = slide.themeColorScheme;

  // Queue up getThemeColor calls for each theme color.
  const colorResults = themeColorNames.map((colorName) => colorScheme.getThemeColor(colorName));
  await context.sync();

  // Display the results.
  console.log("Theme colors for slide 1:");
  themeColorNames.forEach((colorName, index) => {
    console.log(`  ${colorName}: ${colorResults[index].value}`);
  });
});

方法详细信息

applyLayout(slideLayout)

将指定的布局应用于幻灯片,根据所选布局更改其设计和结构。

applyLayout(slideLayout: PowerPoint.SlideLayout): void;

参数

slideLayout
PowerPoint.SlideLayout

要应用于幻灯片的版式。 这通常是幻灯片母版中预定义布局的实例。

返回

void

注解

API 集:PowerPointApi 1.8

delete()

从演示文稿中删除幻灯片。 如果幻灯片不存在,则不执行任何操作。

delete(): void;

返回

void

注解

API 集:PowerPointApi 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/get-set-slides.yaml

// Deletes the selected slides.
await PowerPoint.run(async (context) => {
  context.presentation.load("slides");
  await context.sync();
  const slides: PowerPoint.SlideScopedCollection = context.presentation.getSelectedSlides();
  const slideCount = slides.getCount();
  slides.load("items");
  await context.sync();
  slides.items.map((slide) => {
    slide.delete();
  });
});

exportAsBase64()

将幻灯片导出到其自己的演示文稿文件,并以 Base64 编码数据的形式返回。

注意:此方法已针对导出单张幻灯片进行了优化。 导出多张幻灯片可能会影响性能。

exportAsBase64(): OfficeExtension.ClientResult<string>;

返回

注解

API 集:PowerPointApi 1.8

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/export-import-slide.yaml

// Exports current slide.
await PowerPoint.run(async (context) => {
  const slide = context.presentation.getSelectedSlides().getItemAt(0);
  const slideBase64DataResult = slide.exportAsBase64();
  const imageBase64DataResult = slide.getImageAsBase64({ height: 300 });
  await context.sync();

  localStorage.setItem("exportedSlide", slideBase64DataResult.value);
  localStorage.setItem("exportedSlideImage", imageBase64DataResult.value);

  updateSlideImage(imageBase64DataResult.value);

  console.log("Slide was exported.");
});

getImageAsBase64(options)

呈现幻灯片的图像。 图像被缩放以适应所需的尺寸。 如果未指定宽度和高度,将使用幻灯片的实际大小。 如果仅指定了宽度或高度中的一个,则将计算另一个以保持纵横比。

getImageAsBase64(options?: PowerPoint.SlideGetImageOptions): OfficeExtension.ClientResult<string>;

参数

options
PowerPoint.SlideGetImageOptions

可选。 用于指定所需幻灯片图像大小的选项。

返回

PNG 格式幻灯片图像的 Base64 编码字符串。

注解

API 集:PowerPointApi 1.8

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/export-import-slide.yaml

// Exports current slide.
await PowerPoint.run(async (context) => {
  const slide = context.presentation.getSelectedSlides().getItemAt(0);
  const slideBase64DataResult = slide.exportAsBase64();
  const imageBase64DataResult = slide.getImageAsBase64({ height: 300 });
  await context.sync();

  localStorage.setItem("exportedSlide", slideBase64DataResult.value);
  localStorage.setItem("exportedSlideImage", imageBase64DataResult.value);

  updateSlideImage(imageBase64DataResult.value);

  console.log("Slide was exported.");
});

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(options?: PowerPoint.Interfaces.SlideLoadOptions): PowerPoint.Slide;

参数

options
PowerPoint.Interfaces.SlideLoadOptions

为要加载的对象属性提供选项。

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(propertyNames?: string | string[]): PowerPoint.Slide;

参数

propertyNames

string | string[]

指定要加载的属性的逗号分隔的字符串或字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): PowerPoint.Slide;

参数

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。

返回

moveTo(slideIndex)

将幻灯片移动到演示文稿中的新位置。

moveTo(slideIndex: number): void;

参数

slideIndex

number

滑动片应从零开始的索引。

返回

void

注解

API 集:PowerPointApi 1.8

setSelectedShapes(shapeIds)

选择指定的形状。 现有形状选择将替换为新的选择。

setSelectedShapes(shapeIds: string[]): void;

参数

shapeIds

string[]

要在幻灯片中选择的形状 ID 列表。 如果列表为空,则清除选择。

返回

void

注解

API 集:PowerPointApi 1.5

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/get-set-shapes.yaml

// Reselects shapes that were saved previously.
await PowerPoint.run(async (context) => {
  const slide1: PowerPoint.Slide = context.presentation.slides.getItem(savedSlideSelection[0]);
  await context.sync();
  slide1.setSelectedShapes(savedShapeSelection);
  await context.sync();
});

...

// Selects the first two shapes on slide 1.
await PowerPoint.run(async (context) => {
  context.presentation.load("slides");
  await context.sync();
  const slide1 = context.presentation.slides.getItemAt(0);
  slide1.load("shapes/items/type");
  await context.sync();

  const shapes = slide1.shapes.items.filter((item) => item.type === PowerPoint.ShapeType.geometricShape);
  const shape1: PowerPoint.Shape = shapes[0];
  const shape2: PowerPoint.Shape = shapes[1];
  shape1.load("id");
  shape2.load("id");
  await context.sync();

  console.log(`IDs: ${shape1.id}, ${shape2.id}`);
  slide1.setSelectedShapes([shape1.id, shape2.id]);
  await context.sync();
});

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 PowerPoint.Slide 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 PowerPoint.Interfaces.SlideData) ,其中包含从原始对象加载的任何子属性的浅层副本。

toJSON(): PowerPoint.Interfaces.SlideData;

返回