WPS文字 JSA 学习笔记 - 批量设置表格

本文档详细介绍了如何使用JSA在WPS文字中创建和批量设置表格样式,包括设置表格边框、颜色、间距以及表头样式,并提供返回当前表格索引的方法。通过示例代码,展示了如何关闭屏幕更新以提高效率,以及如何进行自动调整表格等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPS文字 JSA 学习笔记 - 批量设置表格

创建表格样式

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.TopPadding = PixelsToPoints(0, true);						// 设置上边距为0
//        table.BottomPadding = PixelsToPoints(0, true);					// 设置下边距为0
//        table.LeftPadding = PixelsToPoints(0, true);					// 设置左边距为0
//        table.RightPadding = PixelsToPoints(0, true);					// 设置右边距为0
//        table.Spacing = PixelsToPoints(0, true);						// 允许单元格间距为0
		// ------------ 设置表格背景色 ------------
		table.Shading.ForegroundPatternColor = wdColorAutomatic;		//表格底纹前景色自动
		table.Shading.BackgroundPatternColor = wdColorAutomatic;		//表格底纹背景色自动
		
		// --------------- 设置表格线 ---------------
		//https://qn.cache.wpscdn.cn/encs/doc/office_v19/topics/WPS 基础接口/文字 API 参考/枚举/WdBorderType 枚举.html
		[wdBorderTop,wdBorderLeft,wdBorderBottom,wdBorderRight,wdBorderHorizontal,wdBorderVertical]
			.map(v=> table.Borders.Item(v)) 							// 将 WdBorderType 枚举转换成 Border 对象
			.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; 		// 表格背景色 10% 灰色底纹
		})(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设置表格样式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑虾

多情黯叹痴情癫。情癫苦笑多情难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值