PowerPoint.Presentation class

扩展

注解

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

属性

context

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

id

获取演示文稿的 ID。

slideMasters

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

slides

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

tags

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

title

方法

getSelectedShapes()

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

getSelectedSlides()

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

getSelectedTextRange()

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

getSelectedTextRangeOrNullObject()

返回演示文稿当前视图中选定的 PowerPoint.TextRange 。 如果未选择任何文本,则返回属性设置为 true 的对象isNullObject

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) ,其中包含原始对象中任何已加载子属性的浅表副本。

属性详细信息

context

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

context: RequestContext;

属性值

id

获取演示文稿的 ID。

readonly id: string;

属性值

string

注解

[ API 集:PowerPointApi 1.5 ]

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

方法详细信息

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>";
  $("#outputSpan").empty();
  $("#outputSpan").append(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}`;
  });

  if ($("#id-check-usenative").is(":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>";
  $("#outputSpan").empty();
  $("#outputSpan").append(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>";
  $("#outputSpan").empty();
  $("#outputSpan").append(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 +=
    "<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>";
  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>";
  $("#outputSpan").empty();
  $("#outputSpan").append(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 。 如果未选择任何文本,则返回属性设置为 true 的对象isNullObject

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;

返回