我如何通过 Python 脚本 select SAP GuiGridView 行

How can I select an SAP GuiGridView row via Python scripting

我正在尝试将 Python 用于 SAP 脚本,特别是 select GuiGridView 对象中的一行。它在 VB 脚本中工作得很好,但在 Python 中我得到一个异常:-2147352562,'Invalid number of parameters.',这对我来说没有意义,因为我使用了相同数量的VB 脚本的参数。

我正试图摆脱 VB 脚本,因为它没有正确的错误处理。

这是文档 (https://help.sap.com/doc/9215986e54174174854b0af6bb14305a/760.01/en-US/sap_gui_scripting_api_761.pdf ),但即便如此我也无法解决这个问题:

第 128 页

SelectedRows(读写)

Public 属性 SelectedRows As String

该字符串是行索引号或索引范围的逗号分隔列表,例如“1,2,4-8,10”。将此属性设置为无效字符串或包含无效的字符串行索引将引发异常。

以下 VB 脚本代码 select 是预期的 GuiGridRow,但是当我尝试将其转换为 Python 时,它不起作用。

If Not IsObject(application) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.Children(0)
End If
If Not IsObject(session) Then
   Set session = connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session,     "on"
   WScript.ConnectObject application, "on"
End If

Set GridView = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
GridView.SelectedRows = 1

这是我使用 Jupyter Notebook 的 Python 3.9 尝试和结果:

+*In[1]:*+
import win32com.client


+*In[2]:*+
SapGuiAuto = win32com.client.GetObject('SAPGUI')
application = SapGuiAuto.GetScriptingEngine
connection = application.Children(0)
session = connection.Children(0)
type(session)

+*Out[2]:*+
----win32com.client.CDispatch----


+*In[3]:*+
grid_view = session.findById('wnd[0]/usr/cntlGRID1/shellcont/shell')
print(grid_view)

+*Out[3]:*+
<COMObject <unknown>>


+*In[4]:*+
# to show that grid_view is a valid GuiGridView object
grid_view.RowCount

+*Out[4]:*+
----2----


+*In[5]:*+
# reading the SelectedRows works
grid_view.SelectedRows


+*Out[5]:*+
----'1'----


+*In[6]:*+
# setting SelectedRows as number fails
# note: I had manually unselected the row in SAP before running the rest of the code
grid_view.SelectedRows = 1

+*Out[6]:*+

    ---------------------------------------------------------------------------
    com_error                                 Traceback (most recent call last)

    ~\AppData\Local\Temp/ipykernel_21344/1273355717.py in <module>
    ----> 1 grid_view.SelectedRows = 1
    

    E:\Anaconda\Lib\site-packages\win32com\client\dynamic.py in __setattr__(self, attr, value)
        541                                 entry = self._olerepr_.propMap[attr]
        542                                 invoke_type = _GetDescInvokeType(entry, pythoncom.INVOKE_PROPERTYPUT)
    --> 543                                 self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
        544                                 return
        545                         # Check the specific "put" map.
    

    com_error: (-2147352562, 'Invalid number of parameters.', None, None)
----


+*In[7]:*+
# setting SelectedRows as string fails
grid_view.SelectedRows = '1'

+*Out[7]:*+

    ---------------------------------------------------------------------------

    com_error                                 Traceback (most recent call last)

    ~\AppData\Local\Temp/ipykernel_21344/1222153478.py in <module>
    ----> 1 grid_view.SelectedRows = '1'
    

    E:\Anaconda\Lib\site-packages\win32com\client\dynamic.py in __setattr__(self, attr, value)
        541                                 entry = self._olerepr_.propMap[attr]
        542                                 invoke_type = _GetDescInvokeType(entry, pythoncom.INVOKE_PROPERTYPUT)
    --> 543                                 self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
        544                                 return
        545                         # Check the specific "put" map.
    

    com_error: (-2147352562, 'Invalid number of parameters.', None, None)

----


+*In[8]:*+
# setting SelectedRows as string with multiple rows fails
grid_view.SelectedRows = '0,1'
----

+*Out[8]:*+

    ---------------------------------------------------------------------------

    com_error                                 Traceback (most recent call last)

    ~\AppData\Local\Temp/ipykernel_21344/3316141255.py in <module>
    ----> 1 grid_view.SelectedRows = '0,1'
    

    E:\Anaconda\Lib\site-packages\win32com\client\dynamic.py in __setattr__(self, attr, value)
        541                                 entry = self._olerepr_.propMap[attr]
        542                                 invoke_type = _GetDescInvokeType(entry, pythoncom.INVOKE_PROPERTYPUT)
    --> 543                                 self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
        544                                 return
        545                         # Check the specific "put" map.
    

    com_error: (-2147352562, 'Invalid number of parameters.', None, None)

----


+*In[9]:*+
# setting SelectedRows as string with multiple rows in a method-type notation fails
grid_view.SelectedRows('0,1')
----

+*Out[9]:*+

    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    ~\AppData\Local\Temp/ipykernel_21344/2186236324.py in <module>
    ----> 1 grid_view.SelectedRows('0,1')
    

    TypeError: 'str' object is not callable

----

好的,在深入研究之后,修补到最新版本的 win32com 成功了: https://github.com/mhammond/pywin32/tree/main/com/win32com

完成后,以下所有操作都适用于 select 行:

grid_view.SelectedRows = 1

grid_view.SelectedRows = '1'

grid_view.SelectedRows = '0,1'

grid_view.SelectedRows = '0-1'

以下无效(类型错误),这是有道理的,因为它暗示元组 packing/unpacking:

grid_view.SelectedRows = 0,1

这会导致 select 编辑意外的行,因为它正在执行减法而不是指示行的范围:

grid_view.SelectedRows = 0-1