- 本例程主要针对 docx 文件中的一个表格进行应用表格样式,不包含应用 word 所有的表格代码,需要更改主函数的
var currentChoosetable = selection.Tables.Item(1);
,修改为遍历所有表格即可。
- 如果表格存在合并或拆分,导致不是正常的井字形表格,代码可能未适配。初步测试无法使用。
一、主函数
function 调整表格样式() {
try {
var selection = Selection;
if (!selection.Tables.Count) {
alert("请先选择一个表格!");
return;
}
if (1 != selection.Tables.Count) {
alert("选择的表格数量不为1,自动选择第一个表格");
}
var currentChoosetable = selection.Tables.Item(1);
tableAutoFitWindow(currentChoosetable);
TableStyleAdjustment(currentChoosetable);
TableFontAdjustment(currentChoosetable);
} catch (err) {
alert("发生错误: " + err.message);
}
}
二、整体表格样式调整
function tableAutoFitWindow(table) {
if (!table) {
alert("请传入一个有效的表格对象!");
return;
}
try {
table.AutoFitBehavior(wdAutoFitWindow);
table.PreferredWidth = 100;
table.Rows.Alignment = wdAlignRowCenter;
} catch (error) {
console.error("设置表格样式时出错:", error.message);
}
for (var i = 1; i <= table.Rows.Count; i++) {
var row = table.Rows.Item(i);
Selection.SetRange(row.Range.Start, row.Range.End);
Selection.Cells.SetWidth(100, wdAdjustFirstColumn);
}
}
三、表格样式调整
function TableStyleAdjustment(table) {
if (!table) {
alert("请传入一个有效的表格对象!");
return;
}
table.Borders.Item(wdBorderDiagonalDown).LineStyle = wdLineStyleNone;
table.Borders.Item(wdBorderDiagonalUp).LineStyle = wdLineStyleNone;
table.Borders.Item(wdBorderTop).LineStyle = wdLineStyleSingle;
table.Borders.Item(wdBorderTop).LineWidth = wdLineWidth150pt;
table.Borders.Item(wdBorderTop).Color = wdColorWhite;
table.Borders.Item(wdBorderBottom).LineStyle = wdLineStyleSingle;
table.Borders.Item(wdBorderBottom).LineWidth = wdLineWidth150pt;
table.Borders.Item(wdBorderBottom).Color = wdColorWhite;
table.Borders.Item(wdBorderLeft).LineStyle = wdLineStyleSingle;
table.Borders.Item(wdBorderLeft).LineWidth = wdLineWidth150pt;
table.Borders.Item(wdBorderLeft).Color = wdColorWhite;
table.Borders.Item(wdBorderRight).LineStyle = wdLineStyleSingle;
table.Borders.Item(wdBorderRight).LineWidth = wdLineWidth150pt;
table.Borders.Item(wdBorderRight).Color = wdColorWhite;
table.Borders.Item(wdBorderHorizontal).LineStyle = wdLineStyleSingle;
table.Borders.Item(wdBorderHorizontal).LineWidth = wdLineWidth150pt;
table.Borders.Item(wdBorderHorizontal).Color = wdColorWhite;
table.Borders.Item(wdBorderVertical).LineStyle = wdLineStyleSingle;
table.Borders.Item(wdBorderVertical).LineWidth = wdLineWidth150pt;
table.Borders.Item(wdBorderVertical).Color = wdColorWhite;
for (var i = 1; i <= table.Rows.Count; i++) {
var row = table.Rows.Item(i);
for (var j = 1; j <= row.Cells.Count; j++) {
var cell = row.Cells.Item(j);
if (i == 1 || j == 1) {
cell.Shading.BackgroundPatternColor = 12419407;
} else if (i % 2 == 0) {
cell.Shading.BackgroundPatternColor = 14994616;
} else if (i % 2 == 1) {
cell.Shading.BackgroundPatternColor = 16182757;
}
Selection.SetRange(cell.Range.Start, cell.Range.End);
if (i == 1 || j == 1) {
Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter;
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter;
} else {
Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter;
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify;
}
}
}
Selection.SetRange(table.Range.Start, table.Range.End);
Selection.Cells.Height = 17;
}
四、表格字体调整
function TableFontAdjustment(table) {
if (!table) {
alert("请传入一个有效的表格对象!");
return;
}
Selection.SetRange(table.Range.Start, table.Range.End);
Selection.Font.Name = "宋体 (正文)";
Selection.Font.Size = 10;
Selection.Font.SizeBi = 10;
Selection.Font.NameAscii = "Calibri (正文)";
Selection.Font.NameOther = "Calibri (正文)";
Selection.Font.Color = wdColorBlack;
Selection.Font.BoldBi = 0;
Selection.Font.Underline = wdUnderlineNone;
for (var i = 1; i <= table.Rows.Count; i++) {
var cell = table.Rows.Item(i).Cells.Item(1);
Selection.SetRange(cell.Range.Start, cell.Range.End);
Selection.Font.Name = "宋体 (正文)";
Selection.Font.Size = 10;
Selection.Font.SizeBi = 10;
Selection.Font.Color = wdColorWhite;
Selection.Font.Bold = -1;
Selection.Font.BoldBi = -1;
Selection.Font.Underline = wdUnderlineNone;
}
var row = table.Rows.Item(1);
Selection.SetRange(row.Range.Start, row.Range.End);
Selection.Font.Name = "宋体 (正文)";
Selection.Font.NameAscii = "Calibri (正文)";
Selection.Font.NameOther = "Calibri (正文)";
Selection.Font.Size = 10;
Selection.Font.SizeBi = 10;
Selection.Font.Color = wdColorWhite;
Selection.Font.Bold = -1;
Selection.Font.BoldBi = -1;
Selection.Font.Underline = wdUnderlineNone;
}
五、实现效果
效果

实现

六、参考
- WPS宏编辑器批量设置word中表格样式
- WPS WebOffice开放平台|开发文档
- WPS Office JS宏实现批量处理Word中的表格样式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结