這是一個非常簡單的例子,將需要用大量的額外代碼擴展到處理天翻車和許多其他的東西,但只是給你一個概念
類屬:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Category"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private pweekday As String
Private ptime_start As Double
Private ptime_stop As Double
Private pcategory As String
Public Property Let weekday(day As String)
pweekday = day
End Property
Public Property Let time_start(time As Double)
If checkTime(time) Then
ptime_start = time
Else
MsgBox "Start Error"
End If
End Property
Public Property Let time_stop(time As Double)
If checkTime(time) Then
ptime_stop = time
Else
MsgBox "Stop Error"
End If
End Property
Public Property Let category(cat As String)
pcategory = cat
End Property
Public Sub setup(cat As String, day As String, t_start As Double, t_stop As Double)
If checkTime(t_start) And checkTime(t_stop) Then
pcategory = cat
pweekday = day
ptime_start = t_start
ptime_stop = t_stop
Else
MsgBox "SetupError"
End If
End Sub
Public Property Get category() As String
category = pcategory
End Property
Public Property Get weekday() As String
weekday = pweekday
End Property
Public Property Get time_start() As Double
time_start = ptime_start
End Property
Public Property Get time_stop() As Double
time_stop = ptime_stop
End Property
Private Function checkTime(time As Double) As Boolean
checkTime = time <= 24 And time >= 0
End Function
類時間表
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "TimeTable"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private ptime_table As New Collection
Public Sub add(cat As String, day As String, t_start As Double, t_stop As Double)
Dim addcat As New category
addcat.setup cat, day, t_start, t_stop
ptime_table.add addcat
End Sub
Public Function getStartCategory(day As String, t_start As Double, hours As Double) As String
Dim c As category
Dim c_start As String
Dim start_hours As Double
For Each c In ptime_table
If c.weekday = day Then
If t_start >= c.time_start And t_start <= c.time_stop Then
If hours > (c.time_stop - t_start) Then
start_hours = c.time_stop - t_start
Else
start_hours = hours
End If
c_start = CStr(start_hours) & ";" & c.category
End If
End If
Next c
getStartCategory = c_start
End Function
Public Function getStopCategory(day As String, t_stop As Double, hours As Double) As String
Dim c As category
Dim c_stop As String
Dim stop_hours As Double
For Each c In ptime_table
If c.weekday = day Then
If t_stop <= c.time_stop And t_stop >= c.time_start Then
If hours > (t_stop - c.time_start) Then
stop_hours = t_stop - c.time_start
Else
stop_hours = hours
End If
c_stop = CStr(stop_hours) & ";" & c.category
End If
End If
Next c
getStopCategory = c_stop
End Function
Public Function getCategory(day As String, t_start As Double, t_stop As Double) As String
Dim hours As Double
hours = hoursworked(t_start, t_stop)
c_start = getStartCategory(day, t_start, hours)
c_stop = getStopCategory(day, t_stop, hours)
getCategory = c_start & ";" & c_stop
End Function
Private Function hoursworked(t_start As Double, t_stop As Double) As Double
hoursworked = t_stop - t_start
End Function
簡單的例子模塊
Sub setup()
Dim tt As New TimeTable
With tt
.add "AA", "Monday", 8, 14.5
.add "AA", "Tuesday", 8, 14.5
.add "AA", "Wednesday", 8, 14.5
.add "AA", "Thursday", 8, 14.5
.add "AA", "Friday", 8, 14
.add "AB", "Monday", 14.5, 21
.add "AB", "Tuesday", 14.5, 21
.add "AB", "Wednesday", 14.5, 21
.add "AB", "Thursday", 14.5, 21
.add "AB", "Friday", 14, 21
.add "AC", "Monday", 21, 8
.add "AC", "Tuesday", 21, 8
.add "AC", "Wednesday", 21, 8
.add "AC", "Thursday", 21, 8
.add "AC", "Friday", 21, 8
.add "AC", "Saturday", 0, 24
.add "AC", "Sunday", 0, 24
End With
MsgBox tt.getCategory("Monday", 12, 18)
End Sub
這將返回「2.5; AA; 3.5; AB」含義星期一此人在AA類工作2.5小時,在AB類工作3.5小時。
就像我說的,這是一個如何啓動,需要更多的錯誤處理功能和數據映射一個簡單的例子,但希望這將讓你開始
這似乎是一個複雜的數據結構在Excel中進行處理你有沒有試過把它移到數據庫? – engineersmnky
我還沒試過。我希望能在Excel中進行原型設計。我建議我可以將它移動到sqllite db。 我認爲這裏面臨的主要挑戰是如果我無法做到這一點,那麼就有可能向其他人解釋它的工作原理。開發人員不可能瞭解我們想要的是什麼。 而這次演習將毫無意義。我會嘗試將它移動到數據庫。 除了結構之外,還有關於如何計算類別的建議? – Tommy
如果你想在Excel中這樣做,我會建議設置類對象和使用集合或字典結構 – engineersmnky