OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
679 stars 95 forks source link

.addLine function has unexpected results when height or width is nil in PowerPoint #4121

Open marek-hi opened 1 year ago

marek-hi commented 1 year ago

Hello,

I'm trying to make an add-in that makes legal entity structure charts. I'm struggling to get the "...addLine(PowerPoint.ConnectorType.XXXX, {..." to properly put the beginning and ending of the line in the correct place. I'm using the following format provided on the API docs:

// This sample creates a straight line on the first slide. await PowerPoint.run(async (context) => { const shapes = context.presentation.slides.getItemAt(0).shapes; const line = shapes.addLine(Excel.ConnectorType.straight, {left: 200, top: 50, height: 300, width: 150}); line.name = "StraightLine"; await context.sync(); });

(I think the reference to Excel is a typo, which I've changed to PowerPoint in my code). It seems to not accept width of zero (ie, it won't correctly display a perfectly vertical line, it inserts a line that is somewhat nonsensical). I've cludged this by ensuring that the height and width are never between -1 and 1. That seems to work but would be grateful if there is a more elegant solution to have perfectly vertical lines.

I'm coding in Visual Stuido 2022 17.5.1 for Windows, using the web version of PowerPoint Build 16.0.16306.40507 to develop/debug.

For additional context, it's also worth noting that I'm not very bright.

Kind Regards,

Marek


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

AlexJerabek commented 1 year ago

Hi @marek-hi,

Thank you for the question and reporting the sample issue. @alison-mk, could you please take a look?

alison-mk commented 1 year ago

Hi @marek-hi, thank you for submitting an issue. I've opened PR OfficeDev/office-js-docs-pr#3911 to resolve the typo in the code snippet. For your second concern, I'm researching how to add perfectly horizontal and vertical lines with the addLine API internally. I'll leave this issue open while I do that research and report back when I have more information.

Cheers, Alison

AlexJerabek commented 8 months ago

Looking back at this, this is a product bug. I'll transfer this to the product repo.

AlexJerabek commented 8 months ago

@marek-hi,

There is a workaround for this issue. If you change the height or width after creating the line, it'll make it straight.

  // 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: 0,
      top: 153,
      height: 0,
      width: 400
    });
    line.name = "StraightLine";
    line.height=0;

    await context.sync();
marek-hi commented 8 months ago

Thanks! I'll give that a try.

On Fri, 9 Feb 2024, 20:16 Alex Jerabek, @.***> wrote:

@marek-hi https://github.com/marek-hi,

There is a workaround for this issue. If you change the height or width after creating the line, it'll make it straight.

// 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: 0, top: 153, height: 0, width: 400 }); line.name = "StraightLine"; line.height=0;

await context.sync();

— Reply to this email directly, view it on GitHub https://github.com/OfficeDev/office-js/issues/4121#issuecomment-1936545647, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXNMO4W2X24XSYSM5WPSZTLYSZ7ZTAVCNFSM6AAAAABDB7RLNOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZWGU2DKNRUG4 . You are receiving this because you were mentioned.Message ID: @.***>

marek-hi commented 8 months ago

That worked!

Maybe this should be a different ticket, but when I generate the the shapes, they show wonky on the main screen but fine in the preview panel on the left. Refreshing the browser fixes it in the main screen.

display

AlexJerabek commented 8 months ago

Glad that workaround helped. For the other shape issue, yes, please create a new issue in this repo to track that separately.