我找不到任何关于如何使用 Office 插件的 JS API 连接 PowerPoint 中的 2 个形状的参考(使用截至 03.2024 的最新版本)。
这是我的简单代码:
export async function run() {
await PowerPoint.run(async (context) => {
const shapes = context.presentation.slides.getItemAt(0).shapes; // Get the first slide
// Create rectangle 1
const rectangle1 = shapes.addGeometricShape(PowerPoint.GeometricShapeType.rectangle);
rectangle1.left = 100;
rectangle1.top = 50;
rectangle1.height = 150;
rectangle1.width = 50;
rectangle1.name = "Rectangle1";
// Create rectangle 2
const rectangle2 = shapes.addGeometricShape(PowerPoint.GeometricShapeType.rectangle);
rectangle2.left = 300;
rectangle2.top = 50;
rectangle2.height = 150;
rectangle2.width = 100;
rectangle2.name = "Rectangle2";
await context.sync();
});
}
如何用线连接它们?
使用 ShapeCollection.addLine 方法。这是一个例子:
// This function gets the collection of shapes on the first slide,
// and adds a line to the collection, while specifying its
// start and end points. Then it names the shape.
await PowerPoint.run(async (context) => {
const shapes = context.presentation.slides.getItemAt(0).shapes;
// For a line, left and top are the coordinates of the start point,
// while height and width are the coordinates of the end point.
const line = shapes.addLine(PowerPoint.ConnectorType.straight,
{
left: 400,
top: 200,
height: 20,
width: 150
});
line.name = "StraightLine";
await context.sync();
});