1、概述
利用 wps 的 JS 宏,设置单元格的边框,和单元格的颜色设置;借助 JS宏的录制功能。
2、办公环境
(1)、办公软件:WPS Office
(2)、版本号:11.1.0.14036-release
(3)、操作系统:Win 11
3、设置指定区域单元格边框
3.1、录制的设置单元格边框的 JS 宏代码
代码:
/**
* Macro1 Macro
* 指定区域单元的外边框,设置成紫色的宽实线;内部设置成红色的虚线
*/
function Macro1()
{
(obj=>{
obj.Weight = xlThick;
obj.LineStyle = xlContinuous;
obj.Color = 10498160;
obj.TintAndShade = 0;
})(Selection.Borders.Item(xlEdgeLeft));
(obj=>{
obj.Weight = xlThick;
obj.LineStyle = xlContinuous;
obj.Color = 10498160;
obj.TintAndShade = 0;
})(Selection.Borders.Item(xlEdgeTop));
(obj=>{
obj.Weight = xlThick;
obj.LineStyle = xlContinuous;
obj.Color = 10498160;
obj.TintAndShade = 0;
})(Selection.Borders.Item(xlEdgeBottom));
(obj=>{
obj.Weight = xlThick;
obj.LineStyle = xlContinuous;
obj.Color = 10498160;
obj.TintAndShade = 0;
})(Selection.Borders.Item(xlEdgeRight));
(obj=>{
obj.Weight = xlThin;
obj.LineStyle = xlDash;
obj.Color = 255;
obj.TintAndShade = 0;
})(Selection.Borders.Item(xlInsideVertical));
(obj=>{
obj.Weight = xlThin;
obj.LineStyle = xlDash;
obj.Color = 255;
obj.TintAndShade = 0;
})(Selection.Borders.Item(xlInsideHorizontal));
}
效果:
3.2、不用箭头函数设置单元格边框(细实线)
代码:
// 选择指定区域设置单元格边框
Range("E13:F14").Select();
// 设置选中区域的左边框
Selection.Borders.Item(xlEdgeLeft).ColorIndex = xlColorIndexAutomatic;
// 设置选中区域的上边框
Selection.Borders.Item(xlEdgeTop).ColorIndex = xlColorIndexAutomatic;
// 设置选中区域的下边框
Selection.Borders.Item(xlEdgeBottom).ColorIndex = xlColorIndexAutomatic;
// 设置选中区域的右边框
Selection.Borders.Item(xlEdgeRight).ColorIndex = xlColorIndexAutomatic;
// 设置选中区域的内部垂直竖线
Selection.Borders.Item(xlInsideVertical).ColorIndex = xlColorIndexAutomatic;
// 设置选中区域的内部水平横线
Selection.Borders.Item(xlInsideHorizontal).ColorIndex = xlColorIndexAutomatic;
效果:
3.2、不用箭头函数设置单元格边框(自定义边框样式)
代码:
// 选择指定单元格区域
Range("E13:F14").Select();
// JS 宏录制代码---设置单元格边框
// (obj=>{
// obj.Weight = xlThick;
// obj.LineStyle = xlContinuous;
// obj.Color = 10498160;
// obj.TintAndShade = 0;
// })(Selection.Borders.Item(xlEdgeLeft));
// 设置左边框
Selection.Borders.Item(xlEdgeLeft).Weight = xlThick;
Selection.Borders.Item(xlEdgeLeft).LineStyle = xlContinuous;
Selection.Borders.Item(xlEdgeLeft).Color = 10498160;
Selection.Borders.Item(xlEdgeLeft).TintAndShade = 0;
// JS 宏录制代码---设置单元格边框
// (obj=>{
// obj.Weight = xlThick;
// obj.LineStyle = xlContinuous;
// obj.Color = 10498160;
// obj.TintAndShade = 0;
// })(Selection.Borders.Item(xlEdgeTop));
// 设置上边框
Selection.Borders.Item(xlEdgeTop).Weight = xlThick;
Selection.Borders.Item(xlEdgeTop).LineStyle = xlContinuous;
Selection.Borders.Item(xlEdgeTop).Color = 10498160;
Selection.Borders.Item(xlEdgeTop).TintAndShade = 0;
// JS 宏录制代码---设置单元格边框
// (obj=>{
// obj.Weight = xlThick;
// obj.LineStyle = xlContinuous;
// obj.Color = 10498160;
// obj.TintAndShade = 0;
// })(Selection.Borders.Item(xlEdgeBottom));
// 设置下边框
Selection.Borders.Item(xlEdgeBottom).Weight = xlThick;
Selection.Borders.Item(xlEdgeBottom).LineStyle = xlContinuous;
Selection.Borders.Item(xlEdgeBottom).Color = 10498160;
Selection.Borders.Item(xlEdgeBottom).TintAndShade = 0;
// JS 宏录制代码---设置单元格边框
// (obj=>{
// obj.Weight = xlThick;
// obj.LineStyle = xlContinuous;
// obj.Color = 10498160;
// obj.TintAndShade = 0;
// })(Selection.Borders.Item(xlEdgeRight));
// 设置右边框
Selection.Borders.Item(xlEdgeRight).Weight = xlThick;
Selection.Borders.Item(xlEdgeRight).LineStyle = xlContinuous;
Selection.Borders.Item(xlEdgeRight).Color = 10498160;
Selection.Borders.Item(xlEdgeRight).TintAndShade = 0;
// JS 宏录制代码---设置单元格边框
// (obj=>{
// obj.Weight = xlThin;
// obj.LineStyle = xlDash;
// obj.Color = 255;
// obj.TintAndShade = 0;
// })(Selection.Borders.Item(xlInsideVertical));
// 设置内部垂直竖线
Selection.Borders.Item(xlInsideVertical).Weight = xlThin;
Selection.Borders.Item(xlInsideVertical).LineStyle = xlDash;
Selection.Borders.Item(xlInsideVertical).Color = 255;
Selection.Borders.Item(xlInsideVertical).TintAndShade = 0;
// JS 宏录制代码---设置单元格边框
// (obj=>{
// obj.Weight = xlThin;
// obj.LineStyle = xlDash;
// obj.Color = 255;
// obj.TintAndShade = 0;
// })(Selection.Borders.Item(xlInsideHorizontal));
// 设置内部水平横线
Selection.Borders.Item(xlInsideHorizontal).Weight = xlThin;
Selection.Borders.Item(xlInsideHorizontal).LineStyle = xlDash;
Selection.Borders.Item(xlInsideHorizontal).Color = 255;
Selection.Borders.Item(xlInsideHorizontal).TintAndShade = 0;
效果:
4、设置指定区域单元格颜色
4.1、录制的设置单元格颜色的 JS 宏代码
代码:
/**
* Macro1 Macro
* 设置选定单元格的颜色
*/
function Macro1()
{
(obj=>{
obj.PatternColorIndex = -4105;
obj.PatternTintAndShade = 0;
obj.ThemeColor = 10;
obj.TintAndShade = 0.600000;
obj.Pattern = xlPatternSolid;
})(Selection.Interior);
}
效果:
4.2、不用箭头函数设置单元格颜色(简单实现)
代码:
// 选择指定的单元格区域
Range("B8:D10").Select();
// 设置单元格的颜色
Selection.Interior.Color = 12050152;
效果:
4.2、不用箭头函数设置单元格颜色(自定义单元格颜色)
代码:
// JS 宏录制代码---设置单元格的颜色
// (obj=>{
// obj.PatternColorIndex = -4105;
// obj.PatternTintAndShade = 0;
// obj.ThemeColor = 10;
// obj.TintAndShade = 0.600000;
// obj.Pattern = xlPatternSolid;
// })(Selection.Interior);
// 设置单元格的颜色
Selection.Interior.PatternColorIndex = -4105;
Selection.Interior.PatternTintAndShade = 0;
Selection.Interior.ThemeColor = 10;
Selection.Interior.TintAndShade = 0.600000;
Selection.Interior.Pattern = xlPatternSolid;
效果: