Word.TableCell class

表示 Word 文档中的表格单元格。

扩展

注解

API 集:WordApi 1.3

使用方

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/40-tables/manage-formatting.yaml

// Gets content alignment details about the first cell of the first table in the document.
await Word.run(async (context) => {
  const firstTable: Word.Table = context.document.body.tables.getFirst();
  const firstTableRow: Word.TableRow = firstTable.rows.getFirst();
  const firstCell: Word.TableCell = firstTableRow.cells.getFirst();
  firstCell.load(["horizontalAlignment", "verticalAlignment"]);
  await context.sync();

  console.log(
    `Details about the alignment of the first table's first cell:`,
    `- Horizontal alignment of the cell's content: ${firstCell.horizontalAlignment}`,
    `- Vertical alignment of the cell's content: ${firstCell.verticalAlignment}`,
  );
});

属性

body

获取 Body 单元格的对象。

cellIndex

获取单元格行中的单元格索引。

column

返回 TableColumn 表示包含此单元格的表列的对象。

columnWidth

指定单元格列的宽度(以磅为单位)。 此方法适用于一致的 table。

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

horizontalAlignment

指定单元格的水平对齐方式。 值可以是 leftcenteredright 或 。justified

parentRow

获取单元格的父行。

parentTable

获取单元格的父表。

rowIndex

获取表中单元格行的索引。

shading

返回 ShadingUniversal 表示表格单元格底纹的对象。

shadingColor

指定单元格的底纹颜色。 按“#RRGGBB”格式或使用颜色名称指定颜色。

value

指定单元格的文本。

verticalAlignment

指定单元格的垂直对齐方式。 值可以是 topcenterbottom

width

获取单元格的宽度(以磅为单位)。

方法

autoSum()

插入 = (公式) 字段 ,该字段计算并显示表达式中指定单元格上方或左侧的表单元格中值的总和。

delete(shiftCells)

删除表格单元格,并选择性地控制如何移动其余单元格。

deleteColumn()

删除包含该单元格的列。 此方法适用于一致的 table。

deleteRow()

删除包含该单元格的行。

formula(options)

将包含指定公式的 = (公式) 字段 插入到表单元格中。

getBorder(borderLocation)

获取指定边框的边框样式。

getBorder(borderLocation)

获取指定边框的边框样式。

getCellPadding(cellPaddingLocation)

获取单元格填充(以磅为单位)。

getCellPadding(cellPaddingLocation)

获取单元格填充(以磅为单位)。

getNext()

获取下一个单元格。 如果此单元格是最后一个单元格,则引发 ItemNotFound 错误。

getNextOrNullObject()

获取下一个单元格。 如果此单元格是最后一个单元格,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

insertColumns(insertLocation, columnCount, values)

使用单元格的列作为模板,在单元格的左侧或右侧添加列。 此方法适用于一致的 table。 字符串值(若指定)是在新插入的行中进行设置。

insertRows(insertLocation, rowCount, values)

使用单元格的行作为模板,在单元格的上方或下方插入行。 字符串值(若指定)是在新插入的行中进行设置。

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

merge(mergeTo)

将此表单元格与指定的表单元格合并。

select()

选择表格单元格。

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

setCellPadding(cellPaddingLocation, cellPadding)

设置单元格填充(以磅为单位)。

setCellPadding(cellPaddingLocation, cellPadding)

设置单元格填充(以磅为单位)。

split(rowCount, columnCount)

将单元格拆分为指定行数和列数。

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.TableCell 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Word.Interfaces.TableCellData) ,其中包含从原始对象加载的任何子属性的浅层副本。

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync()

属性详细信息

body

获取 Body 单元格的对象。

readonly body: Word.Body;

属性值

注解

API 集:WordApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/40-tables/table-cell-access.yaml

// Gets the content of the first cell in the first table.
await Word.run(async (context) => {
  const firstCell: Word.Body = context.document.body.tables.getFirst().getCell(0, 0).body;
  firstCell.load("text");

  await context.sync();
  console.log("First cell's text is: " + firstCell.text);
});

cellIndex

获取单元格行中的单元格索引。

readonly cellIndex: number;

属性值

number

注解

API 集:WordApi 1.3

column

返回 TableColumn 表示包含此单元格的表列的对象。

readonly column: Word.TableColumn;

属性值

注解

API 集:WordApiDesktop 1.4

columnWidth

指定单元格列的宽度(以磅为单位)。 此方法适用于一致的 table。

columnWidth: number;

属性值

number

注解

API 集:WordApi 1.3

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

horizontalAlignment

指定单元格的水平对齐方式。 值可以是 leftcenteredright 或 。justified

horizontalAlignment: Word.Alignment | "Mixed" | "Unknown" | "Left" | "Centered" | "Right" | "Justified";

属性值

Word.Alignment | "Mixed" | "Unknown" | "Left" | "Centered" | "Right" | "Justified"

注解

API 集:WordApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/40-tables/manage-formatting.yaml

// Gets content alignment details about the first cell of the first table in the document.
await Word.run(async (context) => {
  const firstTable: Word.Table = context.document.body.tables.getFirst();
  const firstTableRow: Word.TableRow = firstTable.rows.getFirst();
  const firstCell: Word.TableCell = firstTableRow.cells.getFirst();
  firstCell.load(["horizontalAlignment", "verticalAlignment"]);
  await context.sync();

  console.log(
    `Details about the alignment of the first table's first cell:`,
    `- Horizontal alignment of the cell's content: ${firstCell.horizontalAlignment}`,
    `- Vertical alignment of the cell's content: ${firstCell.verticalAlignment}`,
  );
});

parentRow

获取单元格的父行。

readonly parentRow: Word.TableRow;

属性值

注解

API 集:WordApi 1.3

parentTable

获取单元格的父表。

readonly parentTable: Word.Table;

属性值

注解

API 集:WordApi 1.3

rowIndex

获取表中单元格行的索引。

readonly rowIndex: number;

属性值

number

注解

API 集:WordApi 1.3

shading

返回 ShadingUniversal 表示表格单元格底纹的对象。

readonly shading: Word.ShadingUniversal;

属性值

注解

API 集:WordApiDesktop 1.4

shadingColor

指定单元格的底纹颜色。 按“#RRGGBB”格式或使用颜色名称指定颜色。

shadingColor: string;

属性值

string

注解

API 集:WordApi 1.3

value

指定单元格的文本。

value: string;

属性值

string

注解

API 集:WordApi 1.3

verticalAlignment

指定单元格的垂直对齐方式。 值可以是 topcenterbottom

verticalAlignment: Word.VerticalAlignment | "Mixed" | "Top" | "Center" | "Bottom";

属性值

Word.VerticalAlignment | "Mixed" | "Top" | "Center" | "Bottom"

注解

API 集:WordApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/40-tables/manage-formatting.yaml

// Gets content alignment details about the first cell of the first table in the document.
await Word.run(async (context) => {
  const firstTable: Word.Table = context.document.body.tables.getFirst();
  const firstTableRow: Word.TableRow = firstTable.rows.getFirst();
  const firstCell: Word.TableCell = firstTableRow.cells.getFirst();
  firstCell.load(["horizontalAlignment", "verticalAlignment"]);
  await context.sync();

  console.log(
    `Details about the alignment of the first table's first cell:`,
    `- Horizontal alignment of the cell's content: ${firstCell.horizontalAlignment}`,
    `- Vertical alignment of the cell's content: ${firstCell.verticalAlignment}`,
  );
});

width

获取单元格的宽度(以磅为单位)。

readonly width: number;

属性值

number

注解

API 集:WordApi 1.3

方法详细信息

autoSum()

插入 = (公式) 字段 ,该字段计算并显示表达式中指定单元格上方或左侧的表单元格中值的总和。

autoSum(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

delete(shiftCells)

删除表格单元格,并选择性地控制如何移动其余单元格。

delete(shiftCells: any): void;

参数

shiftCells

any

可选。 指定删除后如何移动其余单元格。 默认值为 shiftLeft

返回

void

注解

API 集:WordApiDesktop 1.4

deleteColumn()

删除包含该单元格的列。 此方法适用于一致的 table。

deleteColumn(): void;

返回

void

注解

API 集:WordApi 1.3

deleteRow()

删除包含该单元格的行。

deleteRow(): void;

返回

void

注解

API 集:WordApi 1.3

formula(options)

将包含指定公式的 = (公式) 字段 插入到表单元格中。

formula(options?: Word.TableCellFormulaOptions): void;

参数

options
Word.TableCellFormulaOptions

可选。 指定公式选项的对象。

返回

void

注解

API 集:WordApiDesktop 1.4

getBorder(borderLocation)

获取指定边框的边框样式。

getBorder(borderLocation: Word.BorderLocation): Word.TableBorder;

参数

borderLocation
Word.BorderLocation

边框位置。

返回

注解

API 集:WordApi 1.3

getBorder(borderLocation)

获取指定边框的边框样式。

getBorder(borderLocation: "Top" | "Left" | "Bottom" | "Right" | "InsideHorizontal" | "InsideVertical" | "Inside" | "Outside" | "All"): Word.TableBorder;

参数

borderLocation

"Top" | "Left" | "Bottom" | "Right" | "InsideHorizontal" | "InsideVertical" | "Inside" | "Outside" | "All"

边框位置。

返回

注解

API 集:WordApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/40-tables/manage-formatting.yaml

// Gets border details about the first of the first table in the document.
await Word.run(async (context) => {
  const firstTable: Word.Table = context.document.body.tables.getFirst();
  const firstCell: Word.TableCell = firstTable.getCell(0, 0);
  const borderLocation = "Left";
  const border: Word.TableBorder = firstCell.getBorder(borderLocation);
  border.load(["type", "color", "width"]);
  await context.sync();

  console.log(
    `Details about the ${borderLocation} border of the first table's first cell:`,
    `- Color: ${border.color}`,
    `- Type: ${border.type}`,
    `- Width: ${border.width} points`,
  );
});

getCellPadding(cellPaddingLocation)

获取单元格填充(以磅为单位)。

getCellPadding(cellPaddingLocation: Word.CellPaddingLocation): OfficeExtension.ClientResult<number>;

参数

cellPaddingLocation
Word.CellPaddingLocation

单元格填充位置必须是 topleftbottom 或 。right

返回

注解

API 集:WordApi 1.3

getCellPadding(cellPaddingLocation)

获取单元格填充(以磅为单位)。

getCellPadding(cellPaddingLocation: "Top" | "Left" | "Bottom" | "Right"): OfficeExtension.ClientResult<number>;

参数

cellPaddingLocation

"Top" | "Left" | "Bottom" | "Right"

单元格填充位置必须是 topleftbottom 或 。right

返回

注解

API 集:WordApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/40-tables/manage-formatting.yaml

// Gets cell padding details about the first cell of the first table in the document.
await Word.run(async (context) => {
  const firstTable: Word.Table = context.document.body.tables.getFirst();
  const firstCell: Word.TableCell = firstTable.getCell(0, 0);
  const cellPaddingLocation = "Left";
  const cellPadding = firstCell.getCellPadding(cellPaddingLocation);
  await context.sync();

  console.log(
    `Cell padding details about the ${cellPaddingLocation} border of the first table's first cell: ${cellPadding.value} points`,
  );
});

getNext()

获取下一个单元格。 如果此单元格是最后一个单元格,则引发 ItemNotFound 错误。

getNext(): Word.TableCell;

返回

注解

API 集:WordApi 1.3

getNextOrNullObject()

获取下一个单元格。 如果此单元格是最后一个单元格,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getNextOrNullObject(): Word.TableCell;

返回

注解

API 集:WordApi 1.3

insertColumns(insertLocation, columnCount, values)

使用单元格的列作为模板,在单元格的左侧或右侧添加列。 此方法适用于一致的 table。 字符串值(若指定)是在新插入的行中进行设置。

insertColumns(insertLocation: Word.InsertLocation.before | Word.InsertLocation.after | "Before" | "After", columnCount: number, values?: string[][]): void;

参数

insertLocation

before | after | "Before" | "After"

它必须是 before OR after

columnCount

number

要添加的列数。

values

string[][]

可选的二维数组。 如果指定数组中的对应字符串,则填充单元格。

返回

void

注解

API 集:WordApi 1.3

insertRows(insertLocation, rowCount, values)

使用单元格的行作为模板,在单元格的上方或下方插入行。 字符串值(若指定)是在新插入的行中进行设置。

insertRows(insertLocation: Word.InsertLocation.before | Word.InsertLocation.after | "Before" | "After", rowCount: number, values?: string[][]): Word.TableRowCollection;

参数

insertLocation

before | after | "Before" | "After"

它必须是 before OR after

rowCount

number

要添加的行数。

values

string[][]

可选的二维数组。 如果指定数组中的对应字符串,则填充单元格。

返回

注解

API 集:WordApi 1.3

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: Word.Interfaces.TableCellLoadOptions): Word.TableCell;

参数

options
Word.Interfaces.TableCellLoadOptions

为要加载的对象属性提供选项。

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): Word.TableCell;

参数

propertyNames

string | string[]

指定要加载的属性的逗号分隔的字符串或字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Word.TableCell;

参数

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。

返回

merge(mergeTo)

将此表单元格与指定的表单元格合并。

merge(mergeTo: Word.TableCell): void;

参数

mergeTo
Word.TableCell

要合并的单元格。

返回

void

注解

API 集:WordApiDesktop 1.4

select()

选择表格单元格。

select(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties: Interfaces.TableCellUpdateData, options?: OfficeExtension.UpdateOptions): void;

参数

properties
Word.Interfaces.TableCellUpdateData

一个 JavaScript 对象,其属性与调用该方法的对象的属性同构结构。

options
OfficeExtension.UpdateOptions

提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止错误。

返回

void

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

set(properties: Word.TableCell): void;

参数

properties
Word.TableCell

返回

void

setCellPadding(cellPaddingLocation, cellPadding)

设置单元格填充(以磅为单位)。

setCellPadding(cellPaddingLocation: Word.CellPaddingLocation, cellPadding: number): void;

参数

cellPaddingLocation
Word.CellPaddingLocation

单元格填充位置必须是 topleftbottom 或 。right

cellPadding

number

单元格填充。

返回

void

注解

API 集:WordApi 1.3

setCellPadding(cellPaddingLocation, cellPadding)

设置单元格填充(以磅为单位)。

setCellPadding(cellPaddingLocation: "Top" | "Left" | "Bottom" | "Right", cellPadding: number): void;

参数

cellPaddingLocation

"Top" | "Left" | "Bottom" | "Right"

单元格填充位置必须是 topleftbottom 或 。right

cellPadding

number

单元格填充。

返回

void

注解

API 集:WordApi 1.3

split(rowCount, columnCount)

将单元格拆分为指定行数和列数。

split(rowCount: number, columnCount: number): void;

参数

rowCount

number

要拆分为的行数。 必须是基础行数的除数。

columnCount

number

要拆分为的列数。

返回

void

注解

API 集:WordApi 1.4

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.TableCell 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Word.Interfaces.TableCellData) ,其中包含从原始对象加载的任何子属性的浅层副本。

toJSON(): Word.Interfaces.TableCellData;

返回

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。

track(): Word.TableCell;

返回

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync()

untrack(): Word.TableCell;

返回