【发布时间】:2017-07-12 20:57:04
【问题描述】:
我正在使用 javascript api 开发 Word 插件。我正在尝试在选择中显示第一段的文本。稍后我想更改这一段。
但是,如果段落包含在表格中,则已经显示文本会失败。如果选择了表格单元格,则单元格中包含的任何段落似乎都不包含在 context.document.getSelection().paragraphs 集合中。
为什么会这样?如果所选内容包括大表格中的表格单元格,我应该如何导航到所选内容的第一段?
这是我用来调试问题的代码:
(function () {
"use strict";
// The initialize function is run each time the page is loaded.
Office.initialize = function (reason) {
$(document).ready(function () {
// Use this to check whether the API is supported in the Word client.
if (Office.context.requirements.isSetSupported('WordApi', 1.1)) {
// Do something that is only available via the new APIs
$('#button').click(doSomething);
$('#status').html('Everything is fine.');
}
else {
// Just letting you know that this code will not work with your version of Word.
$('#supportedVersion').html('This code requires WordApi 1.1 or greater.');
}
});
};
function doSomething() {
Word.run(function (context) {
// get selection
var selection = context.document.getSelection();
// load the text of all paragraphs in the selection
var paragraphs = selection.paragraphs.load('text');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(function() {
$('#status').html(JSON.stringify(paragraphs.items));
});
})
.catch(function (error) {
$('#status').html('Error: ' + JSON.stringify(error));
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
})();
第一张图片中的选择返回[{"text":"Content21"}],正如预期的那样。
第二张图片中的选择根本不返回任何文本:[]。 paragraps-collection 似乎是空的。这对我来说似乎没有任何意义。选择中有段落。
我做错了什么?
【问题讨论】:
标签: javascript ms-word ms-office office-js office-addins