Excel催化剂开源第20波-条件格式版聚光灯功能,行列标示方便阅读

Excel聚光灯功能,辅助数据查看,选择区域下的高亮显示所在行列位置,此功能已被广大Excel开发者研究得十分透彻,各种版本的聚光灯流转在网络里,同样地也是一大堆的VBA代码,难找.Net的现成代码,且大部分代码两级分化的入门级和走火入魔级。

Excel催化剂用了条件格式的方式实现聚光灯,唯一的小问题就是把原生的撤销功能给清除了,但其实这真是无关痛痒的不影响使用的小缺憾。

实现原理简介

通过SelectionChange事件,当用户选择不同单元格时激发事件,事件中对当前选择的单元格所在的行列区域进行条件格式设定,且条件为真,格式为填充色,并且条件格式的优先级为最顶级,即这个条件格式覆盖其他同样区域的条件格式。

关键点为将当前的条件格式起作用的区域进行保存,下次SelectionChange响应时,进行上次区域条件格式删除和当前区域的条件格式重新设定。

具体代码

在Ribbon上用复选框的方法提供开启或关闭开关。并将状态写入到配置信息的SpotLightIsEnable上

 private void chkSpotLight_Click(object sender, RibbonControlEventArgs e)
 {
 try
 {
 if (!chkSpotLight.Checked)
 {
 SpotLight.DeletePreviouSpotLightCondition();
 }
 Properties.Settings.Default.SpotLightIsEnable = chkSpotLight.Checked;
 Properties.Settings.Default.Save();
 }
 catch (Exception)
 {
 throw;
 }
 }

订阅SheetSelectionChange 事件

Common.ExcelApp.SheetSelectionChange += ExcelApp_SheetSelectionChangeSpotLightSetting;

事件方法实现

当开关打开时才生效

 private void ExcelApp_SheetSelectionChangeSpotLightSetting(object Sh, Excel.Range Target)
 {
 if (Properties.Settings.Default.SpotLightIsEnable)
 {
 SpotLight.AddFormatConditionToShowSpotLight(Target);
 }
 }

核心代码实现

 private static Excel.Range previousSpotLightRange = null;
 public static void AddFormatConditionToShowSpotLight(Excel.Range target)
 {
 DeletePreviouSpotLightCondition();
 Excel.Worksheet sht = target.Parent;
 int colIndex = target.Column;
 int rowIndex = target.Row;
 Excel.Range spotLightRange = Common.ExcelApp.Union(sht.Range[sht.Cells[1,colIndex],target.Offset[-1,0].Resize[1,target.Columns.Count]],
 sht.Range[target.Offset[target.Rows.Count, 0], sht.Cells[sht.Rows.Count, colIndex]],
 sht.Range[sht.Cells[rowIndex, 1], target.Offset[0, -1].Resize[target.Rows.Count,1]],
 sht.Range[target.Offset[0, target.Columns.Count],sht.Cells[rowIndex,sht.Columns.Count]]
 );
 Excel.FormatCondition currentFormatCondition = spotLightRange.FormatConditions.Add(Type: Excel.XlFormatConditionType.xlExpression, Formula1: "=TRUE");
 currentFormatCondition.SetFirstPriority();
 currentFormatCondition.StopIfTrue = true;
 Color spotColor = Properties.Settings.Default.SpotLightColor;
 currentFormatCondition.Interior.Color = ColorTranslator.ToOle(spotColor);
 previousSpotLightRange = spotLightRange;
 }
 public static void DeletePreviouSpotLightCondition()
 {
 if (previousSpotLightRange != null)
 {
 Excel.FormatCondition previousFormatCondition = previousSpotLightRange.FormatConditions[1];
 if (previousFormatCondition.Formula1 == "=TRUE")
 {
 previousFormatCondition.Delete();
 }
 previousSpotLightRange = null;
 }
 }

同样地,更友好的方式是开放对颜色进行自定义设置,增加一个按钮用于设置颜色

 private void btnSpotLightColorSetting_Click(object sender, RibbonControlEventArgs e)
 {
 ColorDialog colorDialog = new ColorDialog();
 var result = colorDialog.ShowDialog();
 if (result == DialogResult.OK)
 {
 Properties.Settings.Default.SpotLightColor = colorDialog.Color;
 btnSpotLightColorSetting.Image = SpotLight.GetSpotColorImage();
 }
 }
 public static Image GetSpotColorImage()
 {
 Bitmap bmp = new Bitmap(50,50);
 Graphics graphics = Graphics.FromImage(bmp);
 
 SolidBrush brush = new SolidBrush(Properties.Settings.Default.SpotLightColor);
 graphics.FillRectangle(brush, 0, 0, 50, 50);
 return bmp;
 }

结语

此篇讲解了聚光灯的实现效果,其中也可以看到条件格式的妙用,是可以在许多场景上有很好的应用。虽然聚光灯功能不算什么刚需功能,但部分初级用户响应度还是蛮好的,不妨在通用插件时也将其实现一翻。

展开阅读全文

页面更新:2024-03-12

标签:聚光灯   行列   条件   功能   格式   结语   无关痛痒   优先级   催化剂   颜色   区域   事件   代码   方式   方法   数码   用户

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top