问:怎样用WPS表格实现抽签多人中签功能?
程序猿答疑:
以前在Office Excel中用VBA编写宏,今天发现WPS表格支持JS宏,对程序猿答疑个人而言,JavaScript写起来比VBA流畅多了,今天给大家答疑的是如何用WPS表格实现按钮点击抽签功能,并且支持抽中多人。
新建支持宏的文件格式.xlsm,文件名为“抽签.xlsm”。将所有待抽签的人员姓名填在A列,从A3单元格开始向下填写,继“张三”、“李四”、“王二麻子”之后实在编不下去了,只能随机生成人名了……所以如果你发现表格中有你的名字,那绝对是巧合,因为程序猿不认识你(如果想认识下请扫文末的程序猿二维码)。
点“开发工具”,会显示宏编辑功能,点“设计模式”,绘制两个按钮“抽签”、“清空结果”。C3单元格中填写需中签的人数,样例中为需要抽中人为3人。双击“抽签”按钮,会跳转到宏编写界面,自动生成按钮点击事件CommandButton1_Click,在事件函数中编写JS代码。
退出设计模式,如果WPS未启用宏则会提示,点击“启用宏”。
点击”抽签“按钮,B列会显示3名随机中签人员。再次点击后又会重新随机生成3名中签人员。
编写“清空结果”按钮的点击事件,清空中签人员。
这是程序猿答疑的第一篇公众号文章,给大家送上福利,附上全部代码。
/**
* CommandButton1_Click Macro
*/
function CommandButton1_Click()
{
console.log('抽签按钮点击事件')
var cells = Application.Sheets("Sheet1").Cells;
let active_sheet = ThisWorkbook.ActiveSheet
// 获取应中签人数
var all_luckey_person_count = active_sheet.Range("C3").Value2
Debug.Print('应中签人数:')
Debug.Print(all_luckey_person_count)
// 所有中签人员
var all_lucky_persons = [];
var all_person_count = 0
//Debug.Print(all_person_count)
//console.log('all_person_count')
// 获取所有抽签人员
for(let i=1; i<100; i++)
{
var name = ""
//Debug.Print(name)
var row_num = all_person_count + 3
Debug.Print('row_num:')
Debug.Print(row_num)
var row_num_str = row_num.toString()
var cell_position = 'A' + row_num_str
Debug.Print(cell_position)
name = active_sheet.Range(cell_position).Value2
Debug.Print('name:')
Debug.Print(name)
if(name==null||name==""||name==undefined)
{
Debug.Print('姓名为空,不再向下搜索行')
break
}
all_person_count = all_person_count + 1
}
Debug.Print("所有参与抽签人数:")
Debug.Print(all_person_count)
var remain_luckey_person_count = all_luckey_person_count
Debug.Print("剩余中签人数:")
Debug.Print(remain_luckey_person_count)
if(remain_luckey_person_count > 0)
{
var all_lukcy_person_chosen = false
while(all_lukcy_person_chosen == false)
{
// 随机生成行数
// var random_num = Math.floor(Math.random() * (3 - (all_person_count+2))) + all_person_count+2 // 3~person_count+2
var random_num = Math.floor(Math.random() * (all_person_count+2 - 3 + 1)) + 3 // 3~person_count+2
Debug.Print("random_num:")
Debug.Print(random_num)
//active_sheet.Range('B3').Value2 = 'aa'
//active_sheet.Range('B3').Formula = '=INDEX(A3:A19,RANDBETWEEN(1,17))';
var lucky_name = active_sheet.Range("A"+random_num).Value2;
//active_sheet.Range('B3').Value2 = active_sheet.Range("A"+random_num).Value2;
Debug.Print("中签姓名:")
Debug.Print(lucky_name)
// 判断刚抽中人员是否已经在已抽中人员数组中
if(all_lucky_persons.indexOf(lucky_name) < 0)
{
//刚抽中人员是否不在已抽中人员数组中,添加到数组中
all_lucky_persons.push(lucky_name)
}
var chosen_all_lucky_person_count = all_lucky_persons.length
// 已选择的中签人员等于待中签人数
if(chosen_all_lucky_person_count == all_luckey_person_count)
{
Debug.Print("已选择的中签人员等于待中签人数,停止抽签")
all_lukcy_person_chosen = true
}
}
}
//所有中签人员随机排序,本段代码可不用,中签人员已经是随机抽取的了
all_lucky_persons.sort(function () {
return Math.random() - 0.5
})
// 在表格“中签”一列中列出所有中签人员
Debug.Print("所有中签人员:")
for(let i=0; i<all_luckey_person_count; i++)
{
var lucky_name = all_lucky_persons[i]
Debug.Print("姓名:")
Debug.Print(lucky_name)
var all_luckey_person_row_num = i + 3
Debug.Print("填写姓名的行数:")
Debug.Print(all_luckey_person_row_num)
active_sheet.Range('B'+all_luckey_person_row_num).Value2 = lucky_name;
}
}
/**
* CommandButton2_Click Macro
*/
function CommandButton2_Click()
{
Debug.Print('清空按钮点击事件')
let active_sheet = ThisWorkbook.ActiveSheet
// 获取应中签人数
var all_luckey_person_count = active_sheet.Range("C3").Value2
Debug.Print('应中签人数:')
Debug.Print(all_luckey_person_count)
for(let i=0; i<all_luckey_person_count; i++)
{
var all_luckey_person_row_num = i + 3
Debug.Print("要清空的行:")
Debug.Print(all_luckey_person_row_num)
active_sheet.Range('B'+all_luckey_person_row_num).Value2 = ''
}
// 再多清几行吧,代码有些冗余,因为之前发现如果抽签4个人,再设置抽签3个人后,按中签人数就会只清空3行人员姓名,最后一行没清空
for(let i=0; i<100; i++)
{
var all_luckey_person_row_num = i + 3
Debug.Print("要清空的行:")
Debug.Print(all_luckey_person_row_num)
active_sheet.Range('B'+all_luckey_person_row_num).Value2 = ''
}
}
大家可以关注微信公众号“程序员答疑”索要源代码文件~