使用RPA处理SAP系统清账操作中选表格指定的行

喜夏-厌秋 提交于 2019-12-04 06:49:52

SAP 系统中进行某些清账操作时,要求只选中金额汇总(黄色的行)为负数和 0 的项目进行清账,效果如图所示:

SAP 中有一个方法可以选中指定的行:

import sys  
import win32com.client

SapGuiAuto = win32com.client.GetObject("SAPGUI")  
application = SapGuiAuto.GetScriptingEngine  
connection = application.Children(0)  
session = connection.Children(0)  # 连接SAP服务

e = session.findByID('wnd\[0\]/usr/cntlGRID1/shellcont/shell')  # 获取SAP表格  
e.selectedRows = '1'  

如果要选中多行只需要这样写:

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

如果是连续的行可以更简单:

e.selectedRows = '1-5'  

我们运行看一下效果:

完美,另外我们知道了这个选中行是从 0 开始的。

需要注意一点,如果表很大时,表后面的部分没有加载,所以直接运行 e.selectedRows 没有效果,因此必须翻页,当翻到最后一页时运行就可以把需要的行都选中了。

翻页部分代码如下:

import ubpa.ikeyboard as ikeyboard

row = e.rowCount  # 行数  
print(row // 44)  # 需要翻页的次数,44为当前页面显示的行数

for j in range(row // 44):  
    ikeyboard.key\_send\_cs(text='{PGDN}',waitfor=10)  
    time.sleep(0.4)  

现在有个更优秀的翻页方法:http://support.isearch.com.cn/article/1544495156668
因此还是建议少用 pagedown 进行翻页。
##### 下面开始设计算法:
我们需要三个列表,一个存金额汇总所在的行数,一个存每个金额汇总的标记(这边把金额为负和 0 的标记为负号,把金额为正的标记为正号),一个存所有负号的索引。分别命名为 index、type_index、need_index

①首先根据凭证编号遍历整张表,并把凭证编号为空行的索引加入到第一个列表中,这个就是金额汇总所在的行数:

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

②然后给每个金额汇总打上标记并存入第二个列表中:

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

③接下来只要判断第二个列表中的标记,如果为负就取这个索引与上一个索引中间的值,把所有符合条件的值都加入到第三个列表中:

if type_index\[0\] == '+':  
    for i in range(len(type_index)):  
        if type_index\[i\] == '-':  
            if index\[i - 1\] + 1 == index\[i\] - 1:  # 如果两个金额汇总之间只隔了一行  
                need_index.append(str(index\[i - 1\] + 1))  
            else:  
                need_index.append(str(index\[i - 1\] + 1) + '-' + str(index\[i\] - 1))  
elif type_index\[0\] == '-':  # 第一个金额汇总为负号的话情况稍微复杂点  
    if index\[0\] == 1:  # 第一个金额汇总前面只有一行  
        need_index.append('0')  
    else:  
        need_index.append('0-' + str(index\[0\] - 1))  
    for i in range(len(type_index) - 1):  
        if type_index\[i + 1\] == '-':  
            if index\[i\] + 1 == index\[i + 1\] - 1:  
                need_index.append(str(index\[i\]+1))  
            else:  
                need_index.append(str(index\[i\] + 1) + '-' + str(index\[i + 1\] - 1))  

④need_index 现在包含了所有需要选择的行,接下来把它变成我们需要的字符串格式:

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

算法的时间复杂度还是高了点,如果你们有更好的思路也可以分享出来

#### 完整代码

import sys  
import win32com.client  
import time  
import ubpa.ikeyboard as ikeyboard

SapGuiAuto = win32com.client.GetObject("SAPGUI")  
application = SapGuiAuto.GetScriptingEngine  
connection = application.Children(0)  
session = connection.Children(0)  # 连接SAP服务

e = session.findByID('wnd\[0\]/usr/cntlGRID1/shellcont/shell')  # 获取SAP表格

f = e.columnCount  # 列数  
row = e.rowCount  # 行数  
print('表格行数为{},表格列数为{}'.format(row, f))  
print('-' * 20)  
print(int(row / 44)) #需要翻页的次数

for j in range(int(row / 44)):  
    ikeyboard.key\_send\_cs(text = '{PGDN}',waitfor = 10)  
    time.sleep(0.4)

index = \[\] #金额汇总所在的行数  
type_index = \[\] #每个金额汇总的正负值,若为0也标记为负值  
need_index = \[\] #所有负值的索引

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

print('每个金额汇总所在的索引为{},总共的客户数量为{}'.format(index, len(index)))  
print('-'*20)

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

if type_index\[0\] == '+':  
    for i in range(len(type_index)):  
        if type_index\[i\] == '-':  
            if index\[i - 1\] + 1 == index\[i\] - 1:  
                need_index.append(str(index\[i - 1\] + 1))  
            else:  
                need_index.append(str(index\[i - 1\] + 1) + '-' + str(index\[i\]-1))  
elif type_index\[0\] == '-':  
    if index\[0\] == 1:  
        need_index.append('0')  
    else:  
        need_index.append('0-' + str(index\[0\] - 1))  
    for i in range(len(type_index) - 1):  
        if type_index\[i + 1\] == '-':  
            if index\[i\] + 1 == index\[i + 1\] - 1:  
                need_index.append(str(index\[i\] + 1))  
            else:  
                need_index.append(str(index\[i\] + 1) + '-' + str(index\[i + 1\] - 1))  
                  
e.selectedRows = ','.join(need_index)  

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!