Requires AutoHotkey v2.0.21+
SingleInstance Force
; 主GUI创建
MainGUI := Gui("-MinimizeBox +LastFound", "批量生成工具")
MainGUI.SetFont("s10", "微软雅黑")
; 1. 时间输入框(默认当前时间,格式:YYYY-MM-DD HH:mm:ss)
MainGUI.Add("Text", "x20 y20 w80 h25", "时间:")
CurrentTime := FormatTime(, "yyyy-MM-dd HH:mm:ss") ; 获取当前系统时间并格式化
timeInput := MainGUI.Add("Edit", "x100 y20 w200 h25", CurrentTime)
timeInput.ToolTip := "请输入有效的时间,格式:YYYY-MM-DD HH:mm:ss"
; 2. 个数输入框
MainGUI.Add("Text", "x20 y60 w80 h25", "个数:")
countInput := MainGUI.Add("Edit", "x100 y60 w200 h25", "")
countInput.ToolTip := "请输入正整数"
; 3. 生成按钮
generateBtn := MainGUI.Add("Button", "x100 y100 w100 h30", "生成并导出")
generateBtn.OnEvent("Click", GenerateAndExport)
; 调整GUI大小并居中显示
MainGUI.Show("w350 h150 Center")
return
; 生成并导出核心函数(仅递增时分秒,无ByRef,无语法错误)
GenerateAndExport(*) {
; 获取输入框内容
inputTime := timeInput.Value
inputCount := countInput.Value
; 输入验证
if !IsValidTime(inputTime) {
MsgBox("时间格式错误!请按照 YYYY-MM-DD HH:mm:ss 格式输入", "错误", "0x10")
timeInput.Focus()
return
}
if !IsInteger(inputCount) || inputCount < 1 {
MsgBox("个数必须是正整数!", "错误", "0x10")
countInput.Focus()
return
}
; 拆分起始时间:分离日期和时分秒(日期固定不变)
timeParts := StrSplit(inputTime, " ")
datePart := timeParts[1] ; 提取日期部分(YYYY-MM-DD)
hmsPart := timeParts[2] ; 提取时分秒部分(HH:mm:ss)
; 拆分时分秒为数值
hmsSplit := StrSplit(hmsPart, ":")
hour := hmsSplit[1] + 0
min := hmsSplit[2] + 0
sec := hmsSplit[3] + 0
; 构造CSV格式内容(首行是表头)
outputContent := "时间,序号`n"
Loop (inputCount) {
; 格式化当前时分秒(补零确保格式统一)
currentHms := Format("{:02d}:{:02d}:{:02d}", hour, min, sec)
; 拼接日期+当前时分秒,写入CSV
currentTimeStr := datePart . " " . currentHms
outputContent .= currentTimeStr . "," . A_Index . "`n"
; 最后一行不需要递增
if (A_Index < inputCount) {
; 生成X到Y之间的随机秒数
randomSeconds := Round(Random(62, 135))
; 调用函数获取递增后的时分秒(返回数组,无ByRef)
newHMS := AddSecondsToHMS(randomSeconds, hour, min, sec)
hour := newHMS[1]
min := newHMS[2]
sec := newHMS[3]
}
}
; 导出到脚本所在文件夹(CSV格式,UTF-8带BOM适配Excel)
scriptPath := A_ScriptDir
fileName := "生成结果_" . FormatTime(, "yyyyMMddHHmmss") . ".csv"
fullPath := scriptPath . "\" . fileName
try {
FileAppend(outputContent, fullPath, "UTF-8-RAW")
MsgBox("CSV文件生成成功!`n文件路径:" . fullPath, "成功", "0x40")
} catch as err {
MsgBox("生成失败:" . err.Message, "错误", "0x10")
}
}
; 时间格式验证函数(仅验证格式)
IsValidTime(timeStr) {
; 正则匹配 YYYY-MM-DD HH:mm:ss 格式
regexPattern := "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$"
if !RegExMatch(timeStr, regexPattern) {
return false
}
return true
}
; 辅助函数:累加秒数并返回新的时分秒(返回数组,彻底移除ByRef)
AddSecondsToHMS(secondsToAdd, hour, min, sec) {
; 累加秒数
sec += secondsToAdd
; 处理秒→分进位
if (sec >= 60) {
min += Floor(sec / 60)
sec := Mod(sec, 60)
}
; 处理分→时进位(仅到小时,不处理天)
if (min >= 60) {
hour += Floor(min / 60)
min := Mod(min, 60)
}
; 返回新的时分秒数组
return [hour, min, sec]
}