使用 Office.Js API 在 PowerPoint 中绘制对角线

问题描述 投票:0回答:1

希望你一切都好。我在 Office.JS API for PowerPoint 中遇到一个奇怪的问题。我的目的很简单,我想画一条对角线(正面向上),但我不能。在下图中,您可以看到它的顶线,并且在 Shape 文档中,我也找不到任何内容。

我无法在 PowerPoint 中使用 Office.JS API 绘制这条线

This is the result which I want to achieve

我摆弄了下面的代码,但仍然没有成功。非常感谢任何帮助。

  // 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: PowerPoint.ShapeCollection = 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: PowerPoint.Shape = shapes.addLine(PowerPoint.ConnectorType.straight, {
      left: 400,
      top: 200,
      height: 20,
      width: 150
    });
    line.name = "StraightLine";

    await context.sync();
  });
}```



javascript office365 powerpoint office-js office-addins
1个回答
0
投票

您提供的代码在指定线条坐标的方式上存在逻辑错误。在PowerPoint中,添加线条时,left和top属性指定起点,但width和height属性不直接指定终点。相反,它们指定从起点到终点的水平和垂直距离。

尝试简单地切换最后两行

// For a line, left and top are the coordinates of the start point,
// while width and height are the horizontal and vertical distances
// from the start point to the end point.
const line = shapes.addLine(PowerPoint.ConnectorType.straight, {
    left: 400,
    top: 200,
    width: 150,  // Horizontal distance to the end point
    height: 20  // Vertical distance to the end point
});
© www.soinside.com 2019 - 2024. All rights reserved.