【C#】【EXCEL】Bumblebee/Classes/ExRange.cs

Flow diagram

为了创建一个全面但不过于复杂的流程图,我们将重点关注 ExRange 类的主要功能和方法。以下是一个中英双语的流程图,展示了 ExRange 类的主要结构和操作流程:

默认构造函数
Default Constructor
COM 对象构造函数
COM Object Constructor
复制构造函数
Copy Constructor
属性操作
Property Operations
数据操作
Data Operations
格式化操作
Formatting Operations
条件格式化
Conditional Formatting
图表操作
Chart Operations
开始 / Start
创建 ExRange 实例
Create ExRange Instance
选择构造方法
Choose Constructor
创建空 ExRange
Create Empty ExRange
从 Excel Range 创建
Create from Excel Range
复制现有 ExRange
Copy Existing ExRange
选择操作类型
Choose Operation Type
获取/设置属性
Get/Set Properties
读取/写入数据
Read/Write Data
设置单元格格式
Set Cell Formatting
添加条件格式
Add Conditional Formatting
添加迷你图
Add Sparklines
字体属性
Font Properties
边框属性
Border Properties
单元格尺寸
Cell Dimensions
读取数据
Read Data
清除内容
Clear Content
设置字体
Set Font
设置边框
Set Border
合并单元格
Merge Cells
添加值条件
Add Value Condition
添加平均值条件
Add Average Condition
添加数据条
Add Data Bar
添加折线图
Add Line Sparkline
添加柱状图
Add Column Sparkline
操作完成
Operation Completed
结束 / End

这个流程图展示了 ExRange 类的主要功能和操作流程:

  1. 创建 ExRange 实例(对应构造函数)
  2. 选择操作类型(对应类中的不同方法组)
  3. 执行具体操作(对应具体的方法)
  4. 完成操作

流程图中的每个节点都有中英文说明,对应了代码中的相关部分。例如:

  • “创建 ExRange 实例” 对应了类的构造函数
  • “属性操作” 对应了类中的各种属性,如 FontFamily, FontSize 等
  • “数据操作” 对应了 ReadData() 和 ClearContent() 等方法
  • “格式化操作” 对应了 SetFont(), SetBorder() 等方法
  • “条件格式化” 对应了 AddConditionalValue(), AddConditionalAverage() 等方法
  • “图表操作” 对应了 AddSparkLine() 和 AddSparkColumn() 方法

Description

  1. ReadData() 方法
public GH_Structure<GH_String> ReadData()
{
    // 创建一个新的 GH_Structure 对象来存储单元格数据
    GH_Structure<GH_String> data = new GH_Structure<GH_String>();

    // 获取当前范围的边界信息(起始行列和结束行列)
    int[] L = GetRangeArray();

    // 从 Excel COM 对象中获取整个范围的值数组
    System.Array values = (System.Array)this.ComObj.Value2;

    // 如果值数组不为空,则开始处理数据
    if (values != null)
    {
        // 遍历行(注意:Excel 数组索引从 1 开始)
        for (int i = 1; i < (L[3] - L[1] + 2); i++)
        {
            // 为每一行创建一个新的路径
            GH_Path path = new GH_Path(i);
            // 遍历列
            for (int j = 1; j < (L[2] - L[0] + 2); j++)
            {
                // 初始化单元格值为空字符串
                string val = string.Empty;
                // 如果单元格不为空,则获取其字符串表示
                if (values.GetValue(i, j) != null) val = values.GetValue(i, j).ToString();
                // 将单元格值添加到数据结构中
                data.Append(new GH_String(val), path);
            }
        }
    }

    // 返回包含所有单元格值的数据结构
    return data;
}
  1. SetFont() 方法
public void SetFont(string name, double size, Sd.Color color, Justification justification, bool bold, bool italic)
{
    // 获取范围的字体对象
    XL.Font font = this.ComObj.Font;

    // 设置字体名称
    font.Name = name;
    // 设置字体大小
    font.Size = size;
    // 设置字体颜色
    font.Color = color;
    // 设置是否加粗
    font.Bold = bold;
    // 设置是否斜体
    font.Italic = italic;

    // 设置水平对齐方式(使用自定义的 Justification 枚举转换方法)
    this.ComObj.HorizontalAlignment = justification.ToExcelHalign();
    // 设置垂直对齐方式(使用自定义的 Justification 枚举转换方法)
    this.ComObj.VerticalAlignment = justification.ToExcelValign();
}
  1. AddConditionalValue() 方法
public void AddConditionalValue(ValueCondition condition, double value, Sd.Color color)
{
    // 用于标记条件是否有效
    bool valid = true;

    // 根据不同的条件类型添加相应的条件格式
    switch (condition)
    {
        case ValueCondition.Greater:
            // 添加"大于"条件格式
            this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlGreater, Formula1: value);
            break;
        case ValueCondition.GreaterEqual:
            // 添加"大于等于"条件格式
            this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlGreaterEqual, Formula1: value);
            break;
        // ... 其他条件类型的处理(省略)
        default:
            // 如果条件类型无效,将 valid 标记为 false
            valid = false;
            break;
    }

    // 如果条件有效,设置条件格式的背景颜色
    if (valid) this.ComObj.FormatConditions[this.ComObj.FormatConditions.Count].Interior.Color = color;
}
  1. AddSparkLine() 方法
public void AddSparkLine(ExRange placement, Sd.Color color, double weight)
{
    // 在指定位置添加迷你图
    // this.ToString() 返回当前 ExRange 对象的地址字符串,用作数据源范围
    XL.SparklineGroup spark = placement.ComObj.SparklineGroups.Add(XL.XlSparkType.xlSparkLine, this.ToString());
    
    // 设置迷你图的颜色
    spark.SeriesColor.Color = color;
    
    // 设置迷你图线条的粗细
    spark.LineWeight = weight;
}

这些详细的中文注释解释了每个方法的功能、参数的作用以及实现的细节。它们有助于理解代码的逻辑和 Excel COM 对象的使用方式。这种详细的注释对于学习和维护代码都非常有帮助,特别是对于那些不熟悉 Excel 自动化或 COM 编程的开发者来说。

ExRange 类中的其他重要方法,并解释它们的功能。

  1. ReadFillColors() 方法
public GH_Structure<GH_ObjectWrapper> ReadFillColors()
{
    // 创建一个新的 GH_Structure 来存储结果
    GH_Structure<GH_ObjectWrapper> ranges = new GH_Structure<GH_ObjectWrapper>();

    // 获取范围的起始和结束列行
    int Ax = this.Start.Column;
    int Ay = this.Start.Row;
    int Bx = this.Extent.Column;
    int By = this.Extent.Row;

    int k = 0;
    // 遍历范围内的每一列
    for (int i = Ax; i < Bx+1; i++)
    {
        // 为每一列创建一个新的路径
        GH_Path path = new GH_Path(k);
        // 遍历该列中的每一行
        for (int j = Ay; j < By+1; j++)
        {
            // 创建单元格地址
            string address = new ExCell(i, j).Address;
            // 为每个单元格创建一个新的 ExRange 对象
            ExRange rng = new ExRange(this.ComObj.Worksheet.Range[address,address]);
            // 将 ExRange 对象添加到结构中
            ranges.Append(new GH_ObjectWrapper(rng), path);
        }
        k++;
    }

    // 返回包含所有单元格 ExRange 对象的结构
    return ranges;
}
  1. GetMinPixel() 和 GetMaxPixel() 方法
public Rg.Point3d GetMinPixel()
{
    // 返回范围左上角的像素坐标
    // ComObj.Left 和 ComObj.Top 分别表示左边距和上边距
    return new Rg.Point3d(this.ComObj.Left, this.ComObj.Top, 0);
}

public Rg.Point3d GetMaxPixel()
{
    // 获取范围的左边距和上边距
    double X = this.ComObj.Left;
    double Y = this.ComObj.Top;
    // 获取范围的宽度和高度
    double W = this.ComObj.Width;
    double H = this.ComObj.Height;
    // 返回范围右下角的像素坐标
    return new Rg.Point3d(X + W, Y + H, 0);
}
  1. ClearContent() 和 ClearFormat() 方法
public void ClearContent()
{
    // 清除范围内的所有内容,但保留格式
    this.ComObj.ClearContents();
}

public void ClearFormat()
{
    // 清除范围内的所有格式,但保留内容
    this.ComObj.ClearFormats();
}
  1. MergeCells() 和 UnMergeCells() 方法
public void MergeCells()
{
    // 合并选定范围内的所有单元格
    this.ComObj.MergeCells = true;
}

public void UnMergeCells()
{
    // 取消合并选定范围内的单元格
    this.ComObj.UnMerge();
}
  1. SetBorder() 方法
public void SetBorder(Sd.Color color, BorderWeight weight, LineType type, HorizontalBorder horizontal, VerticalBorder vertical)
{
    // 获取范围的边框对象
    XL.Borders borders = this.ComObj.Borders;
    XL.Border left, right, top, bottom, betweenHorizontal, betweenVertical;

    // 处理水平边框
    switch (horizontal)
    {
        case HorizontalBorder.All:
            // 设置底部边框
            bottom = borders[XL.XlBordersIndex.xlEdgeBottom];
            bottom.Weight = weight.ToExcel();
            bottom.Color = color;
            bottom.LineStyle = type.ToExcel();

            // 设置水平内部边框
            betweenHorizontal = borders[XL.XlBordersIndex.xlInsideHorizontal];
            betweenHorizontal.Weight = weight.ToExcel();
            betweenHorizontal.Color = color;
            betweenHorizontal.LineStyle = type.ToExcel();

            // 设置顶部边框
            top = borders[XL.XlBordersIndex.xlEdgeTop];
            top.Weight = weight.ToExcel();
            top.Color = color;
            top.LineStyle = type.ToExcel();
            break;
        // ... (其他情况的处理)
    }

    // 处理垂直边框
    switch (vertical)
    {
        case VerticalBorder.All:
            // 设置左侧边框
            left = borders[XL.XlBordersIndex.xlEdgeLeft];
            left.Weight = weight.ToExcel();
            left.Color = color;
            left.LineStyle = type.ToExcel();

            // 设置垂直内部边框
            betweenVertical = borders[XL.XlBordersIndex.xlInsideVertical];
            betweenVertical.Weight = weight.ToExcel();
            betweenVertical.Color = color;
            betweenVertical.LineStyle = type.ToExcel();

            // 设置右侧边框
            right = borders[XL.XlBordersIndex.xlEdgeRight];
            right.Weight = weight.ToExcel();
            right.Color = color;
            right.LineStyle = type.ToExcel();
            break;
        // ... (其他情况的处理)
    }
}
  1. AddSparkColumn() 方法
public void AddSparkColumn(ExRange placement, Sd.Color color)
{
    // 在指定位置添加柱状迷你图
    // this.ToString() 返回当前范围的地址,作为迷你图的数据源
    XL.SparklineGroup spark = placement.ComObj.SparklineGroups.Add(XL.XlSparkType.xlSparkLine, this.ToString());
    // 设置迷你图类型为柱状图
    spark.Type = XL.XlSparkType.xlSparkColumn;
    // 设置迷你图的颜色
    spark.SeriesColor.Color = color;
}
  1. AddConditionalScale() 方法
public void AddConditionalScale(Sd.Color first, Sd.Color second, Sd.Color third, double mid = 0.5)
{
    // 确保 mid 值在 0 到 1 之间
    mid = Math.Min(mid, 1.0);
    mid = Math.Max(mid, 0.0);
    // 添加一个三色刻度的条件格式
    XL.ColorScale scale = this.ComObj.FormatConditions.AddColorScale(3);

    // 设置最低值的颜色
    scale.ColorScaleCriteria[1].Type = XL.XlConditionValueTypes.xlConditionValueLowestValue;
    scale.ColorScaleCriteria[1].FormatColor.Color = first;

    // 设置中间值的颜色和位置
    scale.ColorScaleCriteria[2].Type = XL.XlConditionValueTypes.xlConditionValuePercentile;
    scale.ColorScaleCriteria[2].Value = (int)(mid*100.0);
    scale.ColorScaleCriteria[2].FormatColor.Color = second;

    // 设置最高值的颜色
    scale.ColorScaleCriteria[3].Type = XL.XlConditionValueTypes.xlConditionValueHighestValue;
    scale.ColorScaleCriteria[3].FormatColor.Color = third;
}

这些详细的注释解释了每个方法的功能、参数的作用以及实现的细节。它们有助于理解代码的逻辑和 Excel COM 对象的使用方式。这种详细的注释对于学习和维护代码都非常有帮助,特别是对于那些不熟悉 Excel 自动化或 COM 编程的开发者来说。

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Sd = System.Drawing;

using Rg = Rhino.Geometry;

using XL = Microsoft.Office.Interop.Excel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

namespace Bumblebee
{
    public class ExRange
    {
        #region members

        protected ExCell start = new ExCell();
        protected ExCell extent = new ExCell();

        public XL.Range ComObj = null;

        #endregion

        #region constructors

        public ExRange()
        {
        }

        public ExRange(XL.Range comObj)
        {
            this.ComObj = comObj;
            GetFirstCell();
            GetLastCell();
        }

        public ExRange(ExRange range)
        {
            this.ComObj = range.ComObj;
            this.start = range.Start;
            this.extent = range.Extent;
        }

        #endregion

        #region properties

        public virtual ExCell Start
        {
            get { return new ExCell(start); }
            set { start = new ExCell(value); }
        }

        public virtual ExCell Extent
        {
            get { return new ExCell(extent); }
            set { extent = new ExCell(value); }
        }

        public virtual ExWorksheet Worksheet
        {
            get { return new ExWorksheet(this.ComObj.Worksheet); }
        }

        public virtual ExWorkbook Workbook
        {
            get { return new ExWorkbook(this.Worksheet.Workbook); }
        }

        public virtual ExApp ParentApp
        {
            get { return new ExApp(this.ComObj.Application); }
        }

        public virtual bool IsSingle
        {
            get { return ((start.Column == extent.Column) & (start.Row == extent.Row)); }
        }

        public virtual string FontFamily
        {
            get { return this.ComObj.Font.Name; }
            set { this.ComObj.Font.Name = value; }
        }

        public virtual double FontSize
        {
            get { return this.ComObj.Font.Size; }
            set { this.ComObj.Font.Size = value; }
        }

        public virtual Sd.Color FontColor
        {
            get { return Sd.ColorTranslator.FromOle((int)this.ComObj.Font.Color); }
            set { this.ComObj.Font.Color = value; }
        }

        public virtual Justification FontJustification
        {
            set{
                this.ComObj.HorizontalAlignment = value.ToExcelHalign();
                this.ComObj.VerticalAlignment = value.ToExcelValign();
            }
            get
            {
                int align = 0;
                switch (this.ComObj.VerticalAlignment)
                {
                    case XL.XlVAlign.xlVAlignCenter:
                        align = 3;
                        break;
                    case XL.XlVAlign.xlVAlignTop:
                        align = 6;
                        break;
                }

                switch (this.ComObj.VerticalAlignment)
                {
                    case XL.XlHAlign.xlHAlignLeft:
                    case XL.XlHAlign.xlHAlignGeneral:
                        break;
                    case XL.XlHAlign.xlHAlignRight:
                        align += 2;
                        break;
                    default:
                        align += 1;
                        break;
                }
                return (Justification)align;
            }
        }

        public bool Bold
        {
            get { return this.ComObj.Font.Bold; }
            set { this.ComObj.Font.Bold = value; }
        }

        public bool Italic
        {
            get { return this.ComObj.Font.Italic; }
            set { this.ComObj.Font.Italic = value; }
        }

        public virtual BorderWeight Weight
        {
            get { return ((XL.XlBorderWeight)this.ComObj.Borders.Weight).ToBB(); }
        }

        public virtual LineType LineType
        {
            get { return ((XL.XlLineStyle)this.ComObj.Borders.LineStyle).ToBB(); }
        }

        public virtual Sd.Color BorderColor
        {
            get { return Sd.ColorTranslator.FromOle((int)this.ComObj.Borders.Color); }
        }

        public virtual Sd.Color Background
        {
            get { return Sd.ColorTranslator.FromOle((int)this.ComObj.Interior.Color); }
            set { this.ComObj.Interior.Color = value; }
        }

        public virtual int Width
        {
            get { return this.ComObj.Columns.ColumnWidth; }
            set { this.ComObj.Columns.ColumnWidth = value; }
        }

        public virtual int Height
        {
            get { return this.ComObj.Rows.RowHeight; }
            set { this.ComObj.Rows.RowHeight = value; }
        }

        #endregion

        #region methods

        #region data

        public GH_Structure<GH_String> ReadData()
        {
            GH_Structure<GH_String> data = new GH_Structure<GH_String>();

            int[] L = GetRangeArray();

            System.Array values = (System.Array)this.ComObj.Value2;

            if (values != null)
            {
                for (int i = 1; i < (L[3] - L[1] + 2); i++)
                {
                    GH_Path path = new GH_Path(i);
                    for (int j = 1; j < (L[2] - L[0] + 2); j++)
                    {
                        string val = string.Empty;
                        if (values.GetValue(i, j) != null) val = values.GetValue(i, j).ToString();
                        data.Append(new GH_String(val), path);
                    }
                }
            }

            return data;
        }

        #endregion

        #region Graphics

        public GH_Structure<GH_ObjectWrapper> ReadFillColors()
        {
            GH_Structure<GH_ObjectWrapper> ranges = new GH_Structure<GH_ObjectWrapper>();

            int Ax = this.Start.Column;
            int Ay = this.Start.Row;
            int Bx = this.Extent.Column;
            int By = this.Extent.Row;

            int k = 0;
            for (int i = Ax; i < Bx+1; i++)
                {
                    GH_Path path = new GH_Path(k);
                    for (int j = Ay; j < By+1; j++)
                    {
                    string address = new ExCell(i, j).Address;
                    ExRange rng = new ExRange(this.ComObj.Worksheet.Range[address,address]);
                    ranges.Append(new GH_ObjectWrapper(rng), path);
                    }
                k++;
                }

            return ranges;
        }

        #endregion

        #region Geometry

        protected int[] GetRangeArray()
        {
            return new int[] { this.Start.Column, this.Start.Row, this.Extent.Column, this.Extent.Row };
        }

        public Rg.Point3d GetMinPixel()
        {
            return new Rg.Point3d(this.ComObj.Left, this.ComObj.Top, 0);
        }

        public Rg.Point3d GetMaxPixel()
        {
            double X = this.ComObj.Left;
            double Y = this.ComObj.Top;

            double W = this.ComObj.Width;
            double H = this.ComObj.Height;

            return new Rg.Point3d(X + W, Y + H, 0);
        }

        #endregion

        #region Extract

        protected void GetFirstCell()
        {
            start= new ExCell(this.ComObj.Column, this.ComObj.Row, false, false);
        }

        protected ExRange GetFirstRange()
        {
            ExWorksheet sheet = new ExWorksheet(this.ComObj.Worksheet);
            return new ExRange(sheet.GetRange(start,start));
        }

        protected void GetLastCell()
        {
            extent= new ExCell(this.ComObj.Columns[this.ComObj.Columns.Count].Column, this.ComObj.Rows[this.ComObj.Rows.Count].Row, false, false);
        }

        public void ClearContent()
        {
            this.ComObj.ClearContents();
        }

        public void MergeCells()
        {
            this.ComObj.MergeCells = true;
        }

        public void UnMergeCells()
        {
            this.ComObj.UnMerge();
        }

        #endregion

        #region graphics

        public void ClearFormat()
        {
            this.ComObj.ClearFormats();
        }

        public void SetFont(string name, double size, Sd.Color color, Justification justification, bool bold, bool italic)
        {
            XL.Font font = this.ComObj.Font;

            font.Name = name;
            font.Size = size;
            font.Color = color;
            font.Bold = bold;
            font.Italic = italic;

            this.ComObj.HorizontalAlignment = justification.ToExcelHalign();
            this.ComObj.VerticalAlignment = justification.ToExcelValign();
        }

        public void SetBorder(Sd.Color color, BorderWeight weight, LineType type, HorizontalBorder horizontal, VerticalBorder vertical)
        {
            XL.Borders borders = this.ComObj.Borders;
            XL.Border left, right, top, bottom, betweenHorizontal, betweenVertical;

            switch (horizontal)
            {
                case HorizontalBorder.All:
                    bottom = borders[XL.XlBordersIndex.xlEdgeBottom];
                    bottom.Weight = weight.ToExcel();
                    bottom.Color = color;
                    bottom.LineStyle = type.ToExcel();

                    betweenHorizontal = borders[XL.XlBordersIndex.xlInsideHorizontal];
                    betweenHorizontal.Weight = weight.ToExcel();
                    betweenHorizontal.Color = color;
                    betweenHorizontal.LineStyle = type.ToExcel();

                    top = borders[XL.XlBordersIndex.xlEdgeTop];
                    top.Weight = weight.ToExcel();
                    top.Color = color;
                    top.LineStyle = type.ToExcel();
                    break;
                case HorizontalBorder.Between:
                    betweenHorizontal = borders[XL.XlBordersIndex.xlInsideHorizontal];
                    betweenHorizontal.Weight = weight.ToExcel();
                    betweenHorizontal.Color = color;
                    betweenHorizontal.LineStyle = type.ToExcel();
                    break;
                case HorizontalBorder.Both:
                    bottom = borders[XL.XlBordersIndex.xlEdgeBottom];
                    bottom.Weight = weight.ToExcel();
                    bottom.Color = color;
                    bottom.LineStyle = type.ToExcel();

                    top = borders[XL.XlBordersIndex.xlEdgeTop];
                    top.Weight = weight.ToExcel();
                    top.Color = color;
                    top.LineStyle = type.ToExcel();
                    break;
                case HorizontalBorder.Bottom:
                    bottom = borders[XL.XlBordersIndex.xlEdgeBottom];
                    bottom.Weight = weight.ToExcel();
                    bottom.Color = color;
                    bottom.LineStyle = type.ToExcel();
                    break;
                case HorizontalBorder.Top:
                    top = borders[XL.XlBordersIndex.xlEdgeTop];
                    top.Weight = weight.ToExcel();
                    top.Color = color;
                    top.LineStyle = type.ToExcel();
                    break;
            }

            switch (vertical)
            {
                case VerticalBorder.All:
                    left = borders[XL.XlBordersIndex.xlEdgeLeft];
                    left.Weight = weight.ToExcel();
                    left.Color = color;
                    left.LineStyle = type.ToExcel();

                    betweenVertical = borders[XL.XlBordersIndex.xlInsideVertical];
                    betweenVertical.Weight = weight.ToExcel();
                    betweenVertical.Color = color;
                    betweenVertical.LineStyle = type.ToExcel();

                    right = borders[XL.XlBordersIndex.xlEdgeRight];
                    right.Weight = weight.ToExcel();
                    right.Color = color;
                    right.LineStyle = type.ToExcel();
                    break;
                case VerticalBorder.Between:
                    betweenVertical = borders[XL.XlBordersIndex.xlInsideVertical];
                    betweenVertical.Weight = weight.ToExcel();
                    betweenVertical.Color = color;
                    betweenVertical.LineStyle = type.ToExcel();
                    break;
                case VerticalBorder.Both:
                    left = borders[XL.XlBordersIndex.xlEdgeLeft];
                    left.Weight = weight.ToExcel();
                    left.Color = color;
                    left.LineStyle = type.ToExcel();

                    right = borders[XL.XlBordersIndex.xlEdgeRight];
                    right.Weight = weight.ToExcel();
                    right.Color = color;
                    right.LineStyle = type.ToExcel();
                    break;
                case VerticalBorder.Left:
                    left = borders[XL.XlBordersIndex.xlEdgeLeft];
                    left.Weight = weight.ToExcel();
                    left.Color = color;
                    left.LineStyle = type.ToExcel();
                    break;
                case VerticalBorder.Right:
                    right = borders[XL.XlBordersIndex.xlEdgeRight];
                    right.Weight = weight.ToExcel();
                    right.Color = color;
                    right.LineStyle = type.ToExcel();
                    break;
            }

        }

        #endregion

        #region sparklines

        public void AddSparkLine(ExRange placement, Sd.Color color, double weight)
        {
            XL.SparklineGroup spark = placement.ComObj.SparklineGroups.Add(XL.XlSparkType.xlSparkLine, this.ToString());
            spark.SeriesColor.Color= color;
            spark.LineWeight = weight;
        }

        public void AddSparkColumn(ExRange placement, Sd.Color color)
        {
            XL.SparklineGroup spark = placement.ComObj.SparklineGroups.Add(XL.XlSparkType.xlSparkLine, this.ToString());
            spark.Type = XL.XlSparkType.xlSparkColumn;
            spark.SeriesColor.Color = color;
        }

        #endregion

        #region conditional

        public void ClearConditions()
        {
            this.ComObj.FormatConditions.Delete();
        }

        public void AddConditionalValue(ValueCondition condition, double value, Sd.Color color)
        {
            bool valid = true;

            switch (condition)
            {
                case ValueCondition.Greater:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlGreater, Formula1: value);
                    break;
                case ValueCondition.GreaterEqual:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlGreaterEqual, Formula1: value);
                    break;
                case ValueCondition.Less:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlLess, Formula1: value);
                    break;
                case ValueCondition.LessEqual:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlLessEqual, Formula1: value);
                    break;
                case ValueCondition.Equal:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlEqual, Formula1: value);
                    break;
                case ValueCondition.NotEqual:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlNotEqual, Formula1: value);
                    break;
                default:
                    valid = false;
                    break;
            }
            if (valid) this.ComObj.FormatConditions[this.ComObj.FormatConditions.Count].Interior.Color = color;

        }

        public void AddConditionalAverage(AverageCondition condition, Sd.Color color)
        {
            bool valid = true;

            switch (condition)
            {
                case AverageCondition.AboveAverage:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlAboveAverageCondition, XL.XlAboveBelow.xlAboveAverage);
                    break;
                case AverageCondition.AboveDeviation:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlAboveAverageCondition, XL.XlAboveBelow.xlAboveStdDev);
                    break;
                case AverageCondition.AboveEqualAverage:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlAboveAverageCondition, XL.XlAboveBelow.xlEqualAboveAverage);
                    break;
                case AverageCondition.BelowAverage:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlAboveAverageCondition, XL.XlAboveBelow.xlBelowAverage);
                    break;
                case AverageCondition.BelowDeviation:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlAboveAverageCondition, XL.XlAboveBelow.xlBelowStdDev);
                    break;
                case AverageCondition.BelowEqualAverage:
                    this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlAboveAverageCondition, XL.XlAboveBelow.xlEqualBelowAverage);
                    break;
                default:
                    valid = false;
                    break;
            }
            if (valid) this.ComObj.FormatConditions[this.ComObj.FormatConditions.Count].Interior.Color = color;

        }

        public void AddConditionalBetween(double low, double high, Sd.Color color, bool flip=false)
        {
            if (flip)
            {
                this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlNotBetween, Formula1: low,Formula2:high);
            }
            else
            {
                this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlCellValue, XL.XlFormatConditionOperator.xlBetween, Formula1: low, Formula2: high);
            }
            this.ComObj.FormatConditions[this.ComObj.FormatConditions.Count].Interior.Color = color;
        }

        public void AddConditionalUnique(Sd.Color color, bool flip = false)
        {
            XL.UniqueValues unique = this.ComObj.FormatConditions.AddUniqueValues();
            if (flip)
            {
                unique.DupeUnique = XL.XlDupeUnique.xlDuplicate;
            }
            else
            {
                unique.DupeUnique = XL.XlDupeUnique.xlUnique;
            }
            unique.Interior.Color = color;
        }

        public void AddConditionalTopCount(int count, Sd.Color color, bool flip = false)
        {
            XL.Top10 top = this.ComObj.FormatConditions.AddTop10();
            top.Percent = false;
            top.Rank = count;
            if (flip)
            {
                top.TopBottom = XL.XlTopBottom.xlTop10Bottom;
            }
            else
            {
                top.TopBottom = XL.XlTopBottom.xlTop10Top;
            }
            top.Interior.Color = color;
        }

        public void AddConditionalTopPercent(double percent, Sd.Color color, bool flip = false)
        {
            percent = Math.Min(percent, 1.0);
            percent = Math.Max(percent, 0.0);
            XL.Top10 top = this.ComObj.FormatConditions.AddTop10();
            top.Percent = true;
            top.Rank = (int)(percent*100.0);
            if (flip)
            {
                top.TopBottom = XL.XlTopBottom.xlTop10Bottom;
            }
            else
            {
                top.TopBottom = XL.XlTopBottom.xlTop10Top;
            }
            top.Interior.Color = color;
        }

        public void AddConditionalBlanks(Sd.Color color, bool flip = false)
        {

            if (flip)
            {
                this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlNoBlanksCondition);
            }
            else
            {
                this.ComObj.FormatConditions.Add(XL.XlFormatConditionType.xlBlanksCondition);
            }
            this.ComObj.FormatConditions[this.ComObj.FormatConditions.Count].Interior.Color = color;
        }

        public void AddConditionalScale(Sd.Color first, Sd.Color second, Sd.Color third, double mid = 0.5)
        {
            mid = Math.Min(mid, 1.0);
            mid = Math.Max(mid, 0.0);
            XL.ColorScale scale = this.ComObj.FormatConditions.AddColorScale(3);

            scale.ColorScaleCriteria[1].Type = XL.XlConditionValueTypes.xlConditionValueLowestValue;
            scale.ColorScaleCriteria[1].FormatColor.Color = first;

            scale.ColorScaleCriteria[2].Type = XL.XlConditionValueTypes.xlConditionValuePercentile;
            scale.ColorScaleCriteria[2].Value = (int)(mid*100.0);
            scale.ColorScaleCriteria[2].FormatColor.Color = second;

            scale.ColorScaleCriteria[3].Type = XL.XlConditionValueTypes.xlConditionValueHighestValue;
            scale.ColorScaleCriteria[3].FormatColor.Color = third;
        }

        public void AddConditionalScale(Sd.Color first, Sd.Color second)
        {
            XL.ColorScale scale = this.ComObj.FormatConditions.AddColorScale(2);
            int count = this.ComObj.FormatConditions.Count;

            scale.ColorScaleCriteria[1].Type = XL.XlConditionValueTypes.xlConditionValueLowestValue;
            scale.ColorScaleCriteria[1].FormatColor.Color = first;

            scale.ColorScaleCriteria[2].Type = XL.XlConditionValueTypes.xlConditionValueHighestValue;
            scale.ColorScaleCriteria[2].FormatColor.Color = second;
        }

        public void AddConditionalBar(Sd.Color color, bool gradient)
        {
            XL.Databar bar = this.ComObj.FormatConditions.AddDatabar();

            bar.BarColor.Color = color;
            if (gradient)
            {
                bar.BarFillType = XL.XlDataBarFillType.xlDataBarFillGradient;
            }
            else
            {
                bar.BarFillType = XL.XlDataBarFillType.xlDataBarFillSolid;
            }

        }

        #endregion

        #endregion

        #region overrides

        public override string ToString()
        {
            return this.start.Address+":"+this.extent.Address;
        }

        #endregion

    }
}
  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hmywillstronger

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

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

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

打赏作者

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

抵扣说明:

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

余额充值