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
678 stars 95 forks source link

[PowerPoint] toJSON returns an empty object even though a property is loaded. #4775

Closed tsuga closed 1 month ago

tsuga commented 2 months ago

Your Environment

Expected behavior

toJSON should return a js object with properties loaded.

Current behavior

toJSON returns an empty object even though a property (i.e. id in the code below) is loaded.

Steps to reproduce

  1. Use Script Lab and execute the following code.
    
    // select a shape and click 'Run'
    $("#run").on("click", run);

function run() { PowerPoint.run(async (context) => { const shapes = context.presentation.getSelectedShapes(); shapes.load("items") await context.sync(); const items = shapes.items; items.forEach((shape) => shape.load('id')); await context.sync(); console.log(items[0].id); // 2 - the id property is loaded. console.log(JSON.stringify(items)); // expected: [{id: 2}] current: [{}] }); }

EsterBergen commented 2 months ago

@tsuga - This looks to be a bug. We are investigating and will create an item in our backlog. We'll provide updates here once available. Thanks!

tsuga commented 2 months ago

@EsterBergen Thank you for picking this up. Is there any update?

tsuga commented 1 month ago

@EsterBergen Is there any update?

EsterBergen commented 1 month ago

HI @tsuga - We don't have an update to share at this moment and will let you know here when more details are available. Thanks for your patience!

EsterBergen commented 1 month ago

@tsuga - Thanks again for reaching out, This is actually by design, @gergzk can provide any follow-up to questions you may have here.

tsuga commented 1 month ago

@EsterBergen Thank you for the update, but I'm afraid I don't get it. If this is by design, how can I use PowerPoint.ShapeScopedCollection.toJSON?

gergzk commented 1 month ago

@tsuga - the intent of the toJSON implementation is to override the JSON.stringify behavior to avoid throwing. The objects (eg: ShapeScopedCollection) have circular references and other non-stringifiy'able content.

tsuga commented 1 month ago

@gergzk Thank you for clarifying, but what you explained to me here does not align with what is written on the docs.

https://learn.microsoft.com/en-us/javascript/api/powerpoint/powerpoint.shapescopedcollection?view=powerpoint-js-1.5

toJSON() Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original PowerPoint.ShapeScopedCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as PowerPoint.Interfaces.ShapeScopedCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.