Файлы MS Excel чаще всего используются для хранения данных в автоматизации тестирования. Мы создали JScript класс, с помощью которого можно создавать новые файлы Excel со списком заданных листов; считывать и записывать данные, обращаясь к ячейкам как по номерам столбцов и строк, так и указывая имена ячеек; считывать сразу целые строки, колонки, массивы данных, и т.д.
Скачать архив с исходными кодами класса и документацией можно здесь.
Ниже представлен пример использования класса ExcelClass.
//USEUNIT ExcelUnit
function TestExcelClass()
{
// specifying excel file name and deleting it, if it is already exists
var excel = new ExcelClass("C:\\temp.xls");
if(Utilities.FileExists(excel.FileName))
{
Utilities.DeleteFile(excel.FileName);
}
// creating new Excel file with the specified name and predefined sheets
var arrSheets = new Array("MySheet1", "MySheet2", "MySheet3", "MySheet4");
excel.CreateFile(null, arrSheets);
// writing data to the created Excel file by different ways
excel.Write("MySheet1", 1, 1, "text in row 1, column 1");
excel.Write("MySheet1", 1, "B", "text in row 1, column 'B'");
excel.WriteCell("MySheet1", "C1", "text in cell 'C1'");
// specifying data for "matrix writing"
var arrRow1 = new Array("text1", "text2", "text3");
var arrRow2 = new Array("text4", "text5", "text6");
var arrMatrix = new Array(arrRow1, arrRow2);
excel.WriteMatrix("MySheet1", 3, "A", arrMatrix);
// reading written data using different methods
Log.Message("Text in cell A1", excel.ReadCell("MySheet1", "A1"));
Log.Message("Text in cell B1", excel.Read("MySheet1", 1, 2));
Log.Message("Text in cell C1", excel.Read("MySheet1", 1, "C"));
var arrNew1 = excel.ReadMatrix("MySheet1", 3, "A", 4, "C");
Log.Message("Matrix data", arrNew1[0].join(" ") + "\n" + arrNew1[1].join(" "));
var arrNew2 = excel.ReadCells("MySheet1", "A1", "C1", "A4", "C4");
Log.Message("Data from several cells", arrNew2.join("\n"));
}
{
// specifying excel file name and deleting it, if it is already exists
var excel = new ExcelClass("C:\\temp.xls");
if(Utilities.FileExists(excel.FileName))
{
Utilities.DeleteFile(excel.FileName);
}
// creating new Excel file with the specified name and predefined sheets
var arrSheets = new Array("MySheet1", "MySheet2", "MySheet3", "MySheet4");
excel.CreateFile(null, arrSheets);
// writing data to the created Excel file by different ways
excel.Write("MySheet1", 1, 1, "text in row 1, column 1");
excel.Write("MySheet1", 1, "B", "text in row 1, column 'B'");
excel.WriteCell("MySheet1", "C1", "text in cell 'C1'");
// specifying data for "matrix writing"
var arrRow1 = new Array("text1", "text2", "text3");
var arrRow2 = new Array("text4", "text5", "text6");
var arrMatrix = new Array(arrRow1, arrRow2);
excel.WriteMatrix("MySheet1", 3, "A", arrMatrix);
// reading written data using different methods
Log.Message("Text in cell A1", excel.ReadCell("MySheet1", "A1"));
Log.Message("Text in cell B1", excel.Read("MySheet1", 1, 2));
Log.Message("Text in cell C1", excel.Read("MySheet1", 1, "C"));
var arrNew1 = excel.ReadMatrix("MySheet1", 3, "A", 4, "C");
Log.Message("Matrix data", arrNew1[0].join(" ") + "\n" + arrNew1[1].join(" "));
var arrNew2 = excel.ReadCells("MySheet1", "A1", "C1", "A4", "C4");
Log.Message("Data from several cells", arrNew2.join("\n"));
}