如何使用VBA从Excel中的条件格式获取背景颜色(How to get the background color from a Conditional Formatting in Excel using VBA)
我想在我的VBA脚本中使用Excel中的条件格式规则获取单元背景颜色。 我意识到使用
Range.Interior.Color
属性不具有由Excel应用的条件格式化功能导致的颜色。我做了一些研究, 在这里发现了很长的路,它编译和运行,但我没有得到指定的颜色[我总是得到(255,255,255)]
我正在使用Excel 2016,我想知道是否有一种更简单的方法来使用某些内置VBA函数或使用任何其他Excel技巧获取此信息。
I would like to obtain the cell background color assigned using a conditional formatting rule in Excel in my VBA script. I realized that using
Range.Interior.Color
property doesn't have the color resulting of an applied conditional formatting feature from Excel.I did some research and I found this long way here, it compiles and runs but I don't get the assigned color [I get always (255,255,255)]
I am using Excel 2016 and I am wondering if there is a simpler way to obtain this information using some built-in VBA function or using any other excel trick.
原文:https://stackoverflow.com/questions/45122782
最满意答案
像这样? 这隐含地将一个装饰器添加到你的方法中(如果你愿意的话,你也可以基于此做一个明确的装饰器):
class Foo(object): def __getattribute__(self,name): attr = object.__getattribute__(self, name) if hasattr(attr, '__call__'): def newfunc(*args, **kwargs): print('before calling %s' %attr.__name__) result = attr(*args, **kwargs) print('done calling %s' %attr.__name__) return result return newfunc else: return attr
当你现在尝试像这样的东西:
class Bar(Foo): def myFunc(self, data): print("myFunc: %s"% data) bar = Bar() bar.myFunc(5)
你会得到:
before calling myFunc myFunc: 5 done calling myFunc
Something like this? This implictly adds a decorator to your method (you can also make an explicit decorator based on this if you prefer that):
class Foo(object): def __getattribute__(self,name): attr = object.__getattribute__(self, name) if hasattr(attr, '__call__'): def newfunc(*args, **kwargs): print('before calling %s' %attr.__name__) result = attr(*args, **kwargs) print('done calling %s' %attr.__name__) return result return newfunc else: return attr
when you now try something like:
class Bar(Foo): def myFunc(self, data): print("myFunc: %s"% data) bar = Bar() bar.myFunc(5)
You'll get:
before calling myFunc myFunc: 5 done calling myFunc
相关问答
更多-
在Python中调用拦截方法(Intercept method calls in Python)[2022-04-30]
像这样? 这隐含地将一个装饰器添加到你的方法中(如果你愿意的话,你也可以基于此做一个明确的装饰器): class Foo(object): def __getattribute__(self,name): attr = object.__getattribute__(self, name) if hasattr(attr, '__call__'): def newfunc(*args, **kwargs): pr ... -
链接方法调用Python中的缩进样式[duplicate](Chained method calls indentation style in Python [duplicate])[2023-06-09]
这是一个行连续字符优于打开括号的情况。 ShortName.objects.distinct() \ .filter().values() # looks better 随着方法名称越来越长,方法开始引用参数,这种风格的需求变得越来越明显: return some_collection.get_objects(locator=l5) \ .get_distinct(case_insensitive=True) \ ... -
只需在完成旧的计时器后创建一个新计时器。 import requests import threading def check_status(url): status = requests.get(url) if status.status_code != 200: print('faulty') threading.Timer(2.0, check_status, args=('https://www.google.com',)).start() def m ...
-
模拟方法在Python中调用(Mocking Method Calls In Python)[2021-09-23]
您不能覆盖函数调用 ,但可以覆盖函数本身。 从文档 : >>> from unittest.mock import MagicMock >>> thing = ProductionClass() >>> thing.method = MagicMock(return_value=3) >>> thing.method(3, 4, 5, key='value') 3 >>> thing.method.assert_called_with(3, 4, 5, key='value') 所以在你的情况下: ope ... -
您可以使client私有并强制子类在Func或Action访问它。 然后你可以在逻辑之前/之后添加你: abstract class CommunicationChannel
: IDisposable where Client : class, IDisposable { private Client client; protected TResult WithClient (Func f) { ... -
我能看到这项工作的唯一方法是让Af()不使用self.f()而是使用Af(self)代替。 更好的设计是让Af()将递归调用委托给一个单独的方法: class A(object): def __init__(self): self.foo = None def f(self): self._f_recursive() def _f_recursive(self): if not self.foo: print ...
-
是的,Boo有IQuackFu 。 基本上,您实现了IQuackFu ,它有三种方法: QuackGet :获取属性值时调用 设置属性值时会调用QuackSet QuackInvoke :调用方法时调用 这是一个例子 。 Yes, Boo has IQuackFu. Basically, you implement IQuackFu, which has three methods: QuackGet: gets called when you get a property value QuackSet g ...
-
你想断言它被调用了多少次,所以使用call_count并使用assert_equal (来自unittest或你正在使用的测试框架中的其他等价物)来验证它等于你期望它被调用的次数: assert_equal(mock.call.method_to_be_mocked.call_count, 2) You want to assert on how many times it was called, so use call_count and use assert_equal (from unittest ...
-
可能解决方案1: 包装库并使用ReSharper之类的工具查找库的所有用法并替换为包装类。 您还可以利用这个机会来清理第三方库的可能糟糕的界面。 可能的方案2: 虽然TypeMock通常用作测试工具,但它允许您模拟所有内容。 因为它将自身注入代码分析器,所以你可以模拟的东西包括类的私有和静态成员。 作为奖励,任何被覆盖的方法都不需要是虚拟的,因此您可以通过这种方式拦截调用。 我的推荐 我建议你使用解决方案1.包装器很容易理解,它将为你提供一个真正改进代码的好机会。 我甚至建议您将第三方库包装为一般规则。 P ...
-
使用PostSharp拦截对Silverlight对象的调用?(Using PostSharp to intercept calls to Silverlight objects?)[2022-04-23]
目前的PostSharp版本无法做到这一点。 PostSharp通过在CLR加载之前转换程序集来工作。 现在,为了做到这一点,必须做两件事: 必须将组件装入CLR; 你只得到一枪,你必须在这一点上。 转换阶段完成后,您无法进行任何其他修改。 这意味着您无法在运行时修改程序集。 最新版本1.5 CTP 3 消除了这两个限制中的第一个 ,但它是第二个真正的问题。 然而,这是一个非常需要的功能 ,所以请保持眼睛剥落: 用户经常询问是否可以在运行时使用PostSharp,因此在编译时不必知道方面。 部署后更改方面确 ...