SheetByIndex Property

Applies to TestComplete 15.47, last modified on January 20, 2023

Note: To work with Excel files in your tests, you do not need to have Microsoft Office Excel installed on your computer.

Description

The SheetByIndex property returns the ExcelSheet object that provides access to the Excel sheet specified by its index.

Declaration

ExcelFileObj.SheetByIndex(SheetIndex)

Read-Only Property An ExcelSheet object
ExcelFileObj An expression, variable or parameter that specifies a reference to an ExcelFile object
SheetIndex [in]    Required    Integer    

Applies To

The property is applied to the following object:

Parameters

The property has the following parameter:

SheetIndex

A zero-based index of the desired sheet.

Property Value

An ExcelSheet object that corresponds to the sheet with the specified index. To perform operations over the Excel sheet, use the methods and properties of the returned object.

If the sheet with the specified index does not exist in the file, the property will return the null value (Nothing in VBScript, nil in DelphiScript, None in Python, null in JavaScript, JScript, C++Script and C#Script) and post an error message to the test log. To get the number of sheets that the file contains, use the ExcelFile.SheetCount property.

Example

The code below demonstrates how you can use the SheetByIndex property in your script:

JavaScript, JScript

function ExcelExample()
{
  // Get the data that will be written into an Excel file
  var curTime = aqDateTime.Now();
  var cpu = Sys.CPUUsage;

  var fileName = "C:\\temp\\MyFile.xlsx";

  if (aqFile.Exists(fileName))
  {
    // Open the existing Excel file
    var excelFile = Excel.Open(fileName);

    if (excelFile.SheetCount > 0)
    {
    // Get the first sheet of the Excel file
    var excelSheet = excelFile.SheetByIndex(0);

    // Write the data into a new row of the Excel file
    var rowIndex = excelSheet.RowCount + 1;
    excelSheet.Cell("A", rowIndex).Value = curTime;
    excelSheet.Cell(2, rowIndex).Value = cpu;
    }
    else
    {
      Log.Error(aqString.Format("The %s Excel file does not contain any sheets.", fileName));
    }
  }
  else
  {
    // 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("Sheet1");

    // Write the data into the first row of the created file
    excelSheet.Cell("A", 1).Value = curTime;
    excelSheet.Cell(2, 1).Value = cpu;
  }

  // Save the file to apply the changes
  excelFile.Save();
}

Python

def ExcelExample():
  # Get the data that will be written into an Excel file
  curTime = aqDateTime.Now()
  cpu = Sys.CPUUsage

  fileName = "C:\\temp\\MyFile.xlsx"

  if (aqFile.Exists(fileName)):
    # Open the existing Excel file
    excelFile = Excel.Open(fileName)

    if (excelFile.SheetCount > 0):
      # Get the first sheet of the Excel file
      excelSheet = excelFile.SheetByIndex[0]

      # Write the data into a new row of the Excel file
      rowIndex = excelSheet.RowCount + 1
      excelSheet.Cell["A", rowIndex].Value = curTime
      excelSheet.Cell[2, rowIndex].Value = cpu
    else:
      Log.Error(aqString.Format("The %s Excel file does not contain any sheets.", fileName))
  else:
    # 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("Sheet1")

    # Write the data into the first row of the created file
    excelSheet.Cell["A", 1].Value = curTime
    excelSheet.Cell[2, 1].Value = cpu

  # Save the file to apply the changes
  excelFile.Save()

VBScript

Sub ExcelExample()

  ' Get the data that will be written into an Excel file
  Set curTime = aqDateTime.Now()
  cpu = Sys.CPUUsage

  fileName = "C:\\temp\\MyFile.xlsx"

  If aqFile.Exists(fileName) Then
    ' Open the existing Excel file
    Set excelFile = Excel.Open(fileName)

    If excelFile.SheetCount > 0 Then
    ' Get the first sheet of the Excel file
    Set excelSheet = excelFile.SheetByIndex(0)

    ' Write the data into a new row of the Excel file
    rowIndex = excelSheet.RowCount + 1
    excelSheet.Cell("A", rowIndex).Value = curTime
    excelSheet.Cell(2, rowIndex).Value = cpu
    Else
      Log.Error(aqString.Format("The %s Excel file does not contain any sheets.", fileName))
    End If
  Else
    ' 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))
    Set excelFile = Excel.Create(fileName)
    Set excelSheet = excelFile.AddSheet("Sheet1")

    ' Write the data into the first row of the created file
    excelSheet.Cell("A", 1).Value = curTime
    excelSheet.Cell(2, 1).Value = cpu
  End If

  ' Save the file to apply the changes
  excelFile.Save()
End Sub

DelphiScript

procedure ExcelExample();
var curTime, cpu, fileName, excelFile, excelSheet, rowIndex;
begin
  // Get the data that will be written into an Excel file
  curTime := aqDateTime.Now();
  cpu := Sys.CPUUsage;

  fileName := 'C:\\temp\\MyFile.xlsx';

  if aqFile.Exists(fileName) then
  begin
    // Open the existing Excel file
    excelFile := Excel.Open(fileName);

    if excelFile.SheetCount > 0 then
    begin
    // Get the first sheet of the Excel file
    excelSheet := excelFile.SheetByIndex(0);

    // Write the data into a new row of the Excel file
    rowIndex := excelSheet.RowCount + 1;
    excelSheet.Cell('A', rowIndex).Value := curTime;
    excelSheet.Cell(2, rowIndex).Value := cpu;
    end
    else
      Log.Error(aqString.Format('The %s Excel file does not contain any sheets.', fileName));
  end
  else
  begin
    // 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('Sheet1');

    // Write the data into the first row of the created file
    excelSheet.Cell('A', 1).Value := curTime;
    excelSheet.Cell(2, 1).Value := cpu;
  end;

  // Save the file to apply the changes
  excelFile.Save();
end;

C++Script, C#Script

function ExcelExample()
{
  // Get the data that will be written into an Excel file
  var curTime = aqDateTime.Now();
  var cpu = Sys.CPUUsage;

  var fileName = "C:\\temp\\MyFile.xlsx";

  if (aqFile["Exists"](fileName))
  {
    // Open the existing Excel file
    var excelFile = Excel["Open"](fileName);

    if (excelFile["SheetCount"] > 0)
    {
    // Get the first sheet of the Excel file
    var excelSheet = excelFile["SheetByIndex"](0);

    // Write the data into a new row of the Excel file
    var rowIndex = excelSheet["RowCount"] + 1;
    excelSheet["Cell"]("A", rowIndex)["Value"] = curTime;
    excelSheet["Cell"](2, rowIndex)["Value"] = cpu;
    }
    else
    {
      Log["Error"](aqString["Format"]("The %s Excel file does not contain any sheets.", fileName));
    }
  }
  else
  {
    // 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"]("Sheet1");

    // Write the data into the first row of the created file
    excelSheet["Cell"]("A", 1)["Value"] = curTime;
    excelSheet["Cell"](2, 1)["Value"] = cpu;
  }

  // Save the file to apply the changes
  excelFile["Save"]();
}

See Also

ExcelFile Object
Working with Microsoft Excel Files

Highlight search results