Action

Jaymfs Next Seven Days Events

Posted by FlohGro, Last update 8 months ago - Unlisted

UPDATES

8 months ago

Updates as requested in the forum

created by @FlohGro / more on my Blog

Creates a new draft for each event with attendees in the next seven days.
please adapt the template tag calendar-name with the correct name of the calendar you want to use


If you find this useful and want to support me you can donate or buy me a coffe

Buy Me A Coffee

Steps

  • defineTemplateTag

    name
    calendar-name
    template
    Privat
  • script

    // jaymf events for next seven days
    
    // type the name of the calendar between the ""
    const calendarName = draft.processTemplate("[[calendar-name]]")
    
    // ======================================================
    // no changes from here unless you know what you're doing
    // ======================================================
    // seven days in the future
    const eventDaysOffset = 7
    let calendar = Calendar.find(calendarName)
    
    
    if (calendar) {
        // found the calendar
        let startDate = new Date()
        let endDate = getOffsetDate()
        // now get the events between those dates    
        let events = calendar.events(startDate, endDate)
        if (events.length > 0) {
            // found some events iterate through them and delegate extracction of date into separate function
            let createdDrafts = 0
            for (ev of events) {
                // returns 1 if draft was created, 0 if no draft was created (e.g. no attendees)
                createdDrafts += extractEventDetailsIntoNewDraft(ev)
            }
            if (createdDrafts == 0) {
                app.displayInfoMessage("No drafts created")
            } else {
                app.displaySuccessMessage("created " + createdDrafts + " event drafts")
            }
    
    
        } else {
            app.displayErrorMessage("No events found in calendar \"" + calendarName + "\" in the next " + eventDaysOffset + " days.")
        }
    
    } else {
        app.displayErrorMessage("Calendar \"" + calendarName + "\" wasn't found")
    }
    
    function getOffsetDate() {
        let date = new Date()
        // add days offset
        date.setDate(date.getDate() + eventDaysOffset)
        // change time to end of day
        date.setHours(23, 59, 59, 999)
        return date
    }
    
    function extractEventDetailsIntoNewDraft(event) {
        /* required information:
            event title,
            attendee names,
            attendee email addresses
        */
        let title = event.title
        let startDate = formatDate(event.startDate)
        let endDate = formatDate(event.endDate)
        let attendees = event.attendees
        if (attendees.length > 0) {
            // event has attendees
            let parsedAttendees = parseAttendees(attendees)
            let parsedMailAddresses = parseMailAddresses(attendees)
            let content = "Confirming " + title + " " + startDate + "\nDear " + parsedAttendees.join(", ") + ", \nA quick note to confirm our meeting " + startDate + ".\nIf your plans have changed - please reply and let me know so we can reschedule.\nMany thanks and looking forward,\nJason\n\n" + parsedMailAddresses.join("\n");
            createNewDraftWithContent(content)
            return 1
        } else {
            // event doesn't have any attendees
            // we will ignore those for now!
            return 0
        }
    }
    
    function formatDate(date) {
        return date.toLocaleString()
    }
    
    const emailRegex = /[\w._%+-]+@[\w.-]+\.[a-zA-Z]{2,4}/g
    
    function parseAttendees(attendees) {
        let parsedAttendees = []
        for (att of attendees) {
            // check if it's not the current user
            if (!att.isCurrentUser) {
                let name = att.name
                // noch check for e-mail Address
                let mail = name.match(emailRegex)
                if(mail.length != 0){
                    for(e of mail){
                        name = name.replace(e,"")
                    }
                }
                let status = att.status
                parsedAttendees.push(name.trim() + " (" + status + ")")
            }
        }
        return parsedAttendees.join("\n")
    }
    
    function parseMailAddresses(attendees){
        let parsedMailAddresses = []
        for (att of attendees) {
            // check if it's not the current user
            if (!att.isCurrentUser) {
                let name = att.name
                // noch check for e-mail Address
                let mail = name.match(emailRegex)
                if(mail.length != 0){
                    parsedMailAddresses = parsedMailAddresses.concat(mail)
                }
            }
        }
        return parsedMailAddresses.join("\n")
    }
    
    function createNewDraftWithContent(content) {
        let d = new Draft()
        d.content = content
        d.update()
    }

Options

  • After Success Default
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.