[英]How can an Excel XLL tell if a UDF is being called from the formula bar?
可以通过挂钩 SheetChange 事件来复制 TODAY() 行为的这一方面 - 这只会在输入公式时触发,而不是在重新计算时触发(即使使用 Ctrl-Alt-F9 也是如此)。 我不知道 TODAY() 是否使用这种方法,或者它可能使用了用户定义函数不可用的东西。 此外,复制 TODAY() 将需要解析公式并应用一些逻辑来确定它是否应用日期格式,这会有点棘手,但有一些 Excel 公式解析器在那里。
在xlOil (免责声明:我写的)中,它可以实现为:
XLO_FUNC_START(testToday())
{
CallerInfo caller;
if (!caller.fullSheetName().empty()) // Check caller is worksheet
{
// Add a SheetChange handler
auto handle = xloil::Event::SheetChange().bind(
[=](const wchar_t* wsName, const Range& target)
{
// Need to check range here as well to avoid being triggered by another sheet change
if (wsName == caller.sheetName())
excelApp().Range[caller.writeAddress().c_str()]->NumberFormat = L"dd-mm-yyyy";
}
);
// Wait 0.5 sec then unbind the event handler by queuing a window message with Excel
auto milliSecsDelay = 500;
excelPost([=]() mutable
{
handle.reset(); // Removes the SheetChange handler
}, QueueType::WINDOW, 0, 0, milliSecsDelay);
}
// This is how to get the current date in C++
std::tm buf;
auto now = std::time(0);
localtime_s(&buf, &now);
return returnValue(buf); // xlOil will convert the std::tm to an Excel date
}
XLO_FUNC_END(testToday);
这只是一个概念验证,可以进行一些整理,例如应该检查范围,如果多次调用 function,开销可能会非常大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.