Python 如何从xlwings调用Excel函数
如何直接从xlwings调用Excel函数(如Python 如何从xlwings调用Excel函数,python,excel,vba,xlwings,Python,Excel,Vba,Xlwings,如何直接从xlwings调用Excel函数(如sum、average、product和text) 临时解决办法: 我能想到的唯一方法是制作一本helper Excel手册,并向xlwings提供以下UDF样板文件: 'Some extreme workarounds to get excel-like functions working ~95% 'This function is partly generated from a Python script Public Function xlF
sum
、average
、product
和text
)
临时解决办法:
我能想到的唯一方法是制作一本helper Excel手册,并向xlwings提供以下UDF样板文件:
'Some extreme workarounds to get excel-like functions working ~95%
'This function is partly generated from a Python script
Public Function xlFunction(fName As String, _
Optional args As Collection = Nothing) As Variant
If args Is Nothing Then
Set args = New Collection
End If
'First try with Application.WorksheetFunction object
On Error GoTo handleWSError
Select Case args.Count
Case 0
xlFunction = CallByName(Application.WorksheetFunction, fName, VbGet)
Case 1
xlFunction = CallByName(Application.WorksheetFunction, fName, VbGet, args(1))
Case 2
xlFunction = CallByName(Application.WorksheetFunction, fName, VbGet, args(1), args(2))
'... Case 3 to 29 left out
Case 30
xlFunction = callByName(application.worksheetFunction, fName, vbGet, args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9), args(10), args(11), args(12), args(13), args(14), args(15), args(16), args(17), args(18), args(19), args(20), args(21), args(22), args(23), args(24), args(25), args(26), args(27), args(28), args(29), args(30))
End Select
Exit Function
handleWSError:
'Put some effort into a nice message
Dim formula As String
formula = fName & "("
Dim x As Variant
For Each x In args
formula = formula & argify(x) & ","
Next x
If endsWith(formula, ",") Then
formula = Mid(formula, 1, Len(formula) - 1)
End If
formula = formula & ")"
If err.number = 1004 Then
err.raise err.number, err.source, "Error in Arguments: " & formula, err.helpFile, err.helpContext
ElseIf err.number = 438 Then
err.raise err.number, err.source, "No VBA equivalent in Application.WorksheetFunction for " & fName & ": " & formula, err.helpFile, err.helpContext
Else
err.raise err.number, err.source, "Error: " & formula & vbCrLf & err.description, err.helpFile, err.helpContext
End If
End Function
由于当前未在本机xlwings API中实现,因此必须通过
API
属性使用底层pywin32对象,请参阅:
在您的情况下,类似这样的方法会起作用(基于快速启动代码):
将xlwings作为xw导入
def main():
wb=xw.Book.caller()
工作表=工作表工作表[0]
工作表[“A10”].value=wb.app.api.WorksheetFunction.Min(工作表['A1:B2'].api)