[英]How do I get Microsoft word document text as a string?
我是 Microsoft Office 插件和 JS 的新手。 我正在嘗試開發一個 Microsoft Word 插件,將文檔中的選定文本轉換為二維碼。 所以我的問題是將文檔中的選定文本作為簡單字符串獲取。 到目前為止我沒有嘗試過。 這里有一個鏈接,用於獲取文檔中的全部文本,這有點幫助: Word 加載項獲取完整的文檔文本? . 我需要的是如何將選定的文本作為字符串獲取。 我需要你的幫助。 我嘗試了以下內容:
txt = "";
await Word.run(async (context) => {
var documentBody = context.document.body;
context.load(documentBody);
return context.sync().then(function () {
console.log(documentBody.text); //full document text
console.log(document.getSelection.text); //selected only
txt = documentBody.text.getSelection();
});
});
檢查腳本實驗室。 Word 中的第一個示例正是您所需要的:
$("#run").click(() => tryCatch(run));
function run() {
return Word.run(function(context) {
var range = context.document.getSelection();
range.font.color = "red";
range.load("text");
return context.sync().then(function() {
console.log('The selected text was "' + range.text + '".');
});
});
}
/** Default helper for invoking an action and handling errors. */
function tryCatch(callback) {
Promise.resolve()
.then(callback)
.catch(function(error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.