欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios提醒事项同步日历scriptable代码

程序员文章站 2022-06-21 19:17:53
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(`日历的结束时间....

var dur_month = 1

const 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))//过滤重复的reminder
  if(!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.title
    cal = m_dict[cal_name]
    event.calendar = cal  
    if(reminder.isCompleted)
    {
      event.title = `✅${reminder.title}`
    }
    else
    {
       event.title = `⭕️${reminder.title}`
    }
    event.startDate = reminder.dueDate
    var ending = new Date(reminder.dueDate)
    ending.setHours(ending.getHours()+1)
    event.endDate = ending
    event.save()
}

function updateEvent(event, reminder) {
  cal_name = reminder.calendar.title
  cal = 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/24
    period = period.toFixed(1)
    if(period < 0)
    {
      period = -period
      event.location = " 延期" + period + "天完成" 
    }
    else if (period == 0)
    {
      event.location = " 准时完成"
    }
    else
    {
       event.location = " 提前" + period + "天完成"
    }
  }
  //未完成事项
  else{
      const nowtime  = new Date()
      var period = (reminder.dueDate-nowtime)/1000/3600/24
      period = period.toFixed(1)
      event.title = `⭕️${reminder.title}`
      //console.log(reminder.title+(period))
      if(period < 0)
      {
         event.location = " 延期" + (-period) + "天" 
      }
      else
      {
         event.location = " 还剩" + period + "天" 
      }
    }
  event.save()
}


本文地址:https://blog.csdn.net/zackerzhuang/article/details/107956043

相关标签: 笔记