我需要检测文档是否为空,如果是,则使用Word插件向用户显示提示。
有没有办法从OfficeJS Word插件中或通过应用编程接口来检测空文档?
发布于 2017-02-25 00:21:32
您应该能够获取文档的ParagraphCollection并使用它执行检查,如下所示:
Word.run(function (context) {
var paragraphs = context.document.body.paragraphs;
context.load(paragraphs, 'text');
return context.sync().then(function () {
if (paragraphs.items.length === 0) {
// Empty document!
}
});
});这将适用于完全空白的文档。如果希望更加严格,则需要对每个paragraphs.items[]对象的text属性添加额外的检查。
发布于 2018-01-02 20:16:27
Word.run(function(context) {
var body = context.document.body;
body.load();
return context.sync().then(function() {
if(body.text.trim().length) {
//Document is not Empty. Add your code here you want to run in this case.
}
else
//Empty Document
});
});body.text.trim().length返回文档中的字符数。如果文档长度为空,则和条件的计算结果为false,否则将执行部分。
https://stackoverflow.com/questions/42434975
复制相似问题