rianjs / ical.net

ical.NET - an open source iCal library for .NET
MIT License
782 stars 230 forks source link

Generating Recurring Events ( Inserting them to a table) #461

Open ChrisAmstutz opened 5 years ago

ChrisAmstutz commented 5 years ago

Hello, I realize this is a very amateur-ish question but I'm attempting to implement Ical into a side project im making, (im not a programmer I'm just playing around on the side) I store all my needed information on a table, I'd like to generate all instances within a month and have them inputted into a table (this is how my makeshift calendar reads and can display the events). Additionally if it wasn't complicated enough I'm using VB.Net..

My question is how do I get from ical.getoccurance to a table of the events being reoccurred

Thanks a lot for your help and im sorry if this is confusing I'm still very much learning all of this

This is my Code: 'Here I am fillling my table
Dim StartDate As DateTime = New Date(currentDate.Year, currentDate.Month, 1) Dim EndDate As DateTime = StartDate.AddMonths(1).AddDays(-1)

    myAdapter.Fill(table)

    Dim iCal As New Calendar()

    For Each dr As DataRow In table.Rows

        'Defining the recurrance rule.. This is just a test
        Dim RRULE As New RecurrencePattern(FrequencyType.Weekly, 2)
        RRULE.Until = DateTime.Parse(EndDate)

        'Defining an Event
        Dim evt As CalendarEvent = iCal.Create(Of CalendarEvent)()
        evt.RecurrenceRules = New List(Of RecurrencePattern)({RRULE})

        'Getting start date, End date and Description from table
        evt.Start = New CalDateTime(DirectCast(dr("StartDateUTC"), Date))
        evt.End = evt.Start.AddHours(1)
        evt.Description = DirectCast(dr("Maint_Name"), String)

        'Defining search Range

         Next dr

         Dim OccurDurMonth As HashSet(Of Occurrence) = iCal.GetOccurrences(searchStart, 
        searchEnd)

    Dim serializer As New CalendarSerializer()
    Dim output As String = serializer.SerializeToString(iCal)

    MsgBox(output)

Thanks again

ChrisAmstutz commented 5 years ago

my output is only showing the original events, not the generated occurances Untitled

vanpyrzericj commented 4 years ago

@ChrisAmstutz Serializing the Calendar to a string is doing exactly what is intended. The serialization is including your RRULE value. It's the iCal consumer's responsibility to "calculate" and spit out the occurrences one-by-one. Serializing the calendar to the iCal spec isn't going to add every single instance of the event - that would defeat the point of specifying it in an RRULE (thus saving space!).

What you're probably looking for is to either: a) Call iCal.GetOccurrences() which will return all occurrences of all events added to the calendar. b) call evt.GetOccurences() which will return all the occurrences of just that specific event in your loop.

You can then take the resulting list of whichever one of those calls you decide to implement, and store the data where you wish.

Look at Issue #456 , which describes the differences with a lovely example.