【C#】【EXCEL】BumblebeeComponentsAnalysisGH_Ex_Ana_CondVal.cs

这段代码定义了一个名为 GH_Ex_Ana_CondVal 的 Grasshopper 组件,其主要功能是为 Excel 工作表中的特定范围添加条件格式。以下是对这个组件的功能和特点的详细介绍:

  1. 功能概述:
    这个组件允许用户在 Grasshopper 环境中为 Excel 工作表的特定单元格范围添加基于值的条件格式。用户可以指定一个数值、条件类型和颜色,组件会根据这些参数在指定的 Excel 范围内应用相应的条件格式。

  2. 主要特点:

    • 灵活的条件设置:用户可以选择不同的条件类型(如大于、小于、等于等)。
    • 自定义颜色:允许用户指定用于高亮显示的颜色。
    • 可选的清除功能:可以选择在应用新条件前清除现有的条件格式。
    • 激活选项:提供一个开关来控制是否应用条件格式。
  3. 输入参数:

    • 工作表:指定要操作的 Excel 工作表。
    • 范围:指定要应用条件格式的单元格范围。
    • 值:设置条件格式的比较值。
    • 类型:选择条件类型(如大于、小于、等于等)。
    • 单元格颜色:指定满足条件时的高亮颜色。
    • 清除选项:决定是否在应用新条件前清除现有条件。
    • 激活选项:控制是否应用条件格式。
  4. 输出:

    • 处理后的 Excel 范围对象。
  5. 使用场景:
    这个组件特别适用于数据分析和可视化场景,例如:

    • 突出显示超过某个阈值的销售数据。
    • 标记低于平均值的性能指标。
    • 创建简单的热图来展示数据分布。
  6. 技术特点:

    • 继承自 GH_Ex_Rng__Base 类,可能复用了一些基础的 Excel 操作功能。
    • 使用 Grasshopper 的参数系统来处理输入和输出。
    • 通过枚举类型 ValueCondition 来定义不同的条件类型。
  7. 用户界面:

    • 组件在 Grasshopper 界面中显示为次要组件(secondary exposure)。
    • 使用自定义图标(BB_Cond_Value_01)以便于识别。
  8. 扩展性:

    • 通过修改 RegisterInputParams 方法,可以轻松添加新的输入参数。
    • SolveInstance 方法可以进行扩展,以实现更复杂的条件格式逻辑。

总的来说,这个组件为 Grasshopper 用户提供了一个强大而灵活的工具,用于在 Excel 中快速应用条件格式,从而增强数据的可视化效果和分析能力。它将 Excel 的条件格式功能无缝集成到 Grasshopper 的参数化设计环境中,使得数据分析和可视化过程更加高效和直观。

Flow diagram

Yes / 是
Yes / 是
No / 否
No / 否
Start / 开始
Get Worksheet / 获取工作表
Get Range / 获取范围
Get Value / 获取值
Get Condition Type / 获取条件类型
Get Cell Color / 获取单元格颜色
Get Clear Option / 获取清除选项
Get Activate Option / 获取激活选项
Activate? / 是否激活?
Clear? / 是否清除?
Clear Conditions / 清除条件
Add Conditional Value / 添加条件值
Set Output / 设置输出
End / 结束

Explanation (对应解释):

  1. Start (开始) - Beginning of the SolveInstance method
  2. Get Worksheet (获取工作表) - DA.GetData(0, ref gooS);
  3. Get Range (获取范围) - DA.GetData(1, ref gooR);
  4. Get Value (获取值) - DA.GetData(2, ref value);
  5. Get Condition Type (获取条件类型) - DA.GetData(3, ref type);
  6. Get Cell Color (获取单元格颜色) - DA.GetData(4, ref color);
  7. Get Clear Option (获取清除选项) - DA.GetData(5, ref clear);
  8. Get Activate Option (获取激活选项) - DA.GetData(6, ref activate);
  9. Activate? (是否激活?) - if (activate)
  10. Clear? (是否清除?) - if (clear)
  11. Clear Conditions (清除条件) - range.ClearConditions();
  12. Add Conditional Value (添加条件值) - range.AddConditionalValue((ValueCondition)type, value, color);
  13. Set Output (设置输出) - DA.SetData(0, range);
  14. End (结束) - End of the SolveInstance method

Description

public class GH_Ex_Ana_CondVal : GH_Ex_Rng__Base
{
    // 1. 构造函数
    public GH_Ex_Ana_CondVal()
      : base("Conditional Value", "Value",
          "Add conditional formatting to a Range based on a value",
          Constants.ShortName, Constants.SubAnalysis)
    {
    }
  1. 构造函数 (Constructor):
    这个构造函数初始化了 GH_Ex_Ana_CondVal 组件。

    • 它调用基类 GH_Ex_Rng__Base 的构造函数。
    • 设置组件的名称为 “Conditional Value”。
    • 设置组件的昵称为 “Value”。
    • 提供了组件的描述:“Add conditional formatting to a Range based on a value”。
    • 使用 Constants.ShortName 和 Constants.SubAnalysis 设置组件的类别。

    English: This constructor initializes the GH_Ex_Ana_CondVal component. It calls the base class constructor, sets the component’s name, nickname, description, and category.

    // 2. Exposure 属性
    public override GH_Exposure Exposure
    {
        get { return GH_Exposure.secondary; }
    }
  1. Exposure 属性:

    • 这个属性设置组件在 Grasshopper 界面中的暴露级别。
    • 返回 GH_Exposure.secondary,表示这是一个次要组件。
    • 这影响组件在 Grasshopper 界面中的显示位置和重要性。

    English: This property sets the exposure level of the component in the Grasshopper interface. Returning GH_Exposure.secondary indicates it’s a secondary component, affecting its display position and importance.

    // 3. RegisterInputParams 方法
    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        base.RegisterInputParams(pManager);
        pManager[1].Optional = true;
        pManager.AddNumberParameter("Value", "V", "The value to check against", GH_ParamAccess.item);
        pManager.AddIntegerParameter("Type", "T", "The condition type", GH_ParamAccess.item, 0);
        pManager[3].Optional = true;
        pManager.AddColourParameter("Cell Color", "C", "The cell highlight color", GH_ParamAccess.item, Sd.Color.LightGray);
        pManager[4].Optional = true;
        pManager.AddBooleanParameter("Clear", "_X", "If true, the existing conditions will be cleared", GH_ParamAccess.item, false);
        pManager[5].Optional = true;
        pManager.AddBooleanParameter("Activate", "_A", "If true, the condition will be applied", GH_ParamAccess.item, false);
        pManager[6].Optional = true;

        Param_Integer paramA = (Param_Integer)pManager[3];
        foreach (ValueCondition value in Enum.GetValues(typeof(ValueCondition)))
        {
            paramA.AddNamedValue(value.ToString(), (int)value);
        }
    }
  1. RegisterInputParams 方法:
    这个方法注册组件的输入参数。

    • 首先调用基类的 RegisterInputParams 方法。
    • 设置第二个参数(索引1)为可选。
    • 添加一个数字参数 “Value”,用于指定要检查的值。
    • 添加一个整数参数 “Type”,用于指定条件类型。
    • 添加一个颜色参数 “Cell Color”,用于设置单元格高亮颜色。
    • 添加两个布尔参数 “Clear” 和 “Activate”,分别用于清除现有条件和激活条件。
    • 最后,为条件类型参数添加命名值,使用 ValueCondition 枚举。

    English: This method registers the input parameters for the component. It adds several parameters including value, condition type, cell color, clear option, and activate option. It also adds named values for the condition type parameter using the ValueCondition enum.

    // 4. RegisterOutputParams 方法
    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        base.RegisterOutputParams(pManager);
    }
  1. RegisterOutputParams 方法:

    • 这个方法注册组件的输出参数。
    • 在这个例子中,它只是调用了基类的 RegisterOutputParams 方法。
    • 这意味着输出参数的设置完全依赖于基类的实现。

    English: This method registers the output parameters for the component. In this case, it simply calls the base class method, meaning the output parameter setup relies entirely on the base class implementation.

    // 5. SolveInstance 方法
    protected override void SolveInstance(IGH_DataAccess DA)
    {
        IGH_Goo gooS = null;
        DA.GetData(0, ref gooS);
        ExWorksheet worksheet = new ExWorksheet();
        bool hasWs = gooS.TryGetWorksheet(ref worksheet);

        IGH_Goo gooR = null;
        DA.GetData(1, ref gooR);
        ExRange range = new ExRange();
        if (!gooR.TryGetRange(ref range, worksheet)) return;
        if (!hasWs) worksheet = range.Worksheet;

        double value = 1.0;
        if (!DA.GetData(2, ref value)) return;

        int type = 0;
        DA.GetData(3, ref type);

        Sd.Color color = Sd.Color.LightGray;
        DA.GetData(4, ref color);

        bool clear = false;
        DA.GetData(5, ref clear);

        bool activate = false;
        DA.GetData(6, ref activate);

        if (activate)
        {
            if (clear) range.ClearConditions();
            range.AddConditionalValue((ValueCondition)type, value, color);
        }

        DA.SetData(0, range);
    }
  1. SolveInstance 方法:
    这是组件的核心方法,执行主要的逻辑。

    • 获取工作表:从输入获取工作表对象。
    • 获取范围:从输入获取 Excel 范围对象。
    • 获取值:获取要检查的数值。
    • 获取条件类型:获取条件格式的类型。
    • 获取单元格颜色:获取用于高亮的颜色。
    • 获取清除选项:确定是否清除现有条件。
    • 获取激活选项:确定是否应用新的条件格式。
    • 如果激活:
      • 如果需要清除,则清除现有条件。
      • 添加新的条件值格式。
    • 设置输出:将处理后的范围对象设置为输出。

    English: This is the core method of the component, performing the main logic. It retrieves the worksheet, range, value, condition type, cell color, clear option, and activate option from the inputs. If activated, it applies the conditional formatting to the range based on the specified parameters.

    // 6. Icon 属性
    protected override System.Drawing.Bitmap Icon
    {
        get
        {
            return Properties.Resources.BB_Cond_Value_01;
        }
    }
  1. Icon 属性:

    • 这个属性提供组件的图标。
    • 返回一个预定义的资源图像 BB_Cond_Value_01。
    • 这个图标会在 Grasshopper 界面中显示,用于视觉识别组件。

    English: This property provides the icon for the component. It returns a predefined resource image that will be displayed in the Grasshopper interface for visual identification of the component.

    // 7. ComponentGuid 属性
    public override Guid ComponentGuid
    {
        get { return new Guid("04da0fcd-d474-4ebd-a858-6bd92c46311a"); }
    }
}
  1. ComponentGuid 属性:

    • 返回组件的唯一标识符(GUID)。
    • 这个 GUID 用于在 Grasshopper 中唯一标识这个组件。
    • 在发布后不应更改这个 GUID,以确保组件的一致性和兼容性。

    English: This property returns the unique identifier (GUID) for the component. This GUID is used to uniquely identify the component in Grasshopper and should not be changed after release to ensure consistency and compatibility.

总结:
这个 GH_Ex_Ana_CondVal 类定义了一个 Grasshopper 组件,用于在 Excel 范围内添加基于值的条件格式。它允许用户指定一个值、条件类型和颜色,然后根据这些参数在指定的 Excel 范围内应用条件格式。这个组件对于数据可视化和分析非常有用,可以快速突出显示满足特定条件的单元格。通过仔细设计的输入参数和逻辑处理,它提供了一个灵活且强大的工具,用于 Excel 数据的条件格式化。

Code

public class GH_Ex_Ana_CondVal : GH_Ex_Rng__Base
{
    // 构造函数
    // 初始化组件,设置名称、昵称、描述和类别
    public GH_Ex_Ana_CondVal()
      : base("Conditional Value", "Value",
          "Add conditional formatting to a Range based on a value",
          Constants.ShortName, Constants.SubAnalysis)
    {
        // 这里不需要额外的初始化代码,因为所有设置都在基类构造函数中完成
    }

    // 设置组件的暴露级别
    // 这影响组件在Grasshopper界面中的显示位置和重要性
    public override GH_Exposure Exposure
    {
        get { return GH_Exposure.secondary; } // 返回次要暴露级别
    }

    // 注册输入参数
    // 这个方法定义了组件所需的所有输入参数
    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        base.RegisterInputParams(pManager); // 调用基类方法注册基本参数
        pManager[1].Optional = true; // 设置第二个参数(范围)为可选

        // 添加用于检查的值参数
        pManager.AddNumberParameter("Value", "V", "The value to check against", GH_ParamAccess.item);

        // 添加条件类型参数
        pManager.AddIntegerParameter("Type", "T", "The condition type", GH_ParamAccess.item, 0);
        pManager[3].Optional = true;

        // 添加单元格颜色参数
        pManager.AddColourParameter("Cell Color", "C", "The cell highlight color", GH_ParamAccess.item, Sd.Color.LightGray);
        pManager[4].Optional = true;

        // 添加清除现有条件的布尔参数
        pManager.AddBooleanParameter("Clear", "_X", "If true, the existing conditions will be cleared", GH_ParamAccess.item, false);
        pManager[5].Optional = true;

        // 添加激活条件的布尔参数
        pManager.AddBooleanParameter("Activate", "_A", "If true, the condition will be applied", GH_ParamAccess.item, false);
        pManager[6].Optional = true;

        // 为条件类型参数添加命名值
        Param_Integer paramA = (Param_Integer)pManager[3];
        foreach (ValueCondition value in Enum.GetValues(typeof(ValueCondition)))
        {
            paramA.AddNamedValue(value.ToString(), (int)value);
        }
    }

    // 注册输出参数
    // 这个方法定义了组件的输出参数
    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        base.RegisterOutputParams(pManager); // 调用基类方法注册输出参数
    }

    // 主要的求解方法
    // 这个方法包含组件的核心逻辑
    protected override void SolveInstance(IGH_DataAccess DA)
    {
        // 获取工作表
        IGH_Goo gooS = null;
        DA.GetData(0, ref gooS);
        ExWorksheet worksheet = new ExWorksheet();
        bool hasWs = gooS.TryGetWorksheet(ref worksheet);

        // 获取范围
        IGH_Goo gooR = null;
        DA.GetData(1, ref gooR);
        ExRange range = new ExRange();
        if (!gooR.TryGetRange(ref range, worksheet)) return;
        if (!hasWs) worksheet = range.Worksheet;

        // 获取要检查的值
        double value = 1.0;
        if (!DA.GetData(2, ref value)) return;

        // 获取条件类型
        int type = 0;
        DA.GetData(3, ref type);

        // 获取单元格颜色
        Sd.Color color = Sd.Color.LightGray;
        DA.GetData(4, ref color);

        // 获取清除选项
        bool clear = false;
        DA.GetData(5, ref clear);

        // 获取激活选项
        bool activate = false;
        DA.GetData(6, ref activate);

        // 如果激活,应用条件格式
        if (activate)
        {
            if (clear) range.ClearConditions(); // 如果需要清除,清除现有条件
            range.AddConditionalValue((ValueCondition)type, value, color); // 添加条件值格式
        }

        // 设置输出
        DA.SetData(0, range);
    }

    // 提供组件图标
    // 这个属性返回组件在Grasshopper界面中显示的图标
    protected override System.Drawing.Bitmap Icon
    {
        get
        {
            return Properties.Resources.BB_Cond_Value_01;
        }
    }

    // 获取组件的唯一ID
    // 这个属性返回组件的唯一标识符,用于在Grasshopper中识别组件
    public override Guid ComponentGuid
    {
        get { return new Guid("04da0fcd-d474-4ebd-a858-6bd92c46311a"); }
    }
}

这段代码现在包含了详细的中文注释,解释了每个方法和属性的功能和目的。这些注释涵盖了:

  1. 类的整体结构和继承关系
  2. 构造函数的作用和参数设置
  3. Exposure属性的意义
  4. RegisterInputParams方法中各输入参数的含义和设置
  5. RegisterOutputParams方法的作用
  6. SolveInstance方法中的逻辑流程,包括数据获取和条件格式应用
  7. Icon属性的用途
  8. ComponentGuid属性的重要性
  • 18
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmywillstronger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值