使用RPA處理SAP系統清帳操做中選表格指定的行

SAP 系統中進行某些清帳操做時,要求只選中金額彙總(黃色的行)爲負數和 0 的項目進行清帳,效果如圖所示:
算法

 

 

SAP 中有一個方法能夠選中指定的行:shell

 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'

 

若是要選中多行只須要這樣寫:session

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

 


若是是連續的行能夠更簡單:app

e.selectedRows = '1-5'

 


咱們運行看一下效果:
ui

 

 

須要注意一點,若是表很大時,表後面的部分沒有加載,因此直接運行 e.selectedRows 沒有效果,所以必須翻頁,當翻到最後一頁時運行就能夠把須要的行都選中了。spa

翻頁部分代碼以下:設計

1 import ubpa.ikeyboard as ikeyboard
2 
3 row = e.rowCount # 行數
4 print(row // 44) # 須要翻頁的次數,44爲當前頁面顯示的行數
5 
6 for j in range(row // 44):
7     ikeyboard.key_send_cs(text='{PGDN}',waitfor=10)
8     time.sleep(0.4)

 

如今有個更優秀的翻頁方法:http://support.isearch.com.cn/article/1544495156668
所以仍是建議少用 pagedown 進行翻頁。3d

下面開始設計算法:code


咱們須要三個列表,一個存金額彙總所在的行數,一個存每一個金額彙總的標記(這邊把金額爲負和 0 的標記爲負號,把金額爲正的標記爲正號),一個存全部負號的索引。分別命名爲 index、type_index、need_indexorm

①首先根據憑證編號遍歷整張表,並把憑證編號爲空行的索引加入到第一個列表中,這個就是金額彙總所在的行數:

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

 


②而後給每一個金額彙總打上標記並存入第二個列表中:

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('+')    

 


③接下來只要判斷第二個列表中的標記,若是爲負就取這個索引與上一個索引中間的值,把全部符合條件的值都加入到第三個列表中:

 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:  # 若是兩個金額彙總之間只隔了一行
 5                 need_index.append(str(index[i - 1] + 1))
 6             else:
 7                 need_index.append(str(index[i - 1] + 1) + '-' + str(index[i] - 1))
 8 elif type_index[0] == '-':  # 第一個金額彙總爲負號的話狀況稍微複雜點
 9     if index[0] == 1:  # 第一個金額彙總前面只有一行
10         need_index.append('0')
11     else:
12         need_index.append('0-' + str(index[0] - 1))
13     for i in range(len(type_index) - 1):
14         if type_index[i + 1] == '-':
15             if index[i] + 1 == index[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 如今包含了全部須要選擇的行,接下來把它變成咱們須要的字符串格式:

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

 

算法的時間複雜度仍是高了點,若是大家有更好的思路也能夠分享出來

 完整代碼

 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 
13 f = e.columnCount  # 列數
14 row = e.rowCount  # 行數
15 print('表格行數爲{},表格列數爲{}'.format(row, f))
16 print('-' * 20)
17 print(int(row / 44)) #須要翻頁的次數
18 
19 for j in range(int(row / 44)):
20     ikeyboard.key_send_cs(text = '{PGDN}',waitfor = 10)
21     time.sleep(0.4)
22 
23 index = [] #金額彙總所在的行數
24 type_index = [] #每一個金額彙總的正負值,若爲0也標記爲負值
25 need_index = [] #全部負值的索引
26 
27 for i in range(e.rowCount - 1):
28     value = e.getCellValue(i, e.columnOrder(0))
29     if value == '':
30         index.append(i)
31 
32 print('每一個金額彙總所在的索引爲{},總共的客戶數量爲{}'.format(index, len(index)))
33 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)

 

原文連接:https://support.i-search.com.cn/article/1542766504938

相關文章
相關標籤/搜索