前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Excel催化剂开源第20波-条件格式版聚光灯功能,行列标示方便阅读

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

作者头像
Excel催化剂
发布2021-08-19 15:06:37
4300
发布2021-08-19 15:06:37
举报
文章被收录于专栏:Excel催化剂

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

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

实现原理简介

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

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

具体代码

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

代码语言:javascript
复制
        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 事件

代码语言:javascript
复制
Common.ExcelApp.SheetSelectionChange += ExcelApp_SheetSelectionChangeSpotLightSetting;

事件方法实现 当开关打开时才生效

代码语言:javascript
复制
        private void ExcelApp_SheetSelectionChangeSpotLightSetting(object Sh, Excel.Range Target)
        {
            if (Properties.Settings.Default.SpotLightIsEnable)
            {
                SpotLight.AddFormatConditionToShowSpotLight(Target);
            }

        }

核心代码实现

代码语言:javascript
复制
        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;
            }
        }

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

代码语言:javascript
复制
        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();
            }
        }
代码语言:javascript
复制
        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;

        }

结语

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Excel催化剂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现原理简介
  • 具体代码
  • 结语
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档