Word.TableCell class

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

Extends

注解

[ 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 = context.document.body.tables.getFirst().getCell(0, 0).body;
  firstCell.load("text");

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

属性

body

获取单元格的 body 对象。

cellIndex

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

columnWidth

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

context

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

horizontalAlignment

指定单元格的水平对齐方式。 该值可以是“Left”、“Centered”、“Right”或“Justified”。

parentRow

获取单元格的父行。

parentTable

获取单元格的父表。

rowIndex

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

shadingColor

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

value

指定单元格的文本。

verticalAlignment

指定单元格的垂直对齐方式。 该值可以是“Top”、“Center”或“Bottom”。

width

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

方法

deleteColumn()

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

deleteRow()

删除包含该单元格的行。

getBorder(borderLocation)

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

getBorder(borderLocationString)

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

getCellPadding(cellPaddingLocation)

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

getCellPadding(cellPaddingLocationString)

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

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()

set(properties, options)

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

set(properties)

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

setCellPadding(cellPaddingLocation, cellPadding)

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

setCellPadding(cellPaddingLocationString, 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 ]

cellIndex

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

readonly cellIndex: number;

属性值

number

注解

[ API 集:WordApi 1.3 ]

columnWidth

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

columnWidth: number;

属性值

number

注解

[ API 集:WordApi 1.3 ]

context

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

context: RequestContext;

属性值

horizontalAlignment

指定单元格的水平对齐方式。 该值可以是“Left”、“Centered”、“Right”或“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 = context.document.body.tables.getFirst();
  const firstCell = firstTable.getCell(0, 0);
  firstCell.load(["horizontalAlignment", "verticalAlignment"]);
  await context.sync();

  console.log(`Details about the alignment of the first table's first cell:`);
  console.log(`- Horizontal alignment of the cell's content: ${firstCell.horizontalAlignment}`);
  console.log(`- 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 ]

shadingColor

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

shadingColor: string;

属性值

string

注解

[ API 集:WordApi 1.3 ]

value

指定单元格的文本。

value: string;

属性值

string

注解

[ API 集:WordApi 1.3 ]

verticalAlignment

指定单元格的垂直对齐方式。 该值可以是“Top”、“Center”或“Bottom”。

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 = context.document.body.tables.getFirst();
  const firstCell = firstTable.getCell(0, 0);
  firstCell.load(["horizontalAlignment", "verticalAlignment"]);
  await context.sync();

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

width

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

readonly width: number;

属性值

number

注解

[ API 集:WordApi 1.3 ]

方法详细信息

deleteColumn()

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

deleteColumn(): void;

返回

void

注解

[ API 集:WordApi 1.3 ]

deleteRow()

删除包含该单元格的行。

deleteRow(): void;

返回

void

注解

[ API 集:WordApi 1.3 ]

getBorder(borderLocation)

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

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

参数

borderLocation
Word.BorderLocation

必需。 边框位置。

返回

注解

[ API 集:WordApi 1.3 ]

getBorder(borderLocationString)

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

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

参数

borderLocationString

"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 = context.document.body.tables.getFirst();
  const firstCell = firstTable.getCell(0, 0);
  const borderLocation = "Left";
  const border = 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:`);
  console.log(`- Color: ${border.color}`);
  console.log(`- Type: ${border.type}`);
  console.log(`- Width: ${border.width} points`);
});

getCellPadding(cellPaddingLocation)

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

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

参数

cellPaddingLocation
Word.CellPaddingLocation

必填。 单元格填充位置必须为“Top”、“Left”、“Bottom”或“Right”。

返回

注解

[ API 集:WordApi 1.3 ]

getCellPadding(cellPaddingLocationString)

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

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

参数

cellPaddingLocationString

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

必填。 单元格填充位置必须为“Top”、“Left”、“Bottom”或“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 = context.document.body.tables.getFirst();
  const firstCell = 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”或“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”或“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 一个逗号分隔的字符串,指定要加载的导航属性。

返回

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

必填。 单元格填充位置必须为“Top”、“Left”、“Bottom”或“Right”。

cellPadding

number

必填。 单元格填充。

返回

void

注解

[ API 集:WordApi 1.3 ]

setCellPadding(cellPaddingLocationString, cellPadding)

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

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

参数

cellPaddingLocationString

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

必填。 单元格填充位置必须为“Top”、“Left”、“Bottom”或“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;

返回