首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在groovy中使用脚本编写数据到excel?

如何在groovy中使用脚本编写数据到excel?
EN

Stack Overflow用户
提问于 2014-02-21 13:04:32
回答 1查看 3.8K关注 0票数 0

我正在从soapUI中读取属性及其值,并将它们写入excel。

我能够将唯一的属性名称写入excel中。

代码语言:javascript
复制
def oExcel = new ActiveXObject('Excel.Application')
Thread.sleep(1000)
assert oExcel != null, "Excel object not initalized"

def openWb = oExcel.Workbooks.Open(excelPath) //excelPath complete path to the excel
def dtUsedRange = openWb.Sheets(dataSheetName).UsedRange //dataSheetName is the name of teh sheet which will ultimately hold the data

//add property names to xlMapSheet under col d or col# 4
for(int r = 1;r<=uniqPropName.size().toInteger();r++){ //uniqPropName is a list that holds all the unique property names in a test suite
    openWb.Sheets(xlMapSheet).Cells(r,4).Value = uniqPropName[r-1]
}

oExcel.DisplayAlerts = false
openWb.Save
oExcel.DisplayAlerts = true

openWb.Close(false,null,false)
oExcel.Quit()
Scriptom.releaseApartment()

然而,现在我必须把所有的属性都写到同一个excel中。我已经创建了一个excel列名和soapUI属性的映射,所以现在我只需要从映射中找到匹配的excel名称,并将属性值写入该excel下。

我用一个函数来做这件事。此函数从for循环中调用,该循环循环遍历测试用例中的所有属性。对于这个函数,我传递

代码语言:javascript
复制
sheetName //sheet where data has to be written
sheet //path of the excel file
pName //property name
pValue //property value
xMap //excel col name/heading map
tName //test case name
tsNum //step number

此函数的相关代码如下。

代码语言:javascript
复制
def write2Excel(sheetName,sheet,pName,pValue,xMap,tName,tsNum){

    //find the xl Col Name from the map


    def xl = new ActiveXObject('Excel.Application')
    assert xl != null, "Excel object not initalized"

    //open excel
    def wb = xl.Workbooks.Open(sheet)

    def rng = wb.Sheets(sheetName).UsedRange

    //get row count
    int iColumn = rng.Columns.Count.toInteger()
    int iRow = rng.Rows.Count.toInteger()

    //find column number using the col name

    //find the row with matching testcase name and step#

    //write data to excel
    if(rFound){ //if a row matching test case name and step number is found
          rng.Cells(r,colId).Value = pValue
    }else{
          rng = rng.Resize(r+1,iColumn) //if the testcase and step# row doesn't exist then the current range has to be extended to add one more row of data.
          rng.Cells(r+1,colId).Value = pValue
    }

    //save and close
    xl.DisplayAlerts = false
    wb.Save
    xl.DisplayAlerts = true

    wb.Close(false,null,false)
    xl.Quit()
    Scriptom.releaseApartment()
}

该代码目前正在运行。它从昨天晚上(东部时间下午2点)开始运行,所以即使代码工作,它也不是最优的。我等不及要写数据了。

奇怪的是,excel的大小不断增加,这意味着数据正在写入excel,但我检查了excel,它没有新的data..nothing..zilch!

增加文件大小的证据.

代码语言:javascript
复制
20/02/2014  04:23 PM           466,432 my_excel_file.xls
20/02/2014  04:23 PM           466,944 my_excel_file.xls
20/02/2014  04:38 PM           470,016 my_excel_file.xls
20/02/2014  04:45 PM           471,552 my_excel_file.xls
20/02/2014  04:47 PM           472,064 my_excel_file.xls
20/02/2014  05:01 PM           474,112 my_excel_file.xls
20/02/2014  05:01 PM           474,112 my_excel_file.xls
21/02/2014  07:23 AM           607,232 my_excel_file.xls
21/02/2014  07:32 AM           608,768 my_excel_file.xls
21/02/2014  07:50 AM           611,328 my_excel_file.xls

我的问题是:

  1. 为什么当我从for循环中调用函数时,没有编写数据,但是当我将它称为线性时,数据被写了呢?
  2. 在第一段代码中,excel进程在完成编写时会消失,但是当函数运行时,excel进程将保持不变,即使它的内存利用率会上升或下降。

我将终止excel进程,而不是循环,我将尝试使用该函数只编写一两组数据,并相应地更新这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-25 15:39:44

打开excel、写入单元格、保存excel、关闭excel是一项耗时的任务,当您将其与300个测试用例和每个测试的15个属性相乘时,可能会花费很长的时间。这就是在我的情况下所发生的事情,因此,这一过程需要花费很长时间才能完成。

我并不是百分之百地知道为什么excel的大小在增加,什么也没有写出来,但我猜想数据被保存在内存中,并且一旦最后一个单元格被写入,工作簿被保存并且excel关闭,我就会写数据。这从来没有发生,因为我没有让它完成,当我意识到它已经运行了非常长的一段时间后,我会杀死它。

为了使这一工作,我改变了我的方法如下。

  1. 生成一个名称和支柱名称的映射。
  2. 为每个测试用例生成一个道具名称和支持值的映射。由于一个测试用例可以有多个属性测试步骤,所以我创建一个像这样的多个映射. [Step#:propname:propvalue,....propname:propvalue]
  3. 创建另一个具有create和col id的映射。
  4. 创建一个具有col和prop值的新地图。我是用上面创建的地图制作的。
  5. 将数据写入excel。因为我已经有了col,以及它的值。我不做任何检查,只写数据到excel。

对于测试套件中的所有测试用例,都会重复这些步骤。使用这个过程,我能够在几分钟内完成我的任务。

我知道我正在使用相当多的地图,但这是我能想到的方法。如果有人有更好的方法,我也很想尝试一下。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21935285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档