PowerPoint.Presentation class

Presentation对象是具有一张或多张载有演示文稿内容的幻灯片的顶层对象。 若要了解有关 PowerPoint 对象模型的详细信息,请参阅 PowerPoint JavaScript 对象模型

扩展

注解

API 集:PowerPointApi 1.0

使用方

示例

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

await PowerPoint.run(async function(context) {
    // Get the ID of the first selected slide.
    const presentation: PowerPoint.Presentation = context.presentation;
    const selected: PowerPoint.Slide = presentation.getSelectedSlides().getItemAt(0);
    selected.load("id");
    await context.sync();

    // Insert the other presentation after the selected slide.
    const insertOptions: PowerPoint.InsertSlideOptions = {
        formatting: PowerPoint.InsertSlideFormatting.useDestinationTheme,
        targetSlideId: selected.id
    };
    presentation.insertSlidesFromBase64(chosenFileBase64, insertOptions);
    await context.sync();
});

属性

bindings

返回与演示文稿关联的绑定集合。

context

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

customXmlParts

返回与演示文稿关联的自定义 XML 部分的集合。

id

获取演示文稿的 ID。

pageSetup

返回页面设置信息,其属性控制演示文稿的幻灯片设置属性。

properties

获取演示文稿的属性。

sensitivityLabel

返回演示文稿的敏感度标签。

slideMasters

返回演示文稿中的对象集合 SlideMaster

slides

返回演示文稿中已排序的幻灯片集合。

tags

返回附加到演示文稿的标签集合。

title

返回演示文稿的标题。

方法

getActiveSlideOrNullObject()

返回在编辑区域中可见的当前活动 PowerPoint.Slide 。 如果没有活动幻灯片,则返回属性设置为 的true对象isNullObject。 有关详细信息,请参阅 *OrNullObject 方法和属性

getSelectedShapes()

返回演示文稿的当前幻灯片中的所选形状。 如果未选择任何形状,则返回空集合。

getSelectedSlides()

在演示文稿的当前视图中返回所选幻灯片。 集合中的第一项是在编辑区域中可见的活动幻灯片。 如果未选择任何幻灯片,则返回空集合。

getSelectedTextRange()

返回演示文稿当前视图中的所选 PowerPoint.TextRange 。 如果未选择任何文本,则引发异常。

getSelectedTextRangeOrNullObject()

返回演示文稿当前视图中的所选 PowerPoint.TextRange 。 如果未选择任何文本,则返回属性 isNullObject 设置为 的 true 对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

insertSlidesFromBase64(base64File, options)

将演示文稿中的指定幻灯片插入到当前演示文稿中。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

setSelectedSlides(slideIds)

在演示文稿的当前视图中选择幻灯片。 现有幻灯片所选内容将替换为新的所选内容。

toJSON()

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

活动

onSlideSelectionChanged

当演示文稿中的幻灯片选择更改时发生。 当用户选择其他幻灯片或更改幻灯片选择时,将引发此事件。 当幻灯片中) 形状等内容 (的选择更改时,不会引发此事件。

属性详细信息

bindings

返回与演示文稿关联的绑定集合。

readonly bindings: PowerPoint.BindingCollection;

属性值

注解

API 集:PowerPointApi 1.8

示例

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

// Loads bindings.
await PowerPoint.run(async (context) => {
  const bindings = context.presentation.bindings;
  bindings.load("items");
  await context.sync();

  const bindingCount = bindings.items.length;
  if (bindingCount === 0) {
    console.log(`There are no bindings.`);
  } else if (bindingCount === 1) {
    console.log("There's 1 binding.");
  } else {
    console.log(`There are ${bindingCount} bindings.`);
  }

  bindings.items.forEach((binding) => {
    getShapeForBindingId(binding.id).then((shape) => {
      if (shape) {
        console.log(`Binding ID: ${binding.id} refers to shape ID ${shape.id}`);
      } else {
        console.log(`Binding ID: ${binding.id} doesn't refers to shape.`);
      }
    });
  });

  populateBindingsDropdown(bindings.items);
});

context

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

context: RequestContext;

属性值

customXmlParts

返回与演示文稿关联的自定义 XML 部分的集合。

readonly customXmlParts: PowerPoint.CustomXmlPartCollection;

属性值

注解

API 集:PowerPointApi 1.7

id

获取演示文稿的 ID。

readonly id: string;

属性值

string

注解

API 集:PowerPointApi 1.5

pageSetup

返回页面设置信息,其属性控制演示文稿的幻灯片设置属性。

readonly pageSetup: PowerPoint.PageSetup;

属性值

注解

API 集:PowerPointApi 1.10

properties

获取演示文稿的属性。

readonly properties: PowerPoint.DocumentProperties;

属性值

注解

API 集:PowerPointApi 1.7

sensitivityLabel

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

返回演示文稿的敏感度标签。

readonly sensitivityLabel: PowerPoint.SensitivityLabel;

属性值

注解

API 集:PowerPointApi BETA (仅预览版)

slideMasters

返回演示文稿中的对象集合 SlideMaster

readonly slideMasters: PowerPoint.SlideMasterCollection;

属性值

注解

API 集:PowerPointApi 1.3

slides

返回演示文稿中已排序的幻灯片集合。

readonly slides: PowerPoint.SlideCollection;

属性值

注解

API 集:PowerPointApi 1.2

tags

返回附加到演示文稿的标签集合。

readonly tags: PowerPoint.TagCollection;

属性值

注解

API 集:PowerPointApi 1.3

title

返回演示文稿的标题。

readonly title: string;

属性值

string

注解

API 集:PowerPointApi 1.0

方法详细信息

getActiveSlideOrNullObject()

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

返回在编辑区域中可见的当前活动 PowerPoint.Slide 。 如果没有活动幻灯片,则返回属性设置为 的true对象isNullObject。 有关详细信息,请参阅 *OrNullObject 方法和属性

getActiveSlideOrNullObject(): PowerPoint.Slide;

返回

注解

API 集:PowerPointApi BETA (仅预览版)

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/presentation-get-active-slide.yaml

await PowerPoint.run(async (context) => {
  // Gets the active slide in the presentation.
  const slide: PowerPoint.Slide = context.presentation.getActiveSlideOrNullObject();
  slide.load("id");
  const activeSlideImage = slide.getImageAsBase64({ height: 200 });
  await context.sync();

  if (slide.isNullObject) {
    console.log("There are no slides in this presentation.");
    return;
  }

  console.log(`Active slide ID: ${slide.id}`);

  updateSlideImage(activeSlideImage.value);
});

getSelectedShapes()

返回演示文稿的当前幻灯片中的所选形状。 如果未选择任何形状,则返回空集合。

getSelectedShapes(): PowerPoint.ShapeScopedCollection;

返回

注解

API 集:PowerPointApi 1.5

示例

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

// Arranges the selected shapes in a line from left to right.
await PowerPoint.run(async (context) => {
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  shapes.load("items");
  await context.sync();
  let maxHeight = 0;
  shapes.items.map((shape) => {
    shape.load("width,height");
  });
  await context.sync();
  shapes.items.map((shape) => {
    shape.left = currentLeft;
    shape.top = currentTop;
    currentLeft += shape.width;
    if (shape.height > maxHeight) maxHeight = shape.height;
  });
  await context.sync();
  currentLeft = 0;
  if (currentTop > slideHeight - 200) currentTop = 0;
});

...

// Gets the shapes you selected on the slide and displays their IDs on the task pane.
await PowerPoint.run(async (context) => {
  let finalTable = "";
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  await context.sync();
  finalTable += "<br>getSelectedShapes.getCount returned:<b>" + shapeCount.value + "</b><br>";
  finalTable +=
    "<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  shapes.load("items");
  await context.sync();
  shapes.items.map((shape, index) => {
    finalTable += "<tr><td>" + index + "</td><td>" + shape.id + "</td></tr>";
  });
  finalTable += "</table>";
  const outputSpan = document.getElementById("outputSpan");
  outputSpan.innerHTML = "";
  outputSpan.innerHTML += finalTable;
});

...

// Saves which shapes are selected so that they can be reselected later.
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();
  savedSlideSelection = [];
  slides.items.map((slide) => {
    savedSlideSelection.push(slide.id);
  });
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  shapes.load("items");
  await context.sync();
  shapes.items.map((shape) => {
    savedShapeSelection.push(shape.id);
  });
});

getSelectedSlides()

在演示文稿的当前视图中返回所选幻灯片。 集合中的第一项是在编辑区域中可见的活动幻灯片。 如果未选择任何幻灯片,则返回空集合。

getSelectedSlides(): PowerPoint.SlideScopedCollection;

返回

注解

API 集:PowerPointApi 1.5

示例

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

// Gets the selected slides and displays their IDs on the task pane.
await PowerPoint.run(async (context) => {
  let finalTable = "";
  context.presentation.load("slides");
  await context.sync();
  const allSlidesList = {};
  const allSlidesCount = context.presentation.slides.getCount();
  context.presentation.slides.load("items");
  await context.sync();
  let allSlideItems: PowerPoint.Slide[] = context.presentation.slides.items;
  allSlideItems.map((slide, index) => {
    allSlidesList[slide.id] = `Slide ${index + 1}`;
  });

  const checkbox = document.getElementById("id-check-usenative") as HTMLInputElement;
  if (checkbox && checkbox.checked) {
    context.presentation.load("tags");
  }

  const slides: PowerPoint.SlideScopedCollection = context.presentation.getSelectedSlides();
  const slideCount = slides.getCount();
  slides.load("items");
  await context.sync();
  finalTable += "<br>getSelectedSlides.getCount returned:<b>" + slideCount.value + "</b><br>";
  finalTable +=
    "<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  slides.items.map((slide, index) => {
    finalTable += "<tr><td>" + index + " - " + allSlidesList[slide.id] + "</td><td>" + slide.id + "</td></tr>";
  });
  finalTable += "</table>";
  const outputSpan = document.getElementById("outputSpan");
  outputSpan.innerHTML = ""
  outputSpan.innerHTML += finalTable;
});

...

// Saves which slides are currently selected so they can be reselected later.
await PowerPoint.run(async (context) => {
  let finalTable = "";
  context.presentation.load("slides");
  await context.sync();
  const slides: PowerPoint.SlideScopedCollection = context.presentation.getSelectedSlides();
  const slideCount = slides.getCount();
  await context.sync();
  finalTable += "<br>getSelectedSlides.getCount returned:<b>" + slideCount.value + "</b><br>";
  finalTable +=
    "<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  savedSlideSelection = [];
  slides.load("items");
  await context.sync();
  slides.items.map((slide, index) => {
    finalTable += "<tr><td>" + index + "</td><td>" + slide.id + "</td></tr>";
    savedSlideSelection.push(slide.id);
  });
  finalTable += "</table>";
  const outputSpan = document.getElementById("outputSpan");
  outputSpan.innerHTML = ""
  outputSpan.innerHTML += finalTable;
});

getSelectedTextRange()

返回演示文稿当前视图中的所选 PowerPoint.TextRange 。 如果未选择任何文本,则引发异常。

getSelectedTextRange(): PowerPoint.TextRange;

返回

注解

API 集:PowerPointApi 1.5

示例

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

// Gets the selected text range and prints data about the range on the task pane.
await PowerPoint.run(async (context) => {
  const textRange: PowerPoint.TextRange = context.presentation.getSelectedTextRange();
  try {
    await context.sync();
  } catch (error) {
    console.warn("You must select only one range of text for this action to work.");
    return;
  }
  textRange.load("text");
  textRange.load("start");
  textRange.load("length");
  await context.sync();
  let txtHtml = textRange.text;
  txtHtml = txtHtml.replace(/\n/g, "<br>");
  txtHtml = txtHtml.replace(/\r/g, "<br>");
  txtHtml = txtHtml.replace(/\v/g, "<br>");
  let txtExplained = textRange.text;
  txtExplained = txtExplained.replace(/\n/g, "<font color=red>NL</font>");
  txtExplained = txtExplained.replace(/\r/g, "<font color=red>CR</font>");
  txtExplained = txtExplained.replace(/\v/g, "<font color=red>VV</font>");
  let finalTable = "";
  finalTable +=
    "<table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  finalTable += "<tr><td>Raw</td><td>" + textRange.text + "</td></tr>";
  finalTable += "<tr><td>Html</td><td>" + txtHtml + "</td></tr>";
  finalTable += "<tr><td>Exp</td><td>" + txtExplained + "</td></tr>";
  finalTable += "<tr><td>Start</td><td>" + textRange.start + "</td></tr>";
  finalTable += "<tr><td>Length</td><td>" + textRange.length + "</td></tr>";
  finalTable += "</table>";
  const outputSpan = document.getElementById("outputSpan");
  outputSpan.innerHTML = "";
  outputSpan.innerHTML += finalTable;
});

...

// Sets the range selection to the range that was saved previously.
await PowerPoint.run(async (context) => {
  const slide1: PowerPoint.Slide = context.presentation.slides.getItem(savedTextSlideSelection[0]);
  const shape1: PowerPoint.Shape = slide1.shapes.getItem(savedTextShapeSelection[0]);
  const textRange: PowerPoint.TextRange = shape1.textFrame.textRange.getSubstring(
    savedTextTextRangeStart,
    savedTextTextRangeLength,
  );
  textRange.setSelected();
  await context.sync();
});

getSelectedTextRangeOrNullObject()

返回演示文稿当前视图中的所选 PowerPoint.TextRange 。 如果未选择任何文本,则返回属性 isNullObject 设置为 的 true 对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getSelectedTextRangeOrNullObject(): PowerPoint.TextRange;

返回

注解

API 集:PowerPointApi 1.5

insertSlidesFromBase64(base64File, options)

将演示文稿中的指定幻灯片插入到当前演示文稿中。

insertSlidesFromBase64(base64File: string, options?: PowerPoint.InsertSlideOptions): void;

参数

base64File

string

表示源演示文稿文件的 Base64 编码字符串。

options
PowerPoint.InsertSlideOptions

用于定义将插入哪些幻灯片、新幻灯片将转到何处以及将使用哪种演示文稿的格式设置的选项。

返回

void

注解

API 集:PowerPointApi 1.2

示例

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

await PowerPoint.run(async function(context) {
    // Get the ID of the first selected slide.
    const presentation: PowerPoint.Presentation = context.presentation;
    const selected: PowerPoint.Slide = presentation.getSelectedSlides().getItemAt(0);
    selected.load("id");
    await context.sync();

    // Insert the other presentation after the selected slide.
    const insertOptions: PowerPoint.InsertSlideOptions = {
        formatting: PowerPoint.InsertSlideFormatting.useDestinationTheme,
        targetSlideId: selected.id
    };
    presentation.insertSlidesFromBase64(chosenFileBase64, insertOptions);
    await context.sync();
});

load(options)

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

load(options?: PowerPoint.Interfaces.PresentationLoadOptions): PowerPoint.Presentation;

参数

options
PowerPoint.Interfaces.PresentationLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths

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

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

返回

setSelectedSlides(slideIds)

在演示文稿的当前视图中选择幻灯片。 现有幻灯片所选内容将替换为新的所选内容。

setSelectedSlides(slideIds: string[]): void;

参数

slideIds

string[]

要在演示文稿中选择的幻灯片 ID 列表。 如果列表为空,则清除选择。

返回

void

注解

API 集:PowerPointApi 1.5

示例

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

// Sets selection to the slides that were saved.
await PowerPoint.run(async (context) => {
  context.presentation.setSelectedSlides(savedSlideSelection);
  await context.sync();
});

...

// Selects slides 2, 4, and 5.
await PowerPoint.run(async (context) => {
  context.presentation.load("slides");
  await context.sync();
  const slide2: PowerPoint.Slide = context.presentation.slides.getItemAt(1);
  const slide4: PowerPoint.Slide = context.presentation.slides.getItemAt(3);
  const slide5: PowerPoint.Slide = context.presentation.slides.getItemAt(4);
  slide2.load("id");
  slide4.load("id");
  slide5.load("id");
  try {
    await context.sync();
  } catch (error) {
    console.warn("This action requires at least 5 slides in the presentation.");
    return;
  }
  await context.sync();
  context.presentation.setSelectedSlides([slide2.id, slide4.id, slide5.id]);
  await context.sync();
});

toJSON()

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

toJSON(): PowerPoint.Interfaces.PresentationData;

返回

事件详细信息

onSlideSelectionChanged

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

当演示文稿中的幻灯片选择更改时发生。 当用户选择其他幻灯片或更改幻灯片选择时,将引发此事件。 当幻灯片中) 形状等内容 (的选择更改时,不会引发此事件。

readonly onSlideSelectionChanged: OfficeExtension.EventHandlers<PowerPoint.SlideSelectionChangedEventArgs>;

事件类型

注解

API 集:PowerPointApi BETA (仅预览版)