Получить эффективные свойства фигур из презентаций на JavaScript
Обзор
This topic explains the difference between local and effective properties. Local values are values that are set directly at a specific formatting level, such as:
- Portion properties on a slide.
- Prototype shape text styles on a layout or master slide, when the portion’s text frame shape has one.
- Global text settings in a presentation.
Local values can be defined or omitted at any level. When Aspose.Slides needs the final “as rendered” formatting, it resolves the inheritance chain and returns effective values. You can get them by calling the getEffective method on the local format object.
The following example shows how to get effective values. It assumes that the first shape on the first slide is an AutoShape with a text frame and at least one portion.
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let shape = slide.getShapes().get_Item(0);
let localTextFrameFormat = shape.getTextFrame().getTextFrameFormat();
let effectiveTextFrameFormat = localTextFrameFormat.getEffective();
let paragraph = shape.getTextFrame().getParagraphs().get_Item(0);
let localPortionFormat = paragraph.getPortions().get_Item(0).getPortionFormat();
let effectivePortionFormat = localPortionFormat.getEffective();
} finally {
presentation.dispose();
}
getEffective again after changing parent or inherited formatting can refresh the cached data, and a previously obtained object may no longer represent the earlier state. If you need to preserve effective values for later reuse, copy the required properties, such as font height, fill color, font style, or alignment, into your own data object.
Получить эффективные свойства камеры
Aspose.Slides позволяет получить эффективные свойства камеры. Объект данных эффективной камеры содержит неизменяемые свойства камеры и доступен через эффективные значения, возвращаемые для ThreeDFormat.
The following code sample shows how to get effective properties for the camera. It assumes that the first shape on the first slide has 3D formatting.
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let shape = slide.getShapes().get_Item(0);
let threeDEffectiveData = shape.getThreeDFormat().getEffective();
let camera = threeDEffectiveData.getCamera();
let cameraType = camera.getCameraType();
let fieldOfViewAngle = camera.getFieldOfViewAngle();
let zoom = camera.getZoom();
console.log("= Effective camera properties =");
console.log("Type: " + cameraType);
console.log("Field of view: " + fieldOfViewAngle);
console.log("Zoom: " + zoom);
} finally {
presentation.dispose();
}
Получить эффективные свойства осветительного устройства
Aspose.Slides позволяет получить эффективные свойства осветительного устройства. Объект данных эффективного осветительного устройства содержит неизменяемые свойства осветительного устройства и доступен через эффективные значения, возвращаемые для ThreeDFormat.
The following code sample shows how to get effective properties for the light rig. It assumes that the first shape on the first slide has 3D formatting.
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let shape = slide.getShapes().get_Item(0);
let threeDEffectiveData = shape.getThreeDFormat().getEffective();
let lightRig = threeDEffectiveData.getLightRig();
let lightType = lightRig.getLightType();
let direction = lightRig.getDirection();
console.log("= Effective light rig properties =");
console.log("Type: " + lightType);
console.log("Direction: " + direction);
} finally {
presentation.dispose();
}
Получить эффективные свойства скоса формы
Aspose.Slides позволяет получить эффективные свойства скоса формы. Объект данных эффективного скоса формы содержит неизменяемые свойства рельефа грани формы и доступен через эффективные значения, возвращаемые для ThreeDFormat.
The following code sample shows how to get effective properties for the top bevel of a shape. It assumes that the first shape on the first slide has 3D formatting.
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let shape = slide.getShapes().get_Item(0);
let threeDEffectiveData = shape.getThreeDFormat().getEffective();
let bevelTop = threeDEffectiveData.getBevelTop();
let bevelType = bevelTop.getBevelType();
let bevelWidth = bevelTop.getWidth();
let bevelHeight = bevelTop.getHeight();
console.log("= Effective shape's top face relief properties =");
console.log("Type: " + bevelType);
console.log("Width: " + bevelWidth);
console.log("Height: " + bevelHeight);
} finally {
presentation.dispose();
}
Получить эффективные свойства текстового кадра
Using Aspose.Slides, you can get effective properties of a text frame. The returned effective data object contains text frame formatting properties.
The following code sample shows how to get effective text frame formatting properties. It assumes that the first shape on the first slide is an AutoShape with a text frame.
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let shape = slide.getShapes().get_Item(0);
let textFrameFormat = shape.getTextFrame().getTextFrameFormat();
let effectiveTextFrameFormat = textFrameFormat.getEffective();
let anchoringType = effectiveTextFrameFormat.getAnchoringType();
let autofitType = effectiveTextFrameFormat.getAutofitType();
let textVerticalType = effectiveTextFrameFormat.getTextVerticalType();
let marginLeft = effectiveTextFrameFormat.getMarginLeft();
let marginTop = effectiveTextFrameFormat.getMarginTop();
let marginRight = effectiveTextFrameFormat.getMarginRight();
let marginBottom = effectiveTextFrameFormat.getMarginBottom();
console.log("Anchoring type: " + anchoringType);
console.log("Autofit type: " + autofitType);
console.log("Text vertical type: " + textVerticalType);
console.log("Margins");
console.log(" Left: " + marginLeft);
console.log(" Top: " + marginTop);
console.log(" Right: " + marginRight);
console.log(" Bottom: " + marginBottom);
} finally {
presentation.dispose();
}
Получить эффективные свойства стиля текста
Using Aspose.Slides, you can get effective properties of a text style. The returned effective data object contains text style properties.
The following code sample shows how to get effective text style properties. It assumes that the first shape on the first slide is an AutoShape with a text frame.
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let shape = slide.getShapes().get_Item(0);
let effectiveTextStyle = shape.getTextFrame().getTextFrameFormat().getTextStyle().getEffective();
let levelCount = 9;
for (let levelIndex = 0; levelIndex < levelCount; levelIndex++) {
let effectiveStyleLevel = effectiveTextStyle.getLevel(levelIndex);
let depth = effectiveStyleLevel.getDepth();
let indent = effectiveStyleLevel.getIndent();
let alignment = effectiveStyleLevel.getAlignment();
let fontAlignment = effectiveStyleLevel.getFontAlignment();
console.log("= Effective paragraph formatting for style level #" + levelIndex + " =");
console.log("Depth: " + depth);
console.log("Indent: " + indent);
console.log("Alignment: " + alignment);
console.log("Font alignment: " + fontAlignment);
}
} finally {
presentation.dispose();
}
Получить эффективное значение высоты шрифта
Using Aspose.Slides, you can get the effective font height. The following code demonstrates how a portion’s effective font height changes after local font height values are set at different presentation structure levels.
let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let shapeType = aspose.slides.ShapeType.Rectangle;
let autoShape = slide.getShapes().addAutoShape(shapeType, 100, 100, 400, 75, false);
autoShape.addTextFrame("");
let paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
let firstPortion = new aspose.slides.Portion("Sample text with first portion");
let secondPortion = new aspose.slides.Portion(" and second portion.");
paragraph.getPortions().add(firstPortion);
paragraph.getPortions().add(secondPortion);
let firstPortionFormatEffectiveData = firstPortion.getPortionFormat().getEffective();
let secondPortionFormatEffectiveData = secondPortion.getPortionFormat().getEffective();
let firstPortionFontHeight = firstPortionFormatEffectiveData.getFontHeight();
let secondPortionFontHeight = secondPortionFormatEffectiveData.getFontHeight();
console.log("Effective font height just after creation:");
console.log("Portion #0: " + firstPortionFontHeight);
console.log("Portion #1: " + secondPortionFontHeight);
presentation.getDefaultTextStyle().getLevel(0).getDefaultPortionFormat().setFontHeight(24);
firstPortionFormatEffectiveData = firstPortion.getPortionFormat().getEffective();
secondPortionFormatEffectiveData = secondPortion.getPortionFormat().getEffective();
firstPortionFontHeight = firstPortionFormatEffectiveData.getFontHeight();
secondPortionFontHeight = secondPortionFormatEffectiveData.getFontHeight();
console.log("Effective font height after setting the presentation default font height:");
console.log("Portion #0: " + firstPortionFontHeight);
console.log("Portion #1: " + secondPortionFontHeight);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(40);
firstPortionFormatEffectiveData = firstPortion.getPortionFormat().getEffective();
secondPortionFormatEffectiveData = secondPortion.getPortionFormat().getEffective();
firstPortionFontHeight = firstPortionFormatEffectiveData.getFontHeight();
secondPortionFontHeight = secondPortionFormatEffectiveData.getFontHeight();
console.log("Effective font height after setting paragraph default font height:");
console.log("Portion #0: " + firstPortionFontHeight);
console.log("Portion #1: " + secondPortionFontHeight);
firstPortion.getPortionFormat().setFontHeight(55);
firstPortionFormatEffectiveData = firstPortion.getPortionFormat().getEffective();
secondPortionFormatEffectiveData = secondPortion.getPortionFormat().getEffective();
firstPortionFontHeight = firstPortionFormatEffectiveData.getFontHeight();
secondPortionFontHeight = secondPortionFormatEffectiveData.getFontHeight();
console.log("Effective font height after setting portion #0 font height:");
console.log("Portion #0: " + firstPortionFontHeight);
console.log("Portion #1: " + secondPortionFontHeight);
secondPortion.getPortionFormat().setFontHeight(18);
firstPortionFormatEffectiveData = firstPortion.getPortionFormat().getEffective();
secondPortionFormatEffectiveData = secondPortion.getPortionFormat().getEffective();
firstPortionFontHeight = firstPortionFormatEffectiveData.getFontHeight();
secondPortionFontHeight = secondPortionFormatEffectiveData.getFontHeight();
console.log("Effective font height after setting portion #1 font height:");
console.log("Portion #0: " + firstPortionFontHeight);
console.log("Portion #1: " + secondPortionFontHeight);
let saveFormat = aspose.slides.SaveFormat.Pptx;
presentation.save("SetLocalFontHeightValues.pptx", saveFormat);
} finally {
presentation.dispose();
}
Получить эффективный формат заполнения для таблицы
Using Aspose.Slides, you can get effective fill formatting for different table parts. The returned effective data object contains fill formatting properties. Cell formatting has higher priority than row formatting, row formatting has higher priority than column formatting, and column formatting has higher priority than whole-table formatting.
As a result, effective cell formatting properties are used to draw the table cell. The following code sample shows how to get effective fill formatting for different table parts. It assumes that the first shape on the first slide is a Table.
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let table = slide.getShapes().get_Item(0);
let tableFormatEffective = table.getTableFormat().getEffective();
let rowFormatEffective = table.getRows().get_Item(0).getRowFormat().getEffective();
let columnFormatEffective = table.getColumns().get_Item(0).getColumnFormat().getEffective();
let cellFormatEffective = table.get_Item(0, 0).getCellFormat().getEffective();
let tableFillFormatEffective = tableFormatEffective.getFillFormat();
let rowFillFormatEffective = rowFormatEffective.getFillFormat();
let columnFillFormatEffective = columnFormatEffective.getFillFormat();
let cellFillFormatEffective = cellFormatEffective.getFillFormat();
} finally {
presentation.dispose();
}
FAQ
Возвращает ли getEffective снимок?
Not always. Effective data represents the calculated formatting after inheritance is applied, but some effective data objects can be cached internally. A subsequent getEffective call may recalculate formatting and refresh the cached data, so a previously obtained object should not be treated as a durable snapshot.
Когда мне следует снова прочитать эффективные свойства?
Call getEffective again after changing local formatting, parent styles, layout formatting, master formatting, or presentation-level defaults. The next call re-evaluates the formatting hierarchy and returns the current effective result.
Влияет ли изменение или удаление макета/мастер‑слайда на уже полученные эффективные свойства?
Yes, but the change is reflected on the next getEffective call. If a parent formatting source is changed or removed, previously obtained effective data may be stale. Once getEffective is called again, Aspose.Slides re-evaluates the formatting tree and the resulting fonts, colors, sizes, or other values may change.
Могу ли я изменять значения через объекты эффективных данных?
No. Effective data objects expose calculated values. Make changes in the local formatting objects, and then obtain the effective values again.
Что происходит, если свойство не задано на уровне фигуры, макета/мастера и глобальных настроек?
The effective value is determined by the default mechanism, which includes PowerPoint and Aspose.Slides defaults. That resolved value becomes part of the current effective data.
Можно ли по эффективному значению шрифта определить, с какого уровня пришли размер или гарнитура?
Not directly. Effective data returns the final value. To find the source, check local values at the portion, paragraph, text frame, and text styles at the layout, master, and presentation levels to see where the first explicit definition appears.
Почему иногда эффективные значения совпадают с локальными?
Because the local value ended up being final (no higher-level inheritance was needed). In such cases, the effective value matches the local one.
Когда следует использовать эффективные свойства, а когда работать только с локальными?
Use effective data when you need the “as rendered” result after all inheritance is applied, such as to align colors, indents, or sizes. If you need to preserve those values regardless of later formatting changes, copy the required properties into your own object. If you need to change formatting at a specific level, modify local properties and then, if needed, read the effective data again to verify the outcome.