js宏练手小工具——批量重命名
效果展示:
已把写好的宏加在快捷访问工具栏中(上一篇有教程),这里写了一个集合多个工具的窗体,这里直接选择批量重命名。
(1)点击‘选择文件’按钮选择一个或多个文件’
(2)根据需求选择对应的 重命名规则(这里有7种重命名规则,具体看图二)
(3)在输入框填写需要重命名添加的字符(规则为在中间插入字符还需要选择插入字符的位数)
(4)点击‘重命名’按钮,宏会按照选择的规则执行批量重命名
代母实现:
(1)根据需求拉取窗体和控件
(2)点击批量重命名按钮时,窗体初始化:
//批量重命名功能进入
function UserForm1_CommandButton6_Click()
{
UserForm1.Close()
UserForm4.StartUpPostion = 1
UserForm4.TextEdit1.Value = ""
UserForm4.TextEdit2.Value = ""
UserForm4.Label4.Caption = ""
UserForm4.SpinButton1.Enabled = false
UserForm4.ComboBox1.AddItem("首位添加字符")
UserForm4.ComboBox1.AddItem("末位添加字符")
UserForm4.ComboBox1.AddItem("X位末添加字符")
UserForm4.ComboBox1.AddItem("自定义")
UserForm4.ComboBox1.AddItem("末位插入日期")
UserForm4.ComboBox1.AddItem("全部大写")
UserForm4.ComboBox1.AddItem("全部小写")
UserForm4.Show()
}
(2)‘选择文件’按钮功能实现:
//重命名--选择需重命名的文件
function UserForm4_CommandButton1_Click()
{
let fd = Application.FileDialog(msoFileDialogFilePicker)
, arr = []
//允许文件多选
fd.AllowMultiSelect = true
fd.Filters.Clear()
fd.Filters.Add('Excel文件', '*.xls;*.xlsx;*.csv')
fd.Title = '请选择EXCEL文件'
//判断是否选择文件并反馈
if (fd.Show() != -1) {
UserForm4.TextEdit1.Value = ""
UserForm4.Label4.Caption = "你未选择文件,无法重命名"
}
else{
//选择的文件地址转为数组,在文本框显示
UserForm4.Label4.Caption = ""
for (let i = 0; i < fd.SelectedItems.Count; i++) {
arr[i] = fd.SelectedItems.Item(i + 1)
}
UserForm4.TextEdit1.Value = arr.join('\n')
}
}
(3)重命名规则预览实现:
//重命名--规则预览
function UserForm4_ComboBox1_Change()
{
if(UserForm4.ComboBox1.Value == "X位末添加字符"){
UserForm4.SpinButton1.Enabled = true
UserForm4.Label4.Caption = "xxxx --> xxAxx"
}
else if(UserForm4.ComboBox1.Value == "首位添加字符"){
UserForm4.SpinButton1.Enabled = false
UserForm4.Label4.Caption = "xxxx --> Axxxx"
}
else if(UserForm4.ComboBox1.Value == "末位添加字符"){
UserForm4.SpinButton1.Enabled = false
UserForm4.Label4.Caption = "xxxx --> xxxxA"
}
else if(UserForm4.ComboBox1.Value == "自定义"){
UserForm4.SpinButton1.Enabled = false
UserForm4.Label4.Caption = "xxxx --> AAA"
}
else if(UserForm4.ComboBox1.Value == "末位插入日期"){
UserForm4.SpinButton1.Enabled = false
UserForm4.Label4.Caption = "xxxx --> xxxx(20230222)"
}
else if(UserForm4.ComboBox1.Value == "请选择重命名规则"){
UserForm4.SpinButton1.Enabled = false
UserForm4.Label4.Caption = ""
}
else if(UserForm4.ComboBox1.Value == "全部大写"){
UserForm4.SpinButton1.Enabled = false
UserForm4.Label4.Caption = "AbCd --> ABCD"
}
else if(UserForm4.ComboBox1.Value == "全部小写"){
UserForm4.SpinButton1.Enabled = false
UserForm4.Label4.Caption = "AbCd --> abcd"
}
}
(4)‘重命名’按钮功能实现:
//重命名--重命名按钮
function UserForm4_CommandButton2_Click()
{
if(UserForm4.TextEdit1.Value == ""){
UserForm4.Label4.Caption = "你未选择文件,无法重命名"
}
else{
if(UserForm4.ComboBox1.Value == "请选择重命名规则"){
UserForm4.Label4.Caption = "你未选择规则,无法重命名"
}
else{
UserForm4.Label4.Caption = ""
let addname = UserForm4.TextEdit2.Value//文本框输入的文件名
,pth = UserForm4.TextEdit1.Value.split('\n')
, data = []
for (let i = 0; i < pth.length; i++) {
let wb = Workbooks.Open(pth[i])
, fname1 = wb.Name //原始文件名
, num1 = fname1.length //原始文件名的长度
, num2 = UserForm4.SpinButton1.Value//X位末添加字符——X
, f //保存的路径+文件名
let arr = fname1.split("") //原始文件名字符串转为数组
arr.splice(num1-5, 5) //删除数组最后的5个数据(删除的是.xlsx5个字符)
let fname2 = arr.join('') //处理好的数组转为字符串
arr.splice(num2, 0, addname) //在X位末加入数据
let fname3 = arr.join('') //在X位末加入数据 数组转为字符串
let fname4 = fname2.toLocaleUpperCase()//字母转为大写
let fname5 = fname2.toLocaleLowerCase()//字母转为小写
if(UserForm4.ComboBox1.Value == "首位添加字符"){
f = "C:/Users/oristand/Desktop" + '\\' + addname + fname2 + '.xlsx'
}
else if(UserForm4.ComboBox1.Value == "末位添加字符"){
f = "C:/Users/oristand/Desktop" + '\\' + fname2 + addname + '.xlsx'
}
else if(UserForm4.ComboBox1.Value == "自定义"){
f = "C:/Users/oristand/Desktop" + '\\' + addname + i + '.xlsx'
}
else if(UserForm4.ComboBox1.Value == "末位插入日期"){
f = "C:/Users/oristand/Desktop" + '\\' + fname2 + "(" + dateFormat('yyyymmdd', new Date) + ")" + '.xlsx'
}
else if(UserForm4.ComboBox1.Value == "X位末添加字符"){
f = "C:/Users/oristand/Desktop" + '\\' + fname3 + '.xlsx'
}
else if(UserForm4.ComboBox1.Value == "全部大写"){
f = "C:/Users/oristand/Desktop" + '\\' + fname4 + '.xlsx'
}
else if(UserForm4.ComboBox1.Value == "全部小写"){
f = "C:/Users/oristand/Desktop" + '\\' + fname5 + '.xlsx'
}
//按照自定义规则保存为xlsx文件,地址为桌面
wb.SaveAs(f)
wb.Close()
}
UserForm4.Label4.Caption = "重命名结束,文件保存在桌面!"
}
}
}
到此结束,有问题一起交流!