我正在编写Excel加载项以及我需要计算一些摘要统计信息的一个函数。这些应作为名称添加到整个Workbook
或单个Worksheet
。现在我有两种方法可以完成每件事。但是,两种方法看起来几乎完全相同,区别在于第一种方法将名称添加到工作簿,第二种方法将工作表添加到工作表中。
有没有办法让这个方法更通用,以便根据传递的参数,我可以检查提供的工作表是否为null
,如果是,则添加到整个工作簿?
/// <summary>
/// Generate dynamic summary statistics as <see cref="Name"/>s in the specified <see cref="Workbook"/>.
/// </summary>
/// <param name="range">The <see cref="Range"/> of the data that needs summary statistics.</param>
/// <param name="doCalculate">Defines which variables in the summary have to be calculated.</param>
public DynamicSummaryStatistics(Range range, SummaryStatisticsBool doCalculate)
{
var book = Globals.ThisAddIn.Application.ActiveWorkbook;
var name = ((Name)range.Name).Name;
if (doCalculate.Mean) Mean = book.Names.Add(name + "_MEAN", "=AVERAGE(" + name + ")");
if (doCalculate.Variance) Variance = book.Names.Add(name + "_VAR", "=VAR.S(" + name + ")");
if (doCalculate.StdDev) StdDev = book.Names.Add(name + "_STDEV", "=STDEV.S(" + name + ")");
if (doCalculate.Minimum) Minimum = book.Names.Add(name + "_MINIMUM", "=MIN(" + name + ")");
if (doCalculate.Quartile1) Quartile1 = book.Names.Add(name + "_QUARTILE1", "=QUARTILE.INC(" + name + ",1)");
if (doCalculate.Median) Median = book.Names.Add(name + "_MEDIAN", "=MEDIAN(" + name + ")");
if (doCalculate.Quartile3) Quartile3 = book.Names.Add(name + "_QUARTILE3", "=QUARTILE.INC(" + name + ",3)");
if (doCalculate.Maximum) Maximum = book.Names.Add(name + "_MAXIMUM", "=MAX(" + name + ")");
if (doCalculate.InterquartileRange) InterquartileRange = book.Names.Add(name + "_IQR", "=" + Quartile3.Name + "-" + Quartile1.Name);
if (doCalculate.Skewness) Skewness = book.Names.Add(name + "_SKEW", "=SKEW(" + name + ")");
if (doCalculate.Kurtosis) Kurtosis = book.Names.Add(name + "_KURT", "=KURT(" + name + ")");
if (doCalculate.MeanAbsDev) MeanAbsDev = book.Names.Add(name + "_AVEDEV", "=AVEDEV(" + name + ")");
if (doCalculate.Mode)
{
Mode = book.Names.Add(name + "_MODE", "=MODE.SNGL(" + name + ")");
try
{
Globals.ThisAddIn.Application.WorksheetFunction.Mode_Sngl(range);
HasMode = true;
}
catch
{
HasMode = false;
}
}
if (doCalculate.Range) Range = book.Names.Add(name + "_RANGE", "=" + Maximum.Name + "-" + Minimum.Name);
if (doCalculate.Count) Count = book.Names.Add(name + "_COUNT", "=COUNT(" + name + ")");
if (doCalculate.Sum) Sum = book.Names.Add(name + "_SUM", "=SUM(" + name + ")");
}
/// <summary>
/// Generate dynamic summary statistics as <see cref="Name"/>s in the specified <see cref="Worksheet"/>.
/// </summary>
/// <param name="sheet">The <see cref="Worksheet"/> on which the <see cref="Name"/>s should be generated.</param>
/// <param name="range">The <see cref="Range"/> of the data that needs summary statistics.</param>
/// <param name="doCalculate">Defines which variables in the summary have to be calculated.</param>
public DynamicSummaryStatistics(Worksheet sheet, Range range, SummaryStatisticsBool doCalculate)
{
var name = ((Name)range.Name).Name;
if (doCalculate.Mean) Mean = sheet.Names.Add(name + "_MEAN", "=AVERAGE(" + name + ")");
if (doCalculate.Variance) Variance = sheet.Names.Add(name + "_VAR", "=VAR.S(" + name + ")");
if (doCalculate.StdDev) StdDev = sheet.Names.Add(name + "_STDEV", "=STDEV.S(" + name + ")");
if (doCalculate.Minimum) Minimum = sheet.Names.Add(name + "_MINIMUM", "=MIN(" + name + ")");
if (doCalculate.Quartile1) Quartile1 = sheet.Names.Add(name + "_QUARTILE1", "=QUARTILE.INC(" + name + ",1)");
if (doCalculate.Median) Median = sheet.Names.Add(name + "_MEDIAN", "=MEDIAN(" + name + ")");
if (doCalculate.Quartile3) Quartile3 = sheet.Names.Add(name + "_QUARTILE3", "=QUARTILE.INC(" + name + ",3)");
if (doCalculate.Maximum) Maximum = sheet.Names.Add(name + "_MAXIMUM", "=MAX(" + name + ")");
if (doCalculate.InterquartileRange) InterquartileRange = sheet.Names.Add(name + "_IQR", "=" + Quartile3.Name + "-" + Quartile1.Name);
if (doCalculate.Skewness) Skewness = sheet.Names.Add(name + "_SKEW", "=SKEW(" + name + ")");
if (doCalculate.Kurtosis) Kurtosis = sheet.Names.Add(name + "_KURT", "=KURT(" + name + ")");
if (doCalculate.MeanAbsDev) MeanAbsDev = sheet.Names.Add(name + "_AVEDEV", "=AVEDEV(" + name + ")");
if (doCalculate.Mode)
{
Mode = sheet.Names.Add(name + "_MODE", "=MODE.SNGL(" + name + ")");
try
{
Globals.ThisAddIn.Application.WorksheetFunction.Mode_Sngl(range);
HasMode = true;
}
catch
{
HasMode = false;
}
}
if (doCalculate.Range) Range = sheet.Names.Add(name + "_RANGE", "=" + Maximum.Name + "-" + Minimum.Name);
if (doCalculate.Count) Count = sheet.Names.Add(name + "_COUNT", "=COUNT(" + name + ")");
if (doCalculate.Sum) Sum = sheet.Names.Add(name + "_SUM", "=SUM(" + name + ")");
}
答案 0 :(得分:2)
我没有试图访问Workbook
和Worksheet
通用,而是说提供Func<string, string, Name>
允许访问权限Workbook
或Worksheet
是一种更好的方法。
public DynamicSummaryStatistics(Range range, SummaryStatisticsBool doCalculate)
{
var functionToRun = (arg1, arg2) => Globals.ThisAddIn.Application.ActiveWorkbook.Names.Add(arg1, arg2);
this.ComputeDynamicSummaryStatistics(range, doCalculate, functionToRun);
}
public DynamicSummaryStatistics(Worksheet sheet, Range range, SummaryStatisticsBool doCalculate)
{
var functionToRun = (arg1, arg2) => sheet.Names.Add(arg1, arg2);
this.ComputeDynamicSummaryStatistics(range, doCalcualte, functionToRun);
}
private void ComputeDynamicSummaryStatistics(Range range, SummaryStatisticsBool doCalculate, Func<string, string, Name> functionToRun)
{
var name = ((Name)range.Name).Name;
if(doCalculate.Mean) Mean = functionToRun(name + "_MEAN", "=AVERAGE(" + name + ")");
if(doCalculate.Variance) Variance = functionToRun(name + "_VAR", "=VAR.S(" + name + ")");
// etc. etc.
}
所以我在函数ComputeDynamicSummaryStatistics
中每次调用functionToRun
时都会说我根据调用的构造函数调用sheet.Names.Add(...)
或Globals.ThisAddIn.Application.ActiveWorkbook.Names.Add(...)
。
访问Workbook
或Worksheet
对于该方法完全不了解,现在关注的是提供要添加的名称。