我问了一个有关如何在Excel中使用Office.js获取单元格格式的问题。我再次有同样的问题,但是这次关于ms-word的问题是,我可能会得到在word应用程序中创建的表格单元格中存在的格式化文本。
虽然我可以将选定的文本作为html来获取,但是这给了我所需的样式
Office.context.document.getSelectedDataAsync(Office.CoercionType.Html,
function (result) {
if (result.status === Office.AsyncResultStatus.Succeeded) {
showNotification('The selected text is:', '"' + result.value + '"');
} else {
showNotification('Error:', result.error.message);
}
});
我只想要当前单元格格式的文本,谢谢!
很好的问题Pradeep。为了获得单元格格式,您需要使用当前预览的Word 1.3 API。您可以在此处查看如何尝试1.3 Apis 。(请注意,您需要使用该页面上公开的Office.js CDN预览版!)在此处检查所有新功能。
现在,一旦您准备尝试1.3,下面的代码将为您提供单元格格式设置信息。一般来说,代码在做什么
在下面的代码中执行此操作。请注意,此代码将返回您在整个单元格中应用的格式。如果要转到下一层,即逐字获取格式信息,则需要在body对象上应用split方法,这样就可以遍历并获取每个返回范围的格式信息。希望这会有所帮助!编码愉快!!
function getFormattedText() {
Word.run(function (context) {
//step 1: lets get the selection's range. and find out if its within a table cell.....
var mySelection = context.document.getSelection().parentTableCell;
context.load(mySelection);
return context.sync()
.then(function () {
if (mySelection.isNull == true) {
//selection is not within a cell.....
console.log("selection not in a header");
}
else {
// the selection is inside a cell! lets get the content....
var body = mySelection.body;
context.load(body, { expand: 'font' });
return context.sync()
.then(function () {
console.log(body.font.name + " " + body.font.color); // at this point all the font properties are available, bold, italic color.. etc...
})
.catch(function (e) {
console.log(e.message);
});
}
})
.catch(function (e) {
console.log(e.message);
});
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句