Word.TableCell class
表示Word文档中的表格单元格。
注解
示例
// 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 | 获取 |
| cell |
获取单元格行中的单元格索引。 |
| column |
指定单元格列的宽度(以磅为单位)。 此方法适用于一致的 table。 |
| context | 与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。 |
| horizontal |
指定单元格的水平对齐方式。 该值可以是 |
| parent |
获取单元格的父行。 |
| parent |
获取单元格的父表。 |
| row |
获取表中单元格行的索引。 |
| shading |
指定单元格的底纹颜色。 按“#RRGGBB”格式或使用颜色名称指定颜色。 |
| value | 指定单元格的文本。 |
| vertical |
指定单元格的垂直对齐方式。 该值可以是 |
| width | 获取单元格的宽度(以磅为单位)。 |
方法
| delete |
删除包含该单元格的列。 此方法适用于一致的 table。 |
| delete |
删除包含该单元格的行。 |
| get |
获取指定边框的边框样式。 |
| get |
获取指定边框的边框样式。 |
| get |
获取单元格填充(以磅为单位)。 |
| get |
获取单元格填充(以磅为单位)。 |
| get |
获取下一个单元格。 如果此单元格是最后一个 |
| get |
获取下一个单元格。 如果此单元格是最后一个单元格,则此方法将返回一个 对象,其 |
| insert |
使用单元格的列作为模板,在单元格的左侧或右侧添加列。 此方法适用于一致的 table。 字符串值(若指定)是在新插入的行中进行设置。 |
| insert |
使用单元格的行作为模板,在单元格的上方或下方插入行。 字符串值(若指定)是在新插入的行中进行设置。 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| set(properties, options) | 同时设置对象的多个属性。 可以传递具有相应属性的纯对象,也可以传递同一类型的另一个 API 对象。 |
| set(properties) | 基于现有的已加载对象,同时对对象设置多个属性。 |
| set |
设置单元格填充(以磅为单位)。 |
| set |
设置单元格填充(以磅为单位)。 |
| split(row |
将单元格拆分为指定数量的行和列。 |
| toJSON() | 重写 JavaScript |
| track() | 根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果跨 |
| untrack() | 释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 在内存发布生效之前,需要调用 |
属性详细信息
body
cellIndex
columnWidth
context
horizontalAlignment
指定单元格的水平对齐方式。 该值可以是 left、centered、right 或 。justified
horizontalAlignment: Word.Alignment | "Mixed" | "Unknown" | "Left" | "Centered" | "Right" | "Justified";
属性值
Word.Alignment | "Mixed" | "Unknown" | "Left" | "Centered" | "Right" | "Justified"
注解
示例
// 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
parentTable
rowIndex
shadingColor
value
verticalAlignment
指定单元格的垂直对齐方式。 该值可以是 top、 center或 bottom。
verticalAlignment: Word.VerticalAlignment | "Mixed" | "Top" | "Center" | "Bottom";
属性值
Word.VerticalAlignment | "Mixed" | "Top" | "Center" | "Bottom"
注解
示例
// 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
方法详细信息
deleteColumn()
deleteRow()
getBorder(borderLocation)
获取指定边框的边框样式。
getBorder(borderLocation: Word.BorderLocation): Word.TableBorder;
参数
- borderLocation
- Word.BorderLocation
边框位置。
返回
注解
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"
边框位置。
返回
注解
示例
// 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
单元格填充位置必须为 top、left、bottom 或 。right
返回
OfficeExtension.ClientResult<number>
注解
getCellPadding(cellPaddingLocation)
获取单元格填充(以磅为单位)。
getCellPadding(cellPaddingLocation: "Top" | "Left" | "Bottom" | "Right"): OfficeExtension.ClientResult<number>;
参数
- cellPaddingLocation
-
"Top" | "Left" | "Bottom" | "Right"
单元格填充位置必须为 top、left、bottom 或 。right
返回
OfficeExtension.ClientResult<number>
注解
示例
// 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;
返回
注解
getNextOrNullObject()
获取下一个单元格。 如果此单元格是最后一个单元格,则此方法将返回一个 对象,其 isNullObject 属性设置为 true。 有关详细信息,请参阅 *OrNullObject 方法和属性。
getNextOrNullObject(): Word.TableCell;
返回
注解
insertColumns(insertLocation, columnCount, values)
使用单元格的列作为模板,在单元格的左侧或右侧添加列。 此方法适用于一致的 table。 字符串值(若指定)是在新插入的行中进行设置。
insertColumns(insertLocation: Word.InsertLocation.before | Word.InsertLocation.after | "Before" | "After", columnCount: number, values?: string[][]): void;
参数
- columnCount
-
number
要添加的列数。
- values
-
string[][]
可选的二维数组。 如果指定数组中的对应字符串,则填充单元格。
返回
void
注解
insertRows(insertLocation, rowCount, values)
使用单元格的行作为模板,在单元格的上方或下方插入行。 字符串值(若指定)是在新插入的行中进行设置。
insertRows(insertLocation: Word.InsertLocation.before | Word.InsertLocation.after | "Before" | "After", rowCount: number, values?: string[][]): Word.TableRowCollection;
参数
- rowCount
-
number
要添加的行数。
- values
-
string[][]
可选的二维数组。 如果指定数组中的对应字符串,则填充单元格。
返回
注解
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Word.Interfaces.TableCellLoadOptions): Word.TableCell;
参数
提供要加载对象的属性的选项。
返回
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)
setCellPadding(cellPaddingLocation, cellPadding)
设置单元格填充(以磅为单位)。
setCellPadding(cellPaddingLocation: Word.CellPaddingLocation, cellPadding: number): void;
参数
- cellPaddingLocation
- Word.CellPaddingLocation
单元格填充位置必须为 top、left、bottom 或 。right
- cellPadding
-
number
单元格填充。
返回
void
注解
setCellPadding(cellPaddingLocation, cellPadding)
设置单元格填充(以磅为单位)。
setCellPadding(cellPaddingLocation: "Top" | "Left" | "Bottom" | "Right", cellPadding: number): void;
参数
- cellPaddingLocation
-
"Top" | "Left" | "Bottom" | "Right"
单元格填充位置必须为 top、left、bottom 或 。right
- cellPadding
-
number
单元格填充。
返回
void
注解
split(rowCount, columnCount)
将单元格拆分为指定数量的行和列。
split(rowCount: number, columnCount: number): void;
参数
- rowCount
-
number
要拆分到的行数。 必须是基础行数的除数。
- columnCount
-
number
要拆分到的列数。
返回
void
注解
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;