蒲公英 - 制药技术的传播者 GMP理论的实践者

搜索
查看: 2338|回复: 2
收起左侧

[统计应用] [R语言自动化应用]Calendar Notification

[复制链接]
药徒
发表于 2018-8-7 23:07:41 | 显示全部楼层 |阅读模式

欢迎您注册蒲公英

您需要 登录 才可以下载或查看,没有帐号?立即注册

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的计划任务,让该程序定期自动运行。

全部代码附在后面,需要请拿走。


11.png


22.png


33.png

  1. library(RDCOMClient)
  2. library(stringr)

  3. email_send<-function(moninfo){
  4.     receiver_list<-paste("yao_shu_liang@lilly.com",
  5.                          sep=";",collapse = NULL)   
  6.     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.")
  7.     outApp<-COMCreate("Outlook.Application")
  8.     outMail<-outApp$CreateItem(0)
  9.     outMail[["To"]]=receiver_list
  10.     outMail[["subject"]]="Dynamic EM Notification"
  11.     outMail[["HTMLbody"]]=content_body
  12.     outMail$Send()
  13. }

  14. ot<-COMCreate("Outlook.Application")
  15. otf<-ot$GetNamespace("MAPI")
  16. calendar<-otf$GetDefaultFolder(9)

  17. sublist<-vector()
  18. location<-vector()
  19. startlist<-vector()
  20. endlist<-vector()

  21. 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()),"'"))
  22. filtcal<-calendar$Items()$Restrict(strfilter)

  23. for(i in seq_len(filtcal[["Count"]])){
  24.   sublist[i]<-filtcal$Item(i)[["Subject"]]
  25.   location[i]<-filtcal$Item(i)[["Location"]]
  26.   startlist[i]<-filtcal$Item(i)[["Start"]]
  27.   endlist[i]<-filtcal$Item(i)[["End"]]
  28. }

  29. moninfo<-data.frame(sublist,location,start=startlist,end=endlist,stringsAsFactors = FALSE)
  30. moninfo$start<-as.Date(moninfo$start,format="%Y-%m-%d",origin="1900-01-01")-2
  31. moninfo$end<-as.Date(moninfo$end,format="%Y-%m-%d",origin="1900-01-01")-2
  32. moninfo

  33. Notify_index<-str_detect(as.character(moninfo$sublist), pattern="Static")
  34. Current_mon<-str_c("Mon:",str_sub(Sys.Date(),start=1,end=7))

  35. if(!file.exists("~/EM_Notify_mark.csv")){
  36.   email_mark<-data.frame(Month="Mon:Undefined",MailSent="N",stringsAsFactors = FALSE)
  37.   write.csv(email_mark,file="~/EM_Notify_mark.csv",row.names=FALSE)
  38. }

  39. email_mark_temp<-read.csv("~/EM_Notify_mark.csv",stringsAsFactors = FALSE)

  40. if(file.exists("~/EM_Notify_mark.csv") & !any(str_detect(email_mark_temp$Month,Current_mon))){
  41.   email_mark_temp<-read.csv("~/EM_Notify_mark.csv",stringsAsFactors = FALSE)
  42.   email_send(moninfo)
  43.   email_mark_temp<-rbind(email_mark_temp,data.frame(Month=Current_mon,MailSent="Y",stringsAsFactors = FALSE))
  44.   write.csv(email_mark_temp,file="~/EM_Notify_mark.csv",row.names=FALSE)
  45. }
复制代码





其它应用更多惊喜等待你。
回复

使用道具 举报

药士
发表于 2018-8-8 09:34:16 | 显示全部楼层

如果有检测计划,自动发送邮件 提示 。

感觉,不如发红包好。
回复

使用道具 举报

药士
发表于 2018-8-7 23:32:21 | 显示全部楼层
什么玩意儿
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

×发帖声明
1、本站为技术交流论坛,发帖的内容具有互动属性。您在本站发布的内容:
①在无人回复的情况下,可以通过自助删帖功能随时删除(自助删帖功能关闭期间,可以联系管理员微信:8542508 处理。)
②在有人回复和讨论的情况下,主题帖和回复内容已构成一个不可分割的整体,您将不能直接删除该帖。
2、禁止发布任何涉政、涉黄赌毒及其他违反国家相关法律、法规、及本站版规的内容,详情请参阅《蒲公英论坛总版规》。
3、您在本站发表、转载的任何作品仅代表您个人观点,不代表本站观点。不要盗用有版权要求的作品,转贴请注明来源,否则文责自负。
4、请认真阅读上述条款,您发帖即代表接受上述条款。

QQ|手机版|蒲公英|ouryao|蒲公英 ( 京ICP备14042168号-1 )  增值电信业务经营许可证编号:京B2-20243455  互联网药品信息服务资格证书编号:(京)-非经营性-2024-0033

GMT+8, 2025-3-26 06:51

Powered by Discuz! X3.4运维单位:苏州豚鼠科技有限公司

Copyright © 2001-2020, Tencent Cloud.

声明:蒲公英网站所涉及的原创文章、文字内容、视频图片及首发资料,版权归作者及蒲公英网站所有,转载要在显著位置标明来源“蒲公英”;禁止任何形式的商业用途。违反上述声明的,本站及作者将追究法律责任。
快速回复 返回顶部 返回列表