scot1967
Champion Level 3
7 months agoHere is a simple function to write data to a spreadsheet...
Ref: https://support.smartbear.com/testcomplete/docs/testing-with/working-with-external-data-sources/excel/index.html
function Write2Excel(_valuesObj,_fileName,_sheetName)
{
let excelFile = undefined;
let excelSheet = undefined;
if (!aqFile.Exists(_fileName))
{
// Create a new Excel file and add a new empty sheet to it
Log.Message(aqString.Format("The %s file does not exist and will be created.", _fileName));
excelFile = Excel.Create(_fileName);
excelSheet = excelFile.AddSheet(_sheetName);
excelFile.Save();
}
if (aqFile.Exists(_fileName))
{
// Open the existing Excel file
excelFile = Excel.Open(_fileName);
if(SheetWithTitleExists(excelFile,_sheetName) == false)
{
excelSheet = excelFile.AddSheet(_sheetName);
}
else
{
excelSheet = excelFile.SheetByTitle(_sheetName);
}
Object.entries(_valuesObj).forEach(([key, value]) =>
{
if(excelSheet.RowCount == 0)
{
// Write the header row
Object.entries(value).forEach(([key, value]) =>
{
//Write Line Item Values to rows.
let cellData = "";
Object.entries(value).forEach(([key, value]) =>
{
cellHeader = key;
cellData = value;
});
excelSheet.Cell(key, 1).Value = cellHeader;
excelSheet.Cell(key, 2).Value = cellData;
});
}
else
{
rowIndex = excelSheet.RowCount + 1;
Object.entries(value).forEach(([key, value]) =>
{
//Write Line Item Values to rows.
let cellData = "";
Object.entries(value).forEach(([key, value]) =>
{
cellData = value;
});
excelSheet.Cell(key, rowIndex).Value = cellData;
});
}
});
}
// Save the file to apply the changes
excelFile.Save();
//Excel.Close(fileName);
}
function SheetWithTitleExists(_fileName,_sheetName)
{
if (! equal(_fileName, null))
{
if (_fileName.SheetCount > 0)
{
for (let i = 0; i < _fileName.SheetCount; i++)
{
if (equal(_fileName.SheetByIndex(i).Title, _sheetName))
{
return true;
}
}
return false;
}
else
{
return false;
}
}
}
module.exports.Write2Excel = Write2Excel;
module.exports.SheetWithTitleExists = SheetWithTitleExists;