Visio javascript API - 无法 read/extract 塑造数据
Visio javascript API - Can't read/extract Shape data
我无法检索基本的形状信息,最重要的信息是 .getBounds()。有了这个我就可以知道 (X, Y) 的位置以及形状的大小。
我现在只能检索到形状的名称和文本。
我正在使用这个 javascript Visio API。
https://docs.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.
}
});
如何 get/invoke 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.
我找到了解决方案。一开始并不明显,直到我读了这篇文章。
但本质上,我调用的任何方法 returns 都是 visio 代理的新实例。由于它是一个新实例,我需要再次调用 load/sync。
这里是article,实际上把事情说清楚了(不像微软提供的官方文档)。
参见名为 加载方法与属性的部分。
The reason that this is an error is that you are retrieving the object
anew the second time when you reference it – and so you get a brand
new proxy object, which has no knowledge of the information you loaded
on its twin. This, by the way, is a very common mistake when working
with collections: to first fetch collection.getItem("key") and call
load on it, then sync, and then re-query collection.getItem("key") –
with the latter being a brand new copy of the original object, which
defeats the purpose of having loaded the item to begin with!
我无法检索基本的形状信息,最重要的信息是 .getBounds()。有了这个我就可以知道 (X, Y) 的位置以及形状的大小。
我现在只能检索到形状的名称和文本。
我正在使用这个 javascript Visio API。 https://docs.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.
}
});
如何 get/invoke 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.
我找到了解决方案。一开始并不明显,直到我读了这篇文章。 但本质上,我调用的任何方法 returns 都是 visio 代理的新实例。由于它是一个新实例,我需要再次调用 load/sync。
这里是article,实际上把事情说清楚了(不像微软提供的官方文档)。
参见名为 加载方法与属性的部分。
The reason that this is an error is that you are retrieving the object anew the second time when you reference it – and so you get a brand new proxy object, which has no knowledge of the information you loaded on its twin. This, by the way, is a very common mistake when working with collections: to first fetch collection.getItem("key") and call load on it, then sync, and then re-query collection.getItem("key") – with the latter being a brand new copy of the original object, which defeats the purpose of having loaded the item to begin with!