RPA processing system using SAP closeout selected operating table specified row

When certain operating closeout SAP system, it is required to select only the aggregated value (yellow line) is negative and 0 for closeout items, results as shown:

 

 

SAP has a way to select specific lines:

 1 import sys
 2 import win32com.client
 3 
 4 SapGuiAuto = win32com.client.GetObject("SAPGUI")
 5 application = SapGuiAuto.GetScriptingEngine
 6 connection = application.Children(0)
 7 session = connection.Children(0)  # 连接SAP服务
 8 
 9 e = session.findByID('wnd[0]/usr/cntlGRID1/shellcont/shell')  # 获取SAP表格
10 e.selectedRows = '1'

 

If you want to select multiple rows only need to write:

e.selectedRows = '1,2,3,5'

 


If it is a continuous line can be more simple:

e.selectedRows = '1-5'

 


We run look at the results:

 

 

It should be noted that, if the table is large, the back part of the table is not loaded, so do not run directly e.selectedRows effect, it is necessary to turn the page, run when you turn the last page needs to be selected row.

Turning part of the code as follows:

. 1  Import ubpa.ikeyboard AS ikeyboard
 2  
. 3 Row = e.rowCount # rows 
. 4  Print (Row // 44) # number of required page, the page 44 is currently displayed line number 
. 5  
. 6  for J in Range (Row // 44 is ):
 . 7      ikeyboard.key_send_cs (text = ' {} the PGDN ' , WAITFOR = 10 )
 . 8      the time.sleep (0.4)

 

Now there is a better method page: http: //support.isearch.com.cn/article/1544495156668
so it is recommended to turn the pages with less pagedown.

The following began to design algorithms:


We need three lists, keep a number of rows where aggregated amount, a deposit amount of each marker summary (here the amount is negative and 0 is marked negative sign, the amount being labeled as a positive sign), a keep all indexes negative sign. Named index, type_index, need_index

① First traverse the entire table according to the document number and the document number for the index blank lines added to the first list, the number of lines is the amount of aggregated where:

1 for i in range(e.rowCount - 1):
2     value = e.getCellValue(i, e.columnOrder(0))
3     if value == '':
4         index.append(i)    

 


② then aggregated amount for each tag marked and stored in the second list:

1 for i in index:
2     if e.getCellValue(i, e.columnOrder(12)).endswith('-') or e.getCellValue(i, e.columnOrder(12)) == '0.00':
3         type_index.append('-')
4     else:
5         type_index.append('+')    

 


③ Next, the second list is determined as long as the flag, if it is negative the index to take on a value intermediate index, the values ​​are all eligible to join the third list:

. 1  IF type_index [0] == ' + ' :
 2      for I in Range (len (type_index)):
 . 3          IF type_index [I] == ' - ' :
 . 4              IF index [I -. 1] +. 1 == index [ I] -. 1:   # if separated by only a line between the two amounts summarized 
. 5                  need_index.append (STR (index [I -. 1] +. 1 ))
 . 6              the else :
 . 7                  need_index.append (STR (index [I -. 1] . 1 +) + ' - ' + STR (index [I] -. 1 ))
 . 8  elif type_index [0] == '- ' :   # first aggregated into a negative value, then the situation is slightly more complex 
. 9      IF index [0] ==. 1:   # Summary amount only before the first row 
10          need_index.append ( ' 0 ' )
 . 11      the else :
 12 is          need_index .append ( ' 0- ' + STR (index [0] -. 1 ))
 13 is      for I in Range (len (type_index) -. 1 ):
 14          IF type_index [I +. 1] == ' - ' :
 15              IF index [ I] == index. 1 + [I +. 1] -. 1 :
 16                 need_index.append(str(index[i]+1))
17             else:
18                 need_index.append(str(index[i] + 1) + '-' + str(index[i + 1] - 1))

 


④need_index now contains all the rows you want to select, then turn it into a string format we need:

e.selectedRows = ','.join(need_index)

 

Time complexity of the algorithm or high point, if you have a better idea can also share out

 The complete code

 1 import sys
 2 import win32com.client
 3 import time
 4 import ubpa.ikeyboard as ikeyboard
 5 
 6 SapGuiAuto = win32com.client.GetObject("SAPGUI")
 7 application = SapGuiAuto.GetScriptingEngine
 8 connection = application.Children(0)
 9 session = connection.Children(0)  # 连接SAP服务
10 
11 e = session.findByID('wnd[0]/usr/cntlGRID1/shellcont/shell')  # 获取SAP表格
12 is  
13 is F = e.columnCount   # columns 
14 Row = e.rowCount   # rows 
15  Print ( ' the number of rows in the table is {}, the number of columns of the table {} ' .format (Row, F))
 16  Print ( ' - ' * 20 is )
 . 17  Print (int (Row / 44 is)) # number of required flip 
18 is  
. 19  for J in Range (int (Row / 44 is )):
 20 is      ikeyboard.key_send_cs (text = ' {} the PGDN ' , WAITFOR = 10 )
 21 is      the time.sleep (0.4 )
22 is  
23 is index = [] # line number where the aggregated amount of 
24 type_index = [] # for each aggregate amount of positive and negative values, if a negative value is also labeled 0 
25 need_index = [] # all negative index 
26 is  
27  for I in Range (e.rowCount -. 1 ):
 28      value = e.getCellValue (I, e.columnOrder (0))
 29      IF value == '' :
 30          index.append (I)
 31 is  
32  Print ( ' each amount summary of the index where {}, the total number of customers is {} ' .format (index, len (index)))
 33 is  Print ( ' -'*20)
34 
35 for i in index:
36     if e.getCellValue(i, e.columnOrder(12)).endswith('-') or e.getCellValue(i, e.columnOrder(12)) == '0.00':
37         type_index.append('-')
38     else:
39         type_index.append('+')
40 
41 if type_index[0] == '+':
42     for i in range(len(type_index)):
43         if type_index[i] == '-':
44             if index[i - 1] + 1 == index[i] - 1:
45                 need_index.append(str(index[i - 1] + 1))
46             else:
47                 need_index.append(str(index[i - 1] + 1) + '-' + str(index[i]-1))
48 elif type_index[0] == '-':
49     if index[0] == 1:
50         need_index.append('0')
51     else:
52         need_index.append('0-' + str(index[0] - 1))
53     for i in range(len(type_index) - 1):
54         if type_index[i + 1] == '-':
55             if index[i] + 1 == index[i + 1] - 1:
56                 need_index.append(str(index[i] + 1))
57             else:
58                 need_index.append(str(index[i] + 1) + '-' + str(index[i + 1] - 1))
59                 
60 e.selectedRows = ','.join(need_index)

 

Original link: https: //support.i-search.com.cn/article/1542766504938

Guess you like

Origin www.cnblogs.com/isearch/p/11846944.html
Recommended