在搜索操作期间无法替换文本
Not able to replace text during search operation
我目前正在使用 office-js api 搜索一个 word 文档,并找到放置在 word doc 中的标记的所有实例。但是,当我尝试替换匹配的令牌时,我得到 debug.errorlocation = "Range.insertParagraph"
。似乎该操作应该按其编写的方式工作,但是在找到搜索结果时它不会替换所需的单词。
示例字符串
Our strategy is to consider ~~client~~'s business needs, and our audit will specifically focus on these related key factors:
代码
Word.run(function (context) {
var content = contentObject.Content.replace(/<img[^>"']*((("[^"]*")|('[^']*'))[^"'>]*)*>/g, "");
var range = context.document.getSelection().insertHtml(content, Word.InsertLocation.replace);
var paragraphs = context.document.body.paragraphs;
var clientName;
paragraphs.load(paragraphs, range, 'text');
return context.sync().then(function () {
for (var x = 0; x < paragraphs.items.length; x++) {
var paragraph = paragraphs.items[x];
var styleType = paragraphs.items[x].text.toString().match(/~~([^]*?)~~/g);
if (paragraphs.items[x].text.search("~~") >= 0 && styleType[0] != "~~/picture~~") {
var styleValue = styleType[0].toString().replace(/[\]~~)}[{(]/g, '').trim();
paragraph.style = styleValue;
}
if(paragraphs.items[x].style === "/Title Page Client Name")
{
var name = paragraphs.items[x].text;
clientName = name;
}
}
return context.sync().then(function () {
var searchResults = context.document.body.search('~~client~~', { ignoreSpace: true });
context.load(searchResults);
return context.sync().then(function () {
for (var i = 0; i < searchResults.items.length; i++) {
error location>> searchResults.items[i].insertParagraph(clientName, Word.InsertLocation.replace);
}
clientName = "";
})
})
})
.then(context.sync)
.then(cleanTags())
.catch(function (error) {
feedBackMessage(error.description);
})
});
};
好的,导致我出错的问题是我使用了 .insertParagraph
,其中单词 api 需要插入段落,而不仅仅是一个单词。我认为这个 api 设计得足够好,可以实际检测段落与简单的文本插入,这真是太棒了。作为记录,如果有人要插入文本(仅一个词),他们将需要使用 .insertText
。
写法代码
return context.sync().then(function () {
var searchResults = context.document.body.search('~~client~~', { ignoreSpace: true });
context.load(searchResults);
return context.sync().then(function () {
for (var i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].insertText(clientName, Word.InsertLocation.replace);
}
clientName = "";
return context.sync().then(function () {
})
})
})
我目前正在使用 office-js api 搜索一个 word 文档,并找到放置在 word doc 中的标记的所有实例。但是,当我尝试替换匹配的令牌时,我得到 debug.errorlocation = "Range.insertParagraph"
。似乎该操作应该按其编写的方式工作,但是在找到搜索结果时它不会替换所需的单词。
示例字符串
Our strategy is to consider ~~client~~'s business needs, and our audit will specifically focus on these related key factors:
代码
Word.run(function (context) {
var content = contentObject.Content.replace(/<img[^>"']*((("[^"]*")|('[^']*'))[^"'>]*)*>/g, "");
var range = context.document.getSelection().insertHtml(content, Word.InsertLocation.replace);
var paragraphs = context.document.body.paragraphs;
var clientName;
paragraphs.load(paragraphs, range, 'text');
return context.sync().then(function () {
for (var x = 0; x < paragraphs.items.length; x++) {
var paragraph = paragraphs.items[x];
var styleType = paragraphs.items[x].text.toString().match(/~~([^]*?)~~/g);
if (paragraphs.items[x].text.search("~~") >= 0 && styleType[0] != "~~/picture~~") {
var styleValue = styleType[0].toString().replace(/[\]~~)}[{(]/g, '').trim();
paragraph.style = styleValue;
}
if(paragraphs.items[x].style === "/Title Page Client Name")
{
var name = paragraphs.items[x].text;
clientName = name;
}
}
return context.sync().then(function () {
var searchResults = context.document.body.search('~~client~~', { ignoreSpace: true });
context.load(searchResults);
return context.sync().then(function () {
for (var i = 0; i < searchResults.items.length; i++) {
error location>> searchResults.items[i].insertParagraph(clientName, Word.InsertLocation.replace);
}
clientName = "";
})
})
})
.then(context.sync)
.then(cleanTags())
.catch(function (error) {
feedBackMessage(error.description);
})
});
};
好的,导致我出错的问题是我使用了 .insertParagraph
,其中单词 api 需要插入段落,而不仅仅是一个单词。我认为这个 api 设计得足够好,可以实际检测段落与简单的文本插入,这真是太棒了。作为记录,如果有人要插入文本(仅一个词),他们将需要使用 .insertText
。
写法代码
return context.sync().then(function () {
var searchResults = context.document.body.search('~~client~~', { ignoreSpace: true });
context.load(searchResults);
return context.sync().then(function () {
for (var i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].insertText(clientName, Word.InsertLocation.replace);
}
clientName = "";
return context.sync().then(function () {
})
})
})