PowerPoint.ShapeFill class

表示形状对象的填充格式。

扩展

注解

API 集:PowerPointApi 1.4

使用方

示例

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

// Changes the transparency of every geometric shape in the slide.
await PowerPoint.run(async (context) => {
  // Get the type of shape for every shape in the collection.
  const shapes: PowerPoint.ShapeCollection = context.presentation.slides.getItemAt(0).shapes;
  shapes.load("type");
  await context.sync();

  // Change the shape transparency to be halfway transparent.
  shapes.items.forEach((shape) => {
    if (shape.type === PowerPoint.ShapeType.geometricShape) {
      shape.fill.transparency = 0.5;
    }
  });
  await context.sync();
});

属性

context

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

foregroundColor

以 HTML 颜色格式表示形状填充前景色,其形式 #RRGGBB (例如“FFA500”) 或命名 HTML 颜色 (例如“orange”) 。

transparency

指定填充的透明度百分比,其值从 0.0 (不透明) 到 1.0 (透明) 。 如果形状类型不支持透明度或形状填充具有不一致的透明度(例如渐变填充类型),则返回 null

type

返回形状的填充类型。 有关详细信息,请参阅 PowerPoint.ShapeFillType

方法

clear()

清除此形状的填充格式。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

setImage(base64EncodedImage)

将形状的填充格式设置为图像。 填充类型会更改为 PictureAndTexture

setSolidColor(color)

将形状的填充格式设置为统一颜色。 填充类型会更改为 Solid

toJSON()

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

属性详细信息

context

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

context: RequestContext;

属性值

foregroundColor

以 HTML 颜色格式表示形状填充前景色,其形式 #RRGGBB (例如“FFA500”) 或命名 HTML 颜色 (例如“orange”) 。

foregroundColor: string;

属性值

string

注解

API 集:PowerPointApi 1.4

示例

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

// Creates random shapes on the selected slide.
await PowerPoint.run(async (context) => {
  let finalTable = "";
  const currentSlide: PowerPoint.Slide = context.presentation.getSelectedSlides().getItemAt(0);
  const maxNewShapeWidth = 200;
  const maxNewShapeHeight = 200;
  const minNewShapeWidth = 50;
  const minNewShapeHeight = 50;
  for (let i = 0; i < 20; i++) {
    const rectangle: PowerPoint.Shape = currentSlide.shapes.addGeometricShape(
      PowerPoint.GeometricShapeType.rectangle,
    );
    rectangle.height = getRandomBetween(minNewShapeWidth, maxNewShapeWidth);
    rectangle.width = getRandomBetween(minNewShapeHeight, maxNewShapeHeight);
    rectangle.left = getRandomBetween(0, slideWidth - rectangle.width);
    rectangle.top = getRandomBetween(0, slideHeight - rectangle.height);
    rectangle.fill.foregroundColor = generateRandomHexColor();
  }
  finalTable += "Done<br>";
  const outputSpan = document.getElementById("outputSpan");
  outputSpan.innerHTML = "";
  outputSpan.innerHTML += finalTable;
});

transparency

指定填充的透明度百分比,其值从 0.0 (不透明) 到 1.0 (透明) 。 如果形状类型不支持透明度或形状填充具有不一致的透明度(例如渐变填充类型),则返回 null

transparency: number;

属性值

number

注解

API 集:PowerPointApi 1.4

示例

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

// Changes the transparency of every geometric shape in the slide.
await PowerPoint.run(async (context) => {
  // Get the type of shape for every shape in the collection.
  const shapes: PowerPoint.ShapeCollection = context.presentation.slides.getItemAt(0).shapes;
  shapes.load("type");
  await context.sync();

  // Change the shape transparency to be halfway transparent.
  shapes.items.forEach((shape) => {
    if (shape.type === PowerPoint.ShapeType.geometricShape) {
      shape.fill.transparency = 0.5;
    }
  });
  await context.sync();
});

type

返回形状的填充类型。 有关详细信息,请参阅 PowerPoint.ShapeFillType

readonly type: PowerPoint.ShapeFillType | "NoFill" | "Solid" | "Gradient" | "Pattern" | "PictureAndTexture" | "SlideBackground";

属性值

PowerPoint.ShapeFillType | "NoFill" | "Solid" | "Gradient" | "Pattern" | "PictureAndTexture" | "SlideBackground"

注解

API 集:PowerPointApi 1.4

示例

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

// Changes the selected shapes fill color to red.
await PowerPoint.run(async (context) => {
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  shapes.load("items/fill/type");
  await context.sync();
  shapes.items.map((shape) => {
    const shapeFillType = shape.fill.type as PowerPoint.ShapeFillType;
    console.log(`Shape ID ${shape.id} original fill type: ${shapeFillType}`);
    shape.fill.setSolidColor("red");
  });
  await context.sync();
});

方法详细信息

clear()

清除此形状的填充格式。

clear(): void;

返回

void

注解

API 集:PowerPointApi 1.4

load(options)

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

load(options?: PowerPoint.Interfaces.ShapeFillLoadOptions): PowerPoint.ShapeFill;

参数

options
PowerPoint.Interfaces.ShapeFillLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths

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

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

返回

setImage(base64EncodedImage)

将形状的填充格式设置为图像。 填充类型会更改为 PictureAndTexture

setImage(base64EncodedImage: string): void;

参数

base64EncodedImage

string

图像数据的 Base64 编码的字符串。

返回

void

注解

API 集:PowerPointApi 1.8

示例

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

// Inserts an image with binding.
await PowerPoint.run(async (context) => {
  const bindingId = (document.getElementById("temp-binding-id") as HTMLInputElement).value;
  const slide = context.presentation.getSelectedSlides().getItemAt(0);
  const myShape = slide.shapes.addGeometricShape(PowerPoint.GeometricShapeType.rectangle, {
    top: 100,
    left: 30,
    width: 200,
    height: 200
  });

  myShape.fill.setImage(flowerImage);
  context.presentation.bindings.add(myShape, PowerPoint.BindingType.shape, bindingId);
  await context.sync();

  const bindingsDropdown = document.getElementById("bindings-dropdown") as HTMLSelectElement;

  const option = new Option(`Binding ${bindingId}`, bindingId);

  // When a binding ID already exists, the binding is updated to refer to the new shape
  // so select the existing item rather than add a new one.
  const foundIndex = findDropdownItem(bindingsDropdown, option.text);
  if (foundIndex < 0) {
    bindingsDropdown.add(option);
    bindingsDropdown.selectedIndex = bindingsDropdown.options.length - 1;
  } else {
    bindingsDropdown.selectedIndex = foundIndex;
  }
});

setSolidColor(color)

将形状的填充格式设置为统一颜色。 填充类型会更改为 Solid

setSolidColor(color: string): void;

参数

color

string

一个字符串,它以 HTML 颜色格式指定填充颜色,格式 #RRGGBB (例如“FFA500”) 或命名 HTML 颜色 (例如“orange”) 。

返回

void

注解

API 集:PowerPointApi 1.4

示例

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

// Changes the selected shapes fill color to red.
await PowerPoint.run(async (context) => {
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  shapes.load("items/fill/type");
  await context.sync();
  shapes.items.map((shape) => {
    const shapeFillType = shape.fill.type as PowerPoint.ShapeFillType;
    console.log(`Shape ID ${shape.id} original fill type: ${shapeFillType}`);
    shape.fill.setSolidColor("red");
  });
  await context.sync();
});

toJSON()

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

toJSON(): PowerPoint.Interfaces.ShapeFillData;

返回