Office js 从Word JavaScript API上下文对象加载选定属性
我正在使用Word JavaScript API开发Word插件,需要加载文档中的所有段落。对象相当大,包含许多我不需要的属性 为了优化流程,我尝试只加载每个段落的属性。但是,无论我做什么,外接程序这个词仍然会加载过滤掉的属性,只是会显示一条错误消息 我已尝试通过以下方式仅加载“文本”字段:Office js 从Word JavaScript API上下文对象加载选定属性,office-js,Office Js,我正在使用Word JavaScript API开发Word插件,需要加载文档中的所有段落。对象相当大,包含许多我不需要的属性 为了优化流程,我尝试只加载每个段落的属性。但是,无论我做什么,外接程序这个词仍然会加载过滤掉的属性,只是会显示一条错误消息 我已尝试通过以下方式仅加载“文本”字段: context.document.body.paragraphs.load('text'); context.document.body.paragraphs.load(['text']); context
context.document.body.paragraphs.load('text');
context.document.body.paragraphs.load(['text']);
context.document.body.paragraphs.load({ text: true });
但是,在这三种情况下,段落.items
对象都包含列出的所有属性
这或多或少是当前输出的样子:
{
items: [
{
alignment: [Exception: RichApi.Error: The property 'alignment' is not available. Before reading the property's value, call the load method...],
firstLineIndent: [Exception: RichApi.Error: The property 'firstLineIndent' is not available. Before reading the property's value, call the load method...],
text: 'Some paragraph text',
...
},
...
]
}
我希望ParagraphCollection的输出如下所示:
{
items: [
{
text: 'Some paragraph text'
// No other properties should be loaded
},
{
text: 'Some other paragraph text'
}
]
}
任何帮助都将不胜感激
Morgan要获得所需的输出,可以对对象调用
.toJSON()
。即:
context.document.body.paragraphs.load('text');
await context.sync();
console.log(context.document.body.paragraphs.toJSON())
或者,如果希望它是字符串形式,则可以用以下内容替换最后一行JSON.stringify
自动调用封面下方的toJSON
:
console.log(JSON.stringify(context.document.body.paragraphs, null, 4));
原因是:与之交互的API对象是代理对象。因此,即使属性可能未加载(即使对象本身可能不存在于文档中),它们也为getter和setter提供了方法和占位符。而调用
toJSON()
将获取所有加载的属性(如果有的话),并为您提供一个与数据相对应的普通ol'JavaScript对象。谢谢,它可以工作!我非常感谢您的及时回复和解释。我唯一的意见是,在您的示例中,它应该是:context.document.parations.toJson()
(即,您需要添加“context”)。