wps之使用宏修改全篇表格样式
wps之使用宏修改全篇表格样式
宏使用步骤:
1.【工具】》【开发工具】

2.运行宏

3.创建宏

4.编辑宏

5.编辑后不用保存,直接关闭编辑页面
6.运行宏

注意事项:
保存文档前需要删除或者保存宏
JS宏代码:
1.修改表格和表头底色
/**
* 修改表格样式 Macro
*/
function FormatAllTableHeader() {
const doc = Application.ActiveDocument;
const tables = doc.Tables;
const total = tables.Count;
if (total === 0) {
alert("文档内没有表格");
return;
}
// 表头蓝色
const headerCellBg = RGB(31, 78, 121);
const headerFontColor = wdColorWhite;
const contentCellBg = wdColorWhite;
const contentFontColor = wdColorBlack;
let successNum = 0;
for (let t = 1; t <= total; t++) {
try {
const tbl = tables.Item(t);
const rowTotal = tbl.Rows.Count;
if (rowTotal < 1) continue;
// ========== 表头:第1行单元格 ==========
const headRow = tbl.Rows.Item(1);
const cellNumHead = headRow.Cells.Count;
for (let c = 1; c <= cellNumHead; c++) {
const cell = headRow.Cells.Item(c);
// 设置【单元格底纹】整格填充
cell.Shading.BackgroundPatternColor = headerCellBg;
// 设置文字颜色
cell.Range.Font.Color = headerFontColor;
}
// ========== 内容行:第2行至最后 ==========
for (let r = 2; r <= rowTotal; r++) {
const dataRow = tbl.Rows.Item(r);
const cellNum = dataRow.Cells.Count;
for (let c = 1; c <= cellNum; c++) {
const cell = dataRow.Cells.Item(c);
// 单元格底色白色
cell.Shading.BackgroundPatternColor = contentCellBg;
// 文字黑色
cell.Range.Font.Color = contentFontColor;
}
}
successNum++;
} catch (e) {
console.log(`第${t}个表格异常:` + e.message);
}
}
alert(`处理完成!\n总计表格:${total} 个\n成功处理:${successNum} 个`);
}
2.修改表格边框
function SetAllTableGrayBorder() {
const doc = Application.ActiveDocument;
const tables = doc.Tables;
const total = tables.Count;
if (total === 0) {
alert("文档内没有表格");
return;
}
// 灰色,可自行修改RGB数值
const borderColor = RGB(128, 128, 128);
let successNum = 0;
// 边框类型:上下左右 + 内部横线 + 内部竖线
const borderIds = [-1, -2, -3, -4, -5, -6];
for (let t = 1; t <= total; t++) {
try {
const tbl = tables.Item(t);
tbl.Borders.Enable = true;
for (let i = 0; i < borderIds.length; i++) {
const b = tbl.Borders.Item(borderIds[i]);
b.LineStyle = wdLineStyleSingle; // 实线
b.LineWidth = wdLineWidth050pt; // 0.5磅
b.Color = borderColor; // 灰色
}
successNum++;
} catch (e) {
console.log("第" + t + "个表格边框异常:" + e.message);
}
}
alert("边框处理完成!\n总计表格:" + total + " 个\n成功:" + successNum + " 个");
}
3.表格首列加粗
function BoldFirstColumn() {
const doc = Application.ActiveDocument;
const tables = doc.Tables;
const total = tables.Count;
if (total === 0) {
alert("文档内没有表格");
return;
}
let successNum = 0;
for (let t = 1; t <= total; t++) {
try {
const tbl = tables.Item(t);
const rowTotal = tbl.Rows.Count;
// 遍历每一行,将第一列单元格文字加粗
for (let r = 1; r <= rowTotal; r++) {
const row = tbl.Rows.Item(r);
if (row.Cells.Count >= 1) {
row.Cells.Item(1).Range.Font.Bold = true;
}
}
successNum++;
} catch (e) {
console.log("第" + t + "个表格异常:" + e.message);
}
}
alert("第一列加粗完成!\n总计表格:" + total + " 个\n成功:" + successNum + " 个");
}
4.表格自适应宽度
function SetAllTable_WidthPage() {
const doc = Application.ActiveDocument;
const tables = doc.Tables;
const tableCount = tables.Count;
if(tableCount === 0){
alert("文档未找到表格");
return;
}
let success = 0;
for(let t=1; t<=tableCount; t++){
try{
const tbl = tables.Item(t);
// 自动适配文档正文宽度
tbl.PreferredWidthType = wdPreferredWidthPercent;
tbl.PreferredWidth = 100; // 100% 正文宽度
success++;
}catch(e){
console.log(`第${t}个表格异常:`+e.message);
}
}
alert(`执行完成!\n总共${tableCount}个表格\n成功调整${success}个`);
}
宏代码可AI生成
钻研不易,转载请注明出处。。。。。。

浙公网安备 33010602011771号