当前位置: 首页 > 工具软件 > ExcelJS > 使用案例 >

WPS Excel JS宏简单使用

姚航
2023-12-01

1. 获取指定Sheet并激活

Application对象是整个Excel结构的根对象,可用于获取常用属性如ActiveCellActiveWorkbookActiveSheet

Application.Workbooks.Item(file_name).Worksheets.Item("Sheet1").Activate()

2. 获取Sheet上某一区域

myRange = Application.Range("A1:B5")

3.对选定区域进行设置,修改边框,行高,列宽,字体等操作

myRange.Value2 = "Hello World!"
// 设置行高,列宽
myRange.Rows.ColumnWidth = 12.28
myRange.Rows.RowHeight = 39
// 设置水平和垂直居中
myRange.Rows.HorizontalAlignment=xlHAlignCenter
myRange.Rows.VerticalAlignment=xlVAlignCenter
// 设置字体
myRange.Rows.Font.Size = 12
myRange.Rows.Font.Name = "宋体"
myRange.Font.Bold = true
// 取消边框
myRange.Borders.LineStyle = 0

4. 输入框和提示框

// 输入框
let total = Application.InputBox("请输入数量", "输入提示", undefined, undefined, undefined, undefined, undefined,1)

//提示框 样式参见JSMsgBoxStyle枚举
confirm_flag = MsgBox("请确认信息无误",jsOKCancel+jsDefaultButton1,"确认")

5. 打印设置

myPage = Application.ActiveSheet.PageSetup
//设置页边距
myPage.TopMargin = Application.CentimetersToPoints(1.50)
myPage.BottomMargin = Application.CentimetersToPoints(1.50)
myPage.LeftMargin = Application.CentimetersToPoints(1.50)
myPage.RightMargin = Application.CentimetersToPoints(1.50)
myPage.HeaderMargin = Application.CentimetersToPoints(1.00)
myPage.FooterMargin = Application.CentimetersToPoints(1.00)

//设置页眉和页脚
myPage.RightHeader = "我是页眉"
myPage.CenterFooter = "第 &P 页,共 &N 页"

6. 参考

https://open.wps.cn/docs/client/wpsLoad  wps开发文档

 类似资料: