我正在从soapUI 中读取属性及其值并将它们写入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()
但是现在我必须将所有属性写入同一个excel。我已经创建了 excel 列名和soapUI 属性的映射,所以现在我只需要从映射中找到匹配的 excel col 名称并在该 excel 下写入属性值。
我正在使用一个函数来做这些事情。这个函数是在一个 for 循环中调用的,该循环遍历测试用例中的所有属性。我通过这个函数
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
该功能的相关代码如下。
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,它没有新数据..nothing..zilch !!
文件大小增加的证据。
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 进程,而不是循环,我将尝试使用该函数仅写入一到两组数据,并将相应地更新此问题。