gpt4 book ai didi

excel - Delphi 和 Excel.FormatConditions

转载 作者:行者123 更新时间:2023-12-03 15:29:07 24 4
gpt4 key购买 nike

我在使用 Excel 2010 的早期绑定(bind)从 Delphi XE2 设置条件格式时遇到问题

我尝试重现的宏如下:

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=6"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

尽我所能,我似乎无法访问 Selction.FormatConditions(1) 的等效项来工作

我达到的最接近的是以下代码:

XR := Xlapp.Range(...) 
XR.FormatConditions.Delete;
XR.FormatConditions.Add(xlCellValue, xlGreater, '=6', EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);

这有效。当我尝试定义颜色时遇到问题

FC := XR.FormatConditions[1];
FC.SetFirstPriority;
with FC.Interior do
begin
PatternColorIndex := xlAutomatic;
ThemeColor := xlThemeColorAccent6;
end;

然而,这一直告诉我 XR.FormatConditions(1) 是 IDispatch,因此与 FormatCondition 分配不兼容

我做错了什么?

最佳答案

您需要使用Selection作为ExcelRange。 Excel XP 还要求第二个和第三个参数为 OleVariant,因此这应该可以工作(无论如何,它可以编译):

var
Sel: ExcelRange;
Op, Formula: OleVariant;
Condition: FormatCondition;
begin
Sel := ExcelApplication1.Selection[1] as ExcelRange;
Op := xlGreater;
Formula := '=6';
Sel.FormatConditions.Add(xlCellValue, Op, Formula, EmptyParam);
Condition := Sel.FormatConditions[1] as FormatCondition;
Condition.Interior.PatternColorIndex := xlAutomatic;
// Do whatever else
end;

关于excel - Delphi 和 Excel.FormatConditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com