扩展¶
用自己的代码,比如UDF或者RunPython宏,来扩展xlwings加载项是很容易的。用不着最终用户自己编写和导入函数,就可以进行部署。只需要给xlwings加载项增加一个包含相关代码的VBA模块就行了。
不用设置引用就可以在每个工作簿中使用UDF扩展。
In-Excel SQL¶
xlwings加载项附带了内置的扩展,增加了对Excel内SQL(in-Excel SQL)语法(sqlite语法分支)的支持:
=sql(SQL Statement, [alias a], table a, [alias b], table b, ...)

If there are no aliases provided, tables are aliased automatically as a
, b
, c
, etc. as the screenshot shows. Since v0.33.5 you can, however, provide your own alias/table name like so:
=sql(
"SELECT customers.name, o.product, o.amount
FROM customers
INNER JOIN b ON customers.customer_id = o.customer_id",
"customers", A1:D4,
"o", Orders[#All]
)
This is, you need to provide the table name/alias first before providing the range reference as the next argument, similar to how the =LET
formula works in Excel.
As this extension uses UDFs, it's only available on Windows right now. This formula can be used on macOS and Excel on the web though via xlwings Server.