Active workbook

Updated at:

Retrieves the active workbook object and its name collection, and adds worksheets to a spreadsheet file.

Get the active workbook

Retrieves a workbook object that represents the active workbook.

  • Syntax

    Expression.ActiveWorkbook

    Expression: the document type application object.

  • Example

    The following code retrieves the active workbook object.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Active workbook.
      const activeWorkbook = await app.ActiveWorkbook;
    }

Get the workbook name collection

Retrieves the collection of all names defined in the active workbook.

  • Syntax

    Expression.ActiveWorkbook.Names

    Expression: the document type application object.

  • Example

    The following code retrieves the Names collection from the active workbook.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Active workbook.
      const activeWorkbook = await app.ActiveWorkbook;
    
      // A collection of workbook names.
      const names = await activeWorkbook.Names;
    }

Worksheet

Get worksheets

  • Syntax

    Expression.ActiveWorkbook.Sheets

    Expression: the document type application object.

  • Example

    The following code retrieves the Sheets collection from the active workbook.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // The worksheet collection.
      const sheets = await app.ActiveWorkbook.Sheets;
    }

Add a worksheet

  • Syntax

    Expression.ActiveWorkbook.Sheets.Add({ Before, After, Count, Type, Name })

    Expression: the document type application object.

  • Parameters

    Attribute

    Data type

    Required

    Description

    Before

    String/Number

    Yes

    The worksheet before which the new worksheet is inserted.

    After

    String/Number

    No

    The worksheet after which the new worksheet is inserted.

    Count

    Number

    No

    The number of worksheets to add. Defaults to the number of currently selected sheets.

    Type

    Enum

    No

    The worksheet type. Valid values of Enum.XlSheetType:

    • -4167 or xlWorksheet: worksheet.

    • -4116 or xlDialogSheet: dialog sheet.

    • -4109 or xlChart: chart.

    • 3 or xlExcel4MacroSheet: Excel version 4 macro sheet.

    • 4 or xlExcel4IntlMacroSheet: Excel version 4 international macro sheet.

    Name

    Name

    No

    The name of the new worksheet.

  • Example

    The following code adds a new worksheet named new worksheet to the active workbook.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // The worksheet collection.
      const sheets = await app.ActiveWorkbook.Sheets;
    
      // Add a worksheet.
      await sheets.Add(null, null, 1, app.Enum.XlSheetType.xlWorksheet, 'new worksheet');
    }

Get the worksheet count

  • Syntax

    Expression.ActiveWorkbook.Sheets.Count

    Expression: the document type application object.

  • Example

    The following code retrieves the total number of worksheets in the active workbook and prints it to the console.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // The worksheet collection.
      const sheets = await app.ActiveWorkbook.Sheets;
    
      // The number of worksheets.
      const count = await sheets.Count;
      console.log(count);
    }

Get the worksheet type

  • Syntax

    Expression.ActiveWorkbook.Sheets.Item(Index).Type

    Expression: the document type application object.

  • Example

    The following code retrieves the type of the second worksheet and prints it to the console.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // The index of the worksheet. Indexes start from 1.
      const sheetIndex = 2;
      const sheet = await app.ActiveWorkbook.Sheets.Item(sheetIndex);
    
      // The worksheet type.
      const type = await sheet.Type;
      console.log(type);
    }

Get a single worksheet

Retrieves a specific worksheet from the active workbook by index.

Important

A worksheet object retrieved this way can be used in the same way as the active sheet.

  • Syntax

    Expression.ActiveWorkbook.Sheets.Item(Index)

    Expression: the document type application object.

  • Example

    The following code retrieves the first worksheet in the active workbook.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // The first worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.Sheets.Item(1);
    }

Enable or disable multi-person synchronous filtering

  • Syntax

    Expression.ActiveWorkbook.SetFilterShared({ Checked })

    Expression: the document type application object.

  • Parameter

    Attribute

    Data type

    Required

    Description

    Checked

    Boolean

    Yes

    Specifies whether to enable multi-person synchronous filtering.

    • true: enable.

    • false: disable.

  • Example

    The following code enables multi-person synchronous filtering on the active workbook.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Active workbook.
      const activeWorkbook = await app.ActiveWorkbook;
    
      // Enable multi-person synchronous filtering.
      const result = await activeWorkbook.SetFilterShared(true);
    }