金币
UID1628
帖子
主题
积分1175
注册时间2011-8-15
最后登录1970-1-1
听众
性别保密
|
欢迎您注册蒲公英
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 calvin 于 2018-8-7 23:11 编辑
情景:
我公司会有一个Team定期去车间巡视,然后正好赶上我做环境动态监测,他们不知道被吓了一跳,以为不能进。随后要求我以后动态监测要通知他们一下。
实现:
1。 通知不难,一个邮件而已。
2。 但是需要记着通知他们,这个比较难。
3。 因为我公司办公是围绕office的,平时邮件都是通过outlook交流。而我每次环境监测都会在calendar里book上时间。如下图1。
4。 然后使用R,通过COM组件,抽取当月calendar的schedule数据。如图2。
5。 然后对获取的数据进行判定,如果本月有动态监测,则使用R通过COM组件,控制outlook自动发送邮件给指定的人。如图3。
6。 如果本月没有动态监测,或者本月已通知过了。则不再发送邮件。
7。 通过windows的计划任务,让该程序定期自动运行。
全部代码附在后面,需要请拿走。
- library(RDCOMClient)
- library(stringr)
-
- email_send<-function(moninfo){
- receiver_list<-paste("yao_shu_liang@lilly.com",
- sep=";",collapse = NULL)
- content_body<-paste0("Hi all,<br> This email is produced automatically to tell you that ",moninfo$sublist[Notify_index]," would be performed on ", moninfo$start[Notify_index], ", please check this information.<br> If you have any question, please contact me by reply this mail.")
- outApp<-COMCreate("Outlook.Application")
- outMail<-outApp$CreateItem(0)
- outMail[["To"]]=receiver_list
- outMail[["subject"]]="Dynamic EM Notification"
- outMail[["HTMLbody"]]=content_body
- outMail$Send()
- }
-
- ot<-COMCreate("Outlook.Application")
- otf<-ot$GetNamespace("MAPI")
- calendar<-otf$GetDefaultFolder(9)
-
- sublist<-vector()
- location<-vector()
- startlist<-vector()
- endlist<-vector()
-
- strfilter<-paste0("[Start] >= '",stringr::str_c(stringr::str_sub(Sys.Date(),start=1,end=8),"01"),"' AND [END] <= '",stringr::str_c(stringr::str_sub(Sys.Date(),start=1,end=8),lubridate::days_in_month(Sys.Date()),"'"))
- filtcal<-calendar$Items()$Restrict(strfilter)
-
- for(i in seq_len(filtcal[["Count"]])){
- sublist[i]<-filtcal$Item(i)[["Subject"]]
- location[i]<-filtcal$Item(i)[["Location"]]
- startlist[i]<-filtcal$Item(i)[["Start"]]
- endlist[i]<-filtcal$Item(i)[["End"]]
- }
-
- moninfo<-data.frame(sublist,location,start=startlist,end=endlist,stringsAsFactors = FALSE)
- moninfo$start<-as.Date(moninfo$start,format="%Y-%m-%d",origin="1900-01-01")-2
- moninfo$end<-as.Date(moninfo$end,format="%Y-%m-%d",origin="1900-01-01")-2
- moninfo
-
- Notify_index<-str_detect(as.character(moninfo$sublist), pattern="Static")
- Current_mon<-str_c("Mon:",str_sub(Sys.Date(),start=1,end=7))
-
- if(!file.exists("~/EM_Notify_mark.csv")){
- email_mark<-data.frame(Month="Mon:Undefined",MailSent="N",stringsAsFactors = FALSE)
- write.csv(email_mark,file="~/EM_Notify_mark.csv",row.names=FALSE)
- }
-
- email_mark_temp<-read.csv("~/EM_Notify_mark.csv",stringsAsFactors = FALSE)
-
- if(file.exists("~/EM_Notify_mark.csv") & !any(str_detect(email_mark_temp$Month,Current_mon))){
- email_mark_temp<-read.csv("~/EM_Notify_mark.csv",stringsAsFactors = FALSE)
- email_send(moninfo)
- email_mark_temp<-rbind(email_mark_temp,data.frame(Month=Current_mon,MailSent="Y",stringsAsFactors = FALSE))
- write.csv(email_mark_temp,file="~/EM_Notify_mark.csv",row.names=FALSE)
- }
复制代码
其它应用更多惊喜等待你。
|
|