|
初学js,许多语法还不太熟悉,用forEach精简一下代码,2重循环应该无法避免,因为要标记搜索到关键词字体颜色,必须嵌套循环
- function test1(){
- Range("表2").Columns.Font.Color = 0; //表2字体颜色恢复默认
- let myColor = [0xff, 0xfff900, 0xff00ff]
- Range("表2").Columns(1).Value2.forEach((x, i) => {
- let reg = new RegExp("\\b" + x[0] + "\\b", "i");
- let iCount = 0;
- let iLen = x[0].length + 1;
- Range("表2").Columns(3).Value2.forEach((y, j) => {
- let position = y[0].search(reg);
- let posi = position;
- while (posi !== -1){
- iCount++;
- Range("表2").Cells(j+1,3).Characters(position,iLen).Font.Color = myColor[i];
- posi = y[0].substring(position + 1).search(reg);
- position = position + 1 + posi;
- }
- })
- Range("表2").Cells(i+1,2).Value2 = iCount;
- })
- }
复制代码
|
|