var dur_month = 1const startDate = new Date()startDate.setMonth(startDate.getMonth() - dur_month)console.log(`日历的开始时间 ${startDate.toLocaleDateString()}`)const endDate = new Date()endDate.setMonth(endDate.getMonth() + dur_month)console.log(`日历的结束时间 ${endDate.toLocaleDateString()}`)const reminders = await Reminder.allDueBetween(startDate, endDate)console.log(`获取 ${reminders.length} 条提醒事项`)var calendar = await Calendar.forEvents()//获取日历名和对应的日历var m_dict = {}for(cal of calendar){m_dict[cal.title] = cal//console.log(`日历:${cal.title}`)}const events = await CalendarEvent.between(startDate, endDate, calendar)console.log(`获取 ${events.length} 条日历`)for (const reminder of reminders) {//reminder的标识符const targetNote = `[Reminder] ${reminder.identifier}`const [targetEvent] = events.filter(e => e.notes != null && (e.notes.indexOf(targetNote) != -1))//过滤重复的reminderif(!m_dict[reminder.calendar.title]){console.warn(\"找不到日历\"+ reminder.calendar.title)continue}if (targetEvent) {console.log(`找到已经创建的事项 ${reminder.title}`)updateEvent(targetEvent, reminder)} else {const newEvent = new CalendarEvent()createEvent(newEvent, reminder,targetNote)}}Script.complete()function createEvent(event, reminder,targetNote){console.warn(`创建事项 ${reminder.title} 到 ${reminder.calendar.title}`)event.notes = targetNote + \"\\n\" + reminder.notes//要加入备注cal_name = reminder.calendar.titlecal = m_dict[cal_name]event.calendar = calif(reminder.isCompleted){event.title = `✅${reminder.title}`}else{event.title = `⭕️${reminder.title}`}event.startDate = reminder.dueDatevar ending = new Date(reminder.dueDate)ending.setHours(ending.getHours()+1)event.endDate = endingevent.save()}function updateEvent(event, reminder) {cal_name = reminder.calendar.titlecal = m_dict[cal_name]event.calendar = cal//console.warn(event.calendar.title)//已完成事项if(reminder.isCompleted){event.title = `✅${reminder.title}`var period = (reminder.dueDate-reminder.completionDate)/1000/3600/24period = period.toFixed(1)if(period < 0){period = -periodevent.location = \" 延期\" + period + \"天完成\"}else if (period == 0){event.location = \" 准时完成\"}else{event.location = \" 提前\" + period + \"天完成\"}}//未完成事项else{const nowtime = new Date()var period = (reminder.dueDate-nowtime)/1000/3600/24period = period.toFixed(1)event.title = `⭕️${reminder.title}`//console.log(reminder.title+(period))if(period < 0){event.location = \" 延期\" + (-period) + \"天\"}else{event.location = \" 还剩\" + period + \"天\"}}event.save()}
ios提醒事项同步日历scriptable代码
未经允许不得转载:爱站程序员基地 » ios提醒事项同步日历scriptable代码