1:需求
随着技术的发展,人们的需求也越来越高. Office 文件的在线处理也越来越受到人们的广泛关注.但文档太长的话,就需要搜索关键字查找自己想要的内容.并且高亮显示这些关键字
2:方案
用pageoffice 提供的执行 宏的js 方法
3:核心代码
document.getElementById("PageOfficeCtrl1").RunMacro( MacroName, MacroScript );//运行文档中的无返回值的宏命令.
strValue= document.getElementById("PageOfficeCtrl1").RunMacro( MacroName,MacroScript);//运行有返回值的宏命令.
3:实现过程
(1)先用 pageoffice 在线打开文档
具体请参考 https://my.oschina.net/u/3507515/blog/1802792
(2) 添加显示高亮和取消高亮的按钮
<input name="button" id="Button1" type="button" onclick="SetKeyWord(strKey,true)" value="高亮显示关键字" />
<input name="button" id="Button2" type="button" onclick="SetKeyWord(strKey,false)" value="取消关键字显示" />
(3)写对应的显示取消高亮的js方法
function SetKeyWord(key, visible) {
if (key=="null"||"" == key) {
document.getElementById("PageOfficeCtrl1").Alert("关键字为空。");
return;
}
var sMac = "function myfunc()" + "\r\n"
+ "Application.Selection.HomeKey(6) \r\n"
+ "Application.Selection.Find.ClearFormatting \r\n"
+ "Application.Selection.Find.Replacement.ClearFormatting \r\n"
+ "Application.Selection.Find.Text = \"" + key + "\" \r\n"
+ "While (Application.Selection.Find.Execute()) \r\n"
+ "If (" + visible + ") Then \r\n"
+ "Application.Selection.Range.HighlightColorIndex = 7 \r\n"
+ "Else \r\n"
+ "Application.Selection.Range.HighlightColorIndex = 0 \r\n"
+ "End If \r\n"
+ "Wend \r\n"
+ "Application.Selection.HomeKey(6) \r\n"
+ "End function";
document.getElementById("PageOfficeCtrl1").RunMacro("myfunc", sMac);
}
(4)最终效果
注意一定要启用客户端电脑 office 的宏