创建表格样式
function 创建表格样式(styleName = "表格")
{
try{
ActiveDocument.Styles.Item(styleName).Delete();
}catch(e){
Console.log("样式不存在");
}
ActiveDocument.Styles.Add(styleName, wdStyleTypeParagraph);
(obj=>{
obj.SpaceBefore = 0.5;
obj.SpaceAfter = 0.5;
obj.IndentCharWidth(0);
obj.IndentFirstLineCharWidth(0);
obj.BaseLineAlignment = wdBaselineAlignBaseline;
obj.Alignment = wdAlignParagraphCenter;
})(ActiveDocument.Styles.Item(styleName).ParagraphFormat);
}
创建表格样式();
批量设置表格
function 批量设置表格(begin, end){
Application.ScreenUpdating = false;
[...ActiveDocument.Tables]
.filter((v,i)=>i>=begin&&i<=end)
.forEach(table=>{
table.Style = "网格型";
table.Range.Style = "表格";
table.Shading.ForegroundPatternColor = wdColorAutomatic;
table.Shading.BackgroundPatternColor = wdColorAutomatic;
[wdBorderTop,wdBorderLeft,wdBorderBottom,wdBorderRight,wdBorderHorizontal,wdBorderVertical]
.map(v=> table.Borders.Item(v))
.forEach(border=>{
border.LineStyle = wdLineStyleSingle;
border.LineWidth = wdLineWidth050pt;
border.Color = wdColorBlack;
});
(row=>{
row.Range.Font.Bold = true;
row.Range.Font.BoldBi = true;
row.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter;
row.Range.ParagraphFormat.LineUnitBefore = 1;
row.Range.ParagraphFormat.LineUnitAfter = 1;
row.Cells.Shading.BackgroundPatternColor = wdColorGray10;
})(table.Rows.First);
table.Rows.HeadingFormat = true;
table.Columns.PreferredWidthType = wdPreferredWidthAuto;
table.AutoFitBehavior(wdAutoFitWindow);
});
Application.ScreenUpdating = true;
Console.log('批量设置表格完成。');
}
批量设置表格(8, 31);
返回当前表格索引
function 返回当前表格索引(){
var arr = [...ActiveDocument.Tables];
var len = arr.length;
var index = arr.findIndex(table=> Selection.Range.InRange(table.Range));
Console.log(`当前表格索引【${index}】,最大索引【${len-1}】`);
return [index,len];
}
参考资料
WPS 客户端开发文档
word vba设置表格样式