Name management

更新时间:
复制 MD 格式

This topic describes how to obtain a name collection, add names, and delete a single name when you use a table document.

Obtain name objects

Obtain a name collection that represents the names of all worksheets.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Names

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain a name collection that represents the names of all worksheets.
      const names = await activeSheet.Names;
    }

Add a name

Add a name for a range of cells.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Names.Add({ Name, RefersTo })

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Name

    String

    Yes

    The name you want to add.

    RefersTo

    Number

    No

    The content that the name refers to.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain the name collection of the worksheet.
      const names = await activeSheet.Names;
    
      // Add a name.
      names.Add ('New table', 'A1');
    }

Obtain the number of names

Obtain the number of names of a worksheet.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Names.Count

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain the name collection of the worksheet.
      const names = await activeSheet.Names;
    
      // Obtain the number of names.
      const count = await names.Count;
      console.log(count);
    }

A single name

Obtain a single name object

Obtain the name that is represented by a specified index.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Names.Item(Index)

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain the name objects.
      const names = await activeSheet.Names;
    
      // Obtain a single name object.
      const name = await names.Item(1);
    }

View a specific name

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Names.Item(Index).Name

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain the name objects.
      const names = await activeSheet.Names;
    
      // Obtain a single name object.
      const name = await names.Item(1);
    
      // View the name.
      const nameName = await name.Name;
      console.log(nameName);
    }

Obtain the value of a name

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Names.Item(Index).Value

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain the name objects.
      const names = await activeSheet.Names;
    
      // Obtain a single name object.
      const name = await names.Item(1);
    
      // Obtain the value of the name.
      const value = await name.Value;
      console.log(value);
    }

Delete a specific name

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Names.Item(Index).Delete()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain the name objects.
      const names = await activeSheet.Names;
    
      // Obtain a single name object.
      const name = await names.Item(1);
    
      // Delete the name.
      await name.Delete();
    }