Excel File Interaction
You can use SenseTalk to access data stored in Microsoft Excel format. Using the functions in this section, SenseTalk reads and writes data directly in individual cell values.
Working with Excel Files in SenseTalk
You can update and write values back to an Excel spreadsheet file, and you can update properties, such as text color or font size, in a cell. For example, you could configure SenseTalk to read data from a spreadsheet, use this data to drive tests, then write the test results back to the spreadsheet.
The functions described here are distinct from SenseTalk’s database capabilities. To access the contents of a spreadsheet as a database, see Working with Excel as a Database.
As a best practice, any files referenced within a SenseTalk script should be added to Eggplant Functional through the Resources pane in the Suite window. This method stores files to the Resources directory within the suite directory for the given suite. Although SenseTalk can access files stored elsewhere on the local file system, using the Resources directory provides additional capabilities. See the Resources Pane for more information.
Excel color functions support only index and RGB colors. If a function shown in this section reads and processes an unsupported font color, it returns a color value of 0,0,1
.
To directly read data from or write data to an Excel file, use these functions and properties:
The Workbook
Function identifies the workbook file and provides a starting point for accessing it using the other functions.
The Worksheet
Function identifies a specific worksheet within a workbook.
The Cell
Function gives access to the value and properties of a specific cell within a worksheet.
The CellRange
Function lets you quickly access multiple cells in a range of rows, columns, or rectangular region of a worksheet.
Creating Multiple Connections to an Excel File
While Eggplant Functional allows you to create more than one connection to an Excel file, doing so can lead to issues and errors. For example, if you connect to an Excel file as both a workbook (described on this page) and as a database (described here), Eggplant Functional might return an error message like InternalWorkbookError - data source needed but no workbook value
. You can fix this error by closing one of the connections.
Workbook
Function
Behavior: The Workbook
function is the starting point for direct access to an Excel spreadsheet. This function accepts one parameter, which is the path name to an Excel file with the .xlsx
file extension, and returns a Workbook object which holds a reference to the workbook file.
Parameters: The path name to an Excel file.
Syntax:
Workbook( ExcelFileName )
Example:
set MyExcelFile to Workbook(ResourcePath("TestCases.xlsx")) // Sets MyExcelFile as a reference variable for the TestCases.xlsx Excel file
Example:
set MyExcelFile to Workbook("TestCases.xlsx") // Sets MyExcelFile as a reference variable for the TestCases.xlsx Excel file. The workbook() function looks in the default suite directory for the TestCases.xlsx Excel file
Use this Workbook object reference with the Worksheet
, Cell
or CellRange
functions to access the contents of an Excel file, or to obtain other information about the workbook.
Workbook Properties
The following read-only properties are available for Workbook objects:
Property | Definition |
---|---|
worksheetCount | The number of worksheets contained in a workbook. Read-only. |
worksheetNames | A list of the names of all of the worksheets in a workbook. Read-only. |
Example:
put workbook(excelFilePath) into myWorkbook
put myWorkbook is a workbook --> True (myWorkbook is a workbook object)
put myWorkbook's worksheetCount --> 3
put the worksheetNames of myWorkbook --> ["People","Departments","Facilities"]
Related:
- Worksheet Function: Use this function to access a specific worksheet within a workbook.
- Cell Function: Use this function to access the value or properties of an individual cell.
- CellRange Function: Use this function to access a range of cells, rows, or columns.
Worksheet Function
Behavior: The Worksheet
function gives access to a particular worksheet within a workbook. This function returns a Worksheet object which refers to a specific worksheet.
Parameters: A workbook reference and a sheet identifier. The workbook reference can be passed as the first parameter to the function (the first syntax below), or the Worksheet function can be called as a function of the workbook object (the second and third syntaxes below). The sheet identifier can be either the name of the sheet or its number (1 for the first sheet in the workbook, 2 for the second, etc.).
Syntax:
worksheet( workbook, sheetIdentifier )
worksheet( sheetIdentifier ) of workbook
workbook .worksheet( sheetIdentifier )
Example:
set worksheet2 to Worksheet(MyExcelFile, 2) // Sets worksheet2 to refer to the second worksheet in the Excel file
Example:
set custBudget to MyExcelFile.Worksheet("Customer Budget") // Sets custBudget to refer to the worksheet named "Customer Budget"
Worksheet Properties
The following properties are available for Worksheet objects:
Property | Definition |
---|---|
name | The name of a sheet within a workbook. |
workbook | The workbook to which a sheet belongs (a Workbook object). Read-only. |
Example:
put worksheet(1) of myWorkbook into mySheet // access the first worksheet in the workbook
put mySheet is a worksheet --> True (mySheet is a worksheet object)
put mySheet's name --> "People"
put "Special" before mySheet's name
put the worksheetNames of mySheet's workbook --> ["SpecialPeople","Departments","Facilities"]
Related:
- Workbook Function: Use this function to access an Excel workbook file.
- Cell Function: Use this function to access the value or properties of an individual cell.
- CellRange Function: Use this function to access a range of cells, rows, or columns.
Cell
Function
Behavior: Use this function to access the value or properties of an individual cell. If a workbook reference is used instead of a worksheet reference, the function uses the first worksheet in the workbook. It can be called by passing two parameters (a worksheet reference and a cell identifier), or by calling it as a function of the worksheet or workbook with the cell identifier as its parameter. The cell identifier can be a traditional cell name, such as "C6"
, or a pair of numbers identifying the row and column, such as (6,3)
.
The value returned by the Cell
function is a Cell object -- a reference to a specific cell in the worksheeet -- whose value is the cell contents. The cell object's value might be a string, number, Boolean, or date, depending on what is stored in the spreadsheet cell. A cell object is also a container, allowing its value to be changed just like a variable. In addition, a cell object has a number of properties that can be accessed or changed, such as the text font and color of the spreadsheet cell.
Parameters: Pass parameters using either of the following approaches:
- Pass a worksheet reference and a cell identifier.
- Call the
Cell
function as a function of the worksheet with the cell identifier as its parameter.
Syntax:
cell( worksheetReference, cellIdentifier )
cell( cellIdentifier ) of worksheetReference
worksheetReference .cell( cellIdentifier )
Example: Accessing a cell value using a worksheet reference and a cell identifier:
put Cell(worksheet1, "A3") into cellValue1
Example: Accessing a cell value by using Cell as a function of the worksheet with the cell identifier as its parameter:
put worksheet1.Cell(4,7) into workingBalance
Example: Using a Cell object as a container to update a cell value:
add 1 to worksheet1's Cell("E3") // updates cell E3 of worksheet1
Example: Using a Cell object as a container to set the cell's value:
put the date into Cell(worksheet1, [3,5]) // stores the date into row 3, column 5 of worksheet1