Visio JavaScript API - 无法读取/提取形状数据



我无法检索基本的形状信息,最重要的是。getbounds()。有了这个,我可以知道(X, Y)的位置以及形状的大小。

我现在能检索到的只有形状的名称和文本。

我正在使用这个javascript Visio API。https://learn.microsoft.com/en - us/javascript/api/visio?view=visio - js - 1.1

这是我到目前为止的代码。

Visio.run(this.state.session, function (context) {
var page = context.document.getActivePage();
var shapes = page.shapes;
shapes.load(); // This loads the shapes.
// need to call context.sync() in order to actually read the shapes?
return context.sync().then(function () {
for (var i = 0; i < shapes.items.length; i++) {
var shape = shapes.items[i];
myShapes.push(shape);
// At this point i know the shape's Name. 
// but its .getBounds() is failing.
}
});

我如何获得/调用visio Shape的。getbounds()方法?

我看到的错误(当我尝试执行shape.getBounds())是这样的:

RichApi.Error: The value of the result object has not been loaded yet. Before reading the value property, call "context.sync()" on the associated request context.

我找到了解决方案。一开始并不明显,直到我读了这篇文章。但本质上,我调用的任何方法都会返回一个visio代理的新实例。由于这是一个新实例,我需要再次调用load/sync。

这篇文章实际上把事情讲得很清楚(不像微软提供的官方文档)。

参见方法加载与属性加载。

这是一个错误的原因是您正在检索对象当你第二次引用它的时候,你会重新审视它——这样你就得到了一个品牌新的代理对象,它不知道您加载的信息在它的孪生兄弟身上。顺便说一下,这是工作中很常见的错误with collections:首先获取collection.getItem("key")并调用加载,然后同步,然后重新查询collection.getItem("key") -而后者是原物的全新复制品,这违背了加载项目的初衷!