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.
You can update and write values back to an Excel spreadsheet file, and 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.
Interactions with Excel databases occur through several special types of objects which serve as references to different elements within an Excel file. A workbook object refers to the workbook as a whole, and serves as the starting point for all access to the workbook file. A worksheet object is a reference to a single worksheet within a workbook. A cell object is a reference to a single cell within a worksheet.
Because these objects act as references to the respective parts of an Excel workbook file, changing any property of one of these objects or the value of a cell will change the corresponding aspect of the workbook itself. When passing one of these objects as a parameter to a command or function, you must explicitly pass it by reference if you want to access the workbook from the called handler. Otherwise SenseTalk will make a copy of the object and the value received by the called handler will no longer have a connection to the workbook.
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.
File References
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.
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.
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.
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 Object Properties
The following properties are available for Workbook objects:
| Property | Definition |
|---|---|
autoSave | Defaults to True. When set, changes made to any values or properties within the workbook or its worksheets will be saved to the workbook file immediately. Turn this property off to buffer changes until the property is turned on again, or the workbook is explicitly saved with a save changes command. |
hasUnsavedChanges | True when the autoSave property is off and there are pending changes which have not been saved to the file. Read-only. |
colorMode | Defaults to "RGB" in order to interact with the workbook in Excel's RGB mode, but can be set to "RGB" or "Palette". In Palette mode, a "NoColor" value can be specified for elements that don’t have a color. Note Most common use will be to leave the colorMode set to "RGB". The main purpose of switching to Palette mode is to test for or set a color to "NoColor" which is not available in RGB mode. When in Palette mode, a limited number of colors are supported, so setting a color can result in a slightly different color. |
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 showing worksheetCount and worksheetNames:
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:
WorksheetFunction: Use this function to access a specific worksheet within a workbook.CellFunction: Use this function to access the value or properties of an individual cell.CellRangeFunction: 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 Object Properties
The following properties are available for Worksheet objects:
| Property | Definition |
|---|---|
name | The name of a sheet within a workbook. This property can be set to rename the worksheet. |
workbook | The workbook to which a sheet belongs (a Workbook object). Read-only. |
sequenceNumber | Each worksheet has a sequenceNumber property which indicates its sequence within the workbook. The first worksheet has a sequenceNumber of 1. You may set the sequenceNumber to a different value to move the worksheet to a different ordered position within the workbook. |
hidden | The hidden property of a worksheet indicates whether it is shown to the user or not. When retrieving the hidden property its value will be True (if the worksheet is hidden) or False (if it is visible). When setting the hidden property, it may be set to True or False, or to “Very” to set the worksheet to be “Very Hidden”. |
hidden | The hidden property of a worksheet indicates whether it is shown to the user or not. When retrieving the hidden property its value will be True (if the worksheet is hidden) or False (if it is visible). When setting the hidden property, it may be set to True or False, or to “Very” to set the worksheet to be “Very Hidden”. |
tabColor | The tabColor property of a worksheet specifies the color of the tab for that sheet. |
hyperlinks | The hyperlinks property of a worksheet is a read-only property which returns a list of all of the hyperlinks in the worksheet. Each hyperlink is returned as a property list with properties hyperlink, row, and column whose values are the link URL, and the row and column number of the cell. Related: Hyperlink Property of a cell |
Example of the name property:
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"]