Tables

更新时间:
复制 MD 格式

This topic describes how to obtain table objects, delete a single table, and set the row height and column width of a cell when you use a text document.

Obtain the table objects

Obtain all table objects in a document.

  • Syntax

    Expression.ActiveDocument.Tables

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    }

Insert a table

  • Syntax:

    Expression.ActiveDocument.Tables.Add({ Range, NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior })

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Range

    Object

    Yes

    The range where you want to insert the table.

    Syntax: Expression.ActiveDocument.Selection.Range

    NumRows

    Number

    Yes

    The number of rows in the new table.

    NumColumns

    Number

    Yes

    The number of columns in the new table.

    DefaultTableBehavior

    Number

    No

    Specifies whether the cells are automatically resized to fit the contents of the cells. Valid values of Enum.WdDefaultTableBehavior:

    • 0 or wdWord8TableBehavior (default): disables autofit.

    • 1 or wdWord9TableBehavior: enables autofit.

    AutoFitBehavior

    Number

    No

    Specifies how the table is resized to fit the content. Valid values of Enum.WdAutoFitBehavior:

    • 0 or wdAutoFitFixed (default): The table is set to a fixed size regardless of the content, and is not automatically sized.

    • 1 or wdAutoFitContent: The table is automatically resized to fit the content in the table.

    • 2 or wdAutoFitWindow: The table is automatically resized to the width of the active window.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Insert a table.
      await tables.Add(
        app.ActiveDocument.Selection.Range, // Specify the range where you want to insert the table.
        3, // The number of rows in the new table.
        3, // The number of columns in the new table.
        1, // Enable autofit.
        1, // Automatically resize the table to fit the content in the table.
      );
    }

Obtain the number of tables

Obtain the number of tables in the document.

  • Syntax

    Expression.ActiveDocument.Tables.Count

    Expression: the document type application object.

  • Return value

    Return a number that represents the number of tables in the document.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the number of tables in the document.
      const count = await tables.Count;
      console.log(count);
    }

A single table

Obtain a single table object

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index)

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Index

    Number

    Yes

    The index of the table object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    }

Delete a table

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Delete()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Delete the first table.
      await tableOne.Delete();
    }

Obtain the range of a table

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Range

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain the range of the first table.
      const range = await tableOne.Range;
    }

Columns

Obtain the column objects

Obtain all column objects of a specified table.

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Columns

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all columns of the first table.
      const columns = tableOne.Columns;
    }

Obtain the number of columns

Obtain the number of columns in a specified table.

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Columns.Count

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all columns of the table.
      const columns = await tableOne.Columns;
    
      // Obtain the number of columns in the table.
      const count = await columns.Count;
      console.log(count);
    }

Insert a column

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Columns.Add(BeforeColumn)

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    BeforeColumn

    String

    Yes

    The column that appears immediately to the right of the new column.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all columns of the table.
      const columns = await tableOne.Columns;
    
      // Insert a column.
      await columns.Add(1);
    }

A single column

Obtain a single column object

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Columns.Item(Index)

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Index

    Number

    Yes

    The index of the column.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all columns of the first table.
      const columns = await tableOne.Columns;
    
      // Obtain the first column of the table.
      const columnOne = await columns.Item(1);
    }

Delete a column

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Columns.Item(Index).Delete()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all columns of the first table.
      const columns = await tableOne.Columns;
    
      // Obtain the first column of the table.
      const columnOne = await columns.Item(1);
    
      // Delete the first column.
      columnOne.Delete();
    }

Set the width of a column

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Columns.Item(Index).SetWidth()

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    ColumnWidth

    Number

    Yes

    The width of the specified column in points.

    RulerStyle

    Enum

    No

    The way in which the width of a cell is adjusted. Valid values of Enum.WdRulerStyle:

    • 0 or wdAdjustNone: adjusts the left edge of the row or rows, preserving the width of all columns by shifting them to the left or right. This is the default value.

    • 1 or wdAdjustProportional: adjusts the left edge of the first column, preserving the position of the right edge of the table by proportionally adjusting the widths of all the cells in the specified row or rows.

    • 2 or wdAdjustFirstColumn: adjusts the left edge of the first column only, preserving the positions of the other columns and the right edge of the table.

    • 3 or wdAdjustSameWidth: adjusts the left edge of the first column, preserving the position of the right edge of the table by setting the widths of all the cells in the specified row or rows to the same value.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all columns of the first table.
      const columns = await tableOne.Columns;
    
      // Obtain the first column of the table.
      const columnOne = await columns.Item(1);
    
      // Adjust the width of the first column.
      columnOne.SetWidth(50);
    }

Obtain the range object of a column

  • Syntax

    Expression. ActiveDocument.Tables.Item(Index).Columns.Item(Index).Range

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all columns of the first table.
      const columns = await tableOne.Columns;
    
      // Obtain the first column of the table.
      const columnOne = await columns.Item(1);
    
      // Obtain the range object of the first column.
      const range = columnOne.Range;
    }

Rows

Obtain the row objects

Obtain all row objects of a specified table.

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).rows

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all rows of the first table.
      const columns = tableOne.Rows;
    }

Obtain the number of rows

Obtain the number of rows in a specified table.

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Count

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all rows of the first table.
      const rows = await tableOne.Rows;
    
      // Obtain the number of rows in the table.
      const count = await Rows.Count;
      console.log(count);
    }

Insert a row

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Add(BeforeRow)

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    BeforeRow

    String

    Yes

    The row that appears immediately below the new row.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all rows of the first table.
      const rows = await tableOne.Rows;
    
      // Insert a row.
      await Rows.Add(1);
    }

A single row

Obtain a single row object

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index)

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Index

    Number

    Yes

    The index of the row.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all rows of the first table.
      const rows = await tableOne.Rows;
    
      // Obtain the first row of the table.
      const rowOne = await rows.Item(1);
    }

Delete a row

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Delete()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all rows of the first table.
      const rows = await tableOne.Rows;
    
      // Obtain the first row of the table.
      const rowOne = await rows.Item(1);
    
      // Delete the first row.
      rowOne.Delete();
    }

Set the height of a row

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).SetHeight()

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    RowHeight

    Number

    Yes

    The height of the row in points.

    HeightRule

    Enum

    No

    The rule that is used to determine the height of the specified row. Valid values of Enum.WdRowHeightRule:

    • 0 or wdRowHeightAuto: The row height is adjusted to fit the tallest value in the row. This is the default value.

    • 1 or wdRowHeightAtLeast: The row height is at least a minimum specified value.

    • 2 or wdRowHeightExactly: The row height is an exact value.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all rows of the first table.
      const rows = await tableOne.Rows;
    
      // Obtain the first row of the table.
      const rowOne = await rows.Item(1);
    
      // Adjust the height of the first row.
      rowOne.SetHeight(50);
    }

Obtain the range object of a row

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Range

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain all tables.
      const tables = await app.ActiveDocument.Tables;
    
      // Obtain the first table.
      const tableOne = await tables.Item(1);
    
      // Obtain all rows of the first table.
      const rows = await tableOne.Rows;
    
      // Obtain the first row of the table.
      const rowOne = await rows.Item(1);
    
      // Obtain the range object of the first row.
      const range = rowOne.Range;
    }

Cells

Obtain the cell objects

Obtain a collection of cell objects in a table column, table row, selection, or range.

  • Syntax

    • Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells

    • Expression.ActiveDocument.Tables.Item(Index).Columns.Item(Index).Cells

      Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the first table.
      const tableOne = await app.ActiveDocument.Tables.Item(1);
    
      // Obtain the text of the first cell in the first row of the table.
      const rowText = await tableOne.Rows.Item(1).Cells.Item(1).Range.Text;
      console.log(rowText);
    
      // Obtain the text of the second cell in the second column of the table.
      const columnText = await tableOne.columns.Item(2).Cells.Item(2).Range.Text;
      console.log(columnText);
    }

A single cell

Obtain a single cell object

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index)

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Index

    String

    Yes

    The index of the cell.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the first table.
      const tableOne = await app.ActiveDocument.Tables.Item(1);
    
      // Obtain the first cell in the first row of the table.
      const rowText = await tableOne.Rows.Item(1).Cells.Item(1);
    }

Delete a cell

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Delete()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the first table.
      const tableOne = await app.ActiveDocument.Tables.Item(1);
    
      // Obtain the first cell in the first row of the table.
      const cellOne = await tableOne.Rows.Item(1).Cells.Item(1);
      
      // Delete the row that contains the cell.
      await cellOne.Delete();
    }

Set the row width of a cell

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).SetWidth()

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    RowWidth

    Number

    Yes

    The width of the row in points.

    HeightRule

    Enum

    No

    The rule that is used to determine the height of the specified row. Valid values of Enum.WdRowHeightRule:

    • 0 or wdRowHeightAuto (default): The row height is adjusted to fit the tallest value in the row.

    • 1 or wdRowHeightAtLeast: The row height is at least a minimum specified value.

    • 2 or wdRowHeightExactly: The row height is an exact value.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the first table.
      const tableOne = await app.ActiveDocument.Tables.Item(1);
    
      // Obtain the first cell in the first row of the table.
      const cellOne = await tableOne.Rows.Item(1).Cells.Item(1);
      
      // Adjust the width of the cell.
      await cellOne.SetWidth(50);
    }

Set the row height of a cell

  • Syntax

    Expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).SetHeight()

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    RowHeight

    Number

    Yes

    The height of the row in points.

    HeightRule

    Enum

    No

    The rule that is used to determine the height of the specified row. Valid values of Enum.WdRowHeightRule:

    • 0 or wdRowHeightAuto (default): The row height is adjusted to fit the tallest value in the row.

    • 1 or wdRowHeightAtLeast: The row height is at least a minimum specified value.

    • 2 or wdRowHeightExactly: The row height is an exact value.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Obtain the first table.
      const tableOne = await app.ActiveDocument.Tables.Item(1);
    
      // Obtain the first cell in the first row of the table.
      const cellOne = await tableOne.Rows.Item(1).Cells.Item(1);
      
      // Adjust the height of the cell.
      await cellOne.SetHeight(50);
    }