Robot Framework ExcelLibrary:将数字放入单元格

时间:2017-12-19 12:31:33

标签: excel robotframework

我一直试图将数据写入xls工作表并保存,没有任何运气 我收到以下错误:IndexError:列表索引超出范围。大多数搜索"将数字放入单元格"产生答案为"创建自己的python关键字"。这是我的代码。它检索并保存地址的经度/纬度到文件。任何帮助表示赞赏!

Open and Read the Excel Record
    # Open the file
    Open Excel  ${EXCEL_FILE_LOCATION}${EXCEL_FILE_NAME}
    # Get the number of rows
    ${iTotalRows} =  Get Row Count  ${PageSheetName}

    # Start the for loop here
    : FOR  ${iRowNum}  IN RANGE  1  ${iTotalRows}+1
    \  ${ADDRESS} =  Read Cell Data By Name  ${PageSheetName}  A${iRowNum}
    \  ${CITY} =  Read Cell Data By Name  ${PageSheetName}  B${iRowNum}
    \  ${STATE} =  Read Cell Data By Name  ${PageSheetName}  C${iRowNum}
    \  ${ZIP} =  Read Cell Data By Name  ${PageSheetName}  D${iRowNum}
    \  ${FINAL_STRING} =  Set Variable  ${ADDRESS} ${CITY} ${STATE} ${ZIP}
    \  ${long}  ${lat}  nasa-site.Enter the Final String into Site  ${FINAL_STRING}
    \  Put Number To Cell  ${PageSheetName}  4  ${iRowNum}  ${long}
    \  Put Number To Cell  ${PageSheetName}  5  ${iRowNum}  ${lat}

    Save Excel  ${EXCEL_FILE_LOCATION}${EXCEL_FILE_NAME}

2 个答案:

答案 0 :(得分:0)

鉴于问题是关于使用关键字Put Number To Cell写入文件,我已经删除了关键字的阅读部分。变量被相应的固定值替换,因为这进一步简化了示例。即使您的代码中未指定,我假设您正在使用可以使用ExcelLibrary安装的pip install robotframework-excellibrary

以下示例读取excel文件,然后使用数字3覆盖第4列中的值。然后写入将文件保存为新名称,因为我无法覆盖打开的文件。

由于关键字是期望数字,我使用${4}${3}来确保将这些数字转换为数字而不是字符串。不这样做会引发错误。

*** Settings ***
Library    ExcelLibrary

*** Test Cases ***
Open and Read the Excel Record
    Open Excel  ${EXECDIR}\\Book1.xls

    ${iTotalRows} =  Get Row Count  Sheet1

    :FOR  ${iRowNum}  IN RANGE  1  ${iTotalRows}
    \  Put Number To Cell  Sheet1  ${4}  ${iRowNum}  ${3}

    Save Excel  Book2.xls

答案 1 :(得分:0)

久经考验的解决方案,python -2.7.x 来源-https://groups.google.com/forum/#!topic/robotframework-users/0gxwAa6f8a4

更新文件中存在的以下两种方法-site-packages \ ExcelLibrary \ ExcelLibrary.py

def put_string_to_cell(自身,工作表名称,列,行,值,check_format = True): “” 使用工作表名称,将指示单元格的值设置为参数中给出的字符串。

    Arguments:
            |  Sheet Name (string) | The selected sheet that the cell will be modified from.                                           |
            |  Column (int)        | The column integer value that will be used to modify the cell.                                    |
            |  Row (int)           | The row integer value that will be used to modify the cell.                                       |
            |  Value (string)      | The string value that will be added to the specified sheetname at the specified column and row.   |
    Example:

    | *Keywords*           |  *Parameters*                                                           |
    | Open Excel           |  C:\\Python27\\ExcelRobotTest\\ExcelRobotTest.xls  |     |     |        |
    | Put String To Cell   |  TestSheet1                                        |  0  |  0  |  Hello |

    """
    if self.wb:
        my_sheet_index = self.sheetNames.index(sheetname)
        if self.wb.get_sheet(my_sheet_index).ncols > column and self.wb.get_sheet(my_sheet_index).nrows > row and check_format:
           cell = self.wb.get_sheet(my_sheet_index).cell(int(row), int(column))
           if cell.ctype is XL_CELL_NUMBER:
              self.wb.sheets()
              if not self.tb:
                 self.tb = copy(self.wb)
        else:
          self.wb.sheets()
          if not self.tb:
            self.tb = copy(self.wb)
    if self.tb:
        plain = easyxf('')
        self.tb.get_sheet(my_sheet_index).write(int(row), int(column), value, plain)

def save_excel(自身,文件名,useTempDir = False): “” 保存由文件名指示的Excel文件,如果用户需要保存在临时目录中的文件,可以将useTempDir设置为true。 如果将布尔值useTempDir设置为true,则根据运行测试的计算机的操作系统,如果操作系统是Windows,则文件将保存在Temp目录中;如果不是Windows,则将文件保存在tmp目录中。

    Arguments:
            |  File Name (string)                      | The name of the of the file to be saved.  |
            |  Use Temporary Directory (default=False) | The file will not be saved in a temporary directory by default. To activate and save the file in a temporary directory, pass 'True' in the variable. |
    Example:

    | *Keywords*           |  *Parameters*                                      |
    | Open Excel           |  C:\\Python27\\ExcelRobotTest\\ExcelRobotTest.xls  |
    | Save Excel           |  NewExcelRobotTest.xls                             |

    """
    if useTempDir is True:
        print '*DEBUG* Got fname %s' % filename
        self.tb.save(os.path.join("/", self.tmpDir, filename))
    else:
        if self.wb:
           self.wb.release_resources()
        self.tb.save(filename)