简体   繁体   English

如何在groovy中使用scriptom将数据写入Excel?

[英]how to write data to excel using scriptom in groovy?

I am reading properties and their values from soapUI and write them to an excel. 我正在从soapUI读取属性及其值,并将它们写入Excel。

I am able to write the unique properties name into an excel 我可以将唯一的属性名称写入Excel

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

However now I have to write all the properties to the same excel. 但是现在我必须将所有属性写入相同的excel。 I have already created a map of the excel column names and soapUI properties so now i just have to find the matching excel col name from the map and write the property value under that excel. 我已经创建了一个excel列名称和soapUI属性的映射,所以现在我只需要从该映射中找到匹配的excel col名称,并在该excel下写入属性值即可。

I am using a function to do this stuff. 我正在使用一个函数来执行此操作。 This function is called from within a for loop which loops through all the properties in a test case. 从for循环内调用此函数,该循环遍历测试用例中的所有属性。 To this function I pass 我通过这个功能

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

The relevant code for this function is below. 该功能的相关代码如下。

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()
}

The code is currently running. 该代码当前正在运行。 It has been running since yesterday evening(2pm EST) so even if the code works it is not optimal. 它自昨天晚上(美国东部标准时间下午2点)以来一直在运行,因此即使代码有效,也不是最佳选择。 I can't wait this long to write data. 我等不及要写数据了。

The curious thing is that the size of the excel keeps increasing which would mean that data is being written to the excel but i have check the excel and it has no new data..nothing..zilch!! 奇怪的是,excel的大小一直在增加,这意味着数据正在写入excel,但是我已经检查了excel,没有新数据..没什么..zilch!

Evidence of increasing size of the file. 文件大小增加的证据。

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

My questions are: 我的问题是:
1. Why is data not being written when i am calling the function from within the for loop but getting written when i call it linear-ly? 1.为什么当我从for循环中调用函数时,数据没有被写入,而当我线性地调用它时却被写入?
2. In the first piece of code the excel process goes away when its done writing but when the function is run, the excel process remains even though its memory utilization goes up and down. 2.在第一段代码中,excel进程在完成编写后便消失了,但是当函数运行时,即使excel进程的内存利用率上升或下降,excel进程也会保留。

I am going to kill the excel process and instead of looping I am going to try and write only one or two sets of data using the function and will update this question accordingly. 我将终止excel过程,而不是循环,我将尝试使用该函数仅写入一组或两组数据,并将相应地更新此问题。

The process of opening an excel, writing to a cell, , save the excel, closing the excel is a time consuming task and when you multiply this with 300 test cases and ~15 properties per test, it can take significantly long. 打开excel,写入单元格,保存excel,关闭excel的过程非常耗时,将其与300个测试用例和每个测试的约15个属性相乘时,可能会花费很长时间。 That is what has happening in my case and hence the process was taking forever to complete. 就我而言,这就是发生的事情,因此该过程要花很长时间才能完成。

I am not 100% on why the size of the excel was increasing and nothing was getting written but i would guess that data was being kept in the memory and would have been written once the last cell was written, workbook saved and excel closed. 我不是100%为何会增加excel的大小并且什么都没写,但是我猜想数据将一直保存在内存中,并且在写入最后一个单元格,保存工作簿并关闭excel后会被写入。 This never happened because I didn't let it complete and would kill it when i realized that it has been running for an exceptionally long time. 这从来没有发生过,因为当我意识到它已经运行了很长时间时,我没有让它完成并杀死它。

In order to make this work, i changed my approach to the following. 为了使此工作有效,我将方法更改为以下内容。

  1. generate a map of col name and prop name 生成col名称和prop名称的映射
  2. generate a map of prop name and prop value for each test case. 为每个测试用例生成属性名称和属性值的映射。 As one test case can have multiple property test steps, i create a multi map like this... 由于一个测试用例可以具有多个属性测试步骤,因此我创建了一个像这样的多重映射...
    [Step#:[propname:propvalue,....propname:propvalue]] [Step#:[propname:propvalue,.... propname:propvalue]]
  3. create another map with col name and col id. 使用col名称和col id创建另一个地图。
  4. Create a new map with col id and prop value. 使用col id和prop值创建一个新地图。 I made this using the above created maps. 我使用上面创建的地图进行了此操作。
  5. write data to excel. 将数据写入Excel。 Because i already have the col id, and the value that goes into it. 因为我已经有了col id以及它的值。 i don't do any checks and just write data to excel. 我不做任何检查,只是将数据写入Excel。

these steps are repeated for all test cases in the test suite. 对测试套件中的所有测试用例重复这些步骤。 Using this process, i was able to complete my task within a few minutes. 使用此过程,我能够在几分钟内完成任务。

I know i am using quite a few maps but this is the approach i could come up with. 我知道我正在使用很多地图,但这是我可以想到的方法。 If anyone has a better approach, I would really like to try that out too. 如果有人有更好的方法,我真的也想尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM