我每天都会收到一封电子邮件,其中的附件包含 1 个包含 1 个 csv 文件的 zip 文件。
在我的电子邮件正文中,有一张图片被识别为另一个附件,我很确定。
当电子邮件正文中只有文本但使用“Adobe Marketing Cloud”图像时,以下脚本可以工作,它会搞砸脚本。
有没有办法我只能读取第一个附件读取(假设将是 zip 文件)?
这是我的脚本:
library(readr)
library(RDCOMClient)
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
"Inbox",
"urn:schemas:httpmail:subject = 'SUBJECTNAME'"
)
Sys.sleep(5) # Wait a hot sec!
results <- search$Results() # Saves search results into results object
Sys.sleep(5) # Wait a hot sec!
results$Item(1)$ReceivedTime() # Received time of first search result
as.Date("1899-12-30") + floor(results$Item(1)$ReceivedTime()) # Received
date
# Iterates through results object to pull out all of the items
for (i in 1:results$Count()) {
if (as.Date("1899-12-30") + floor(results$Item(i)$ReceivedTime())
== as.Date(Sys.Date())) {
email <- results$Item(i)
}
}
attachment_file <- tempfile()
email$Attachments(1)$SaveAsFile(attachment_file)
##Automatically Determine csv file name
file_name<-unzip(attachment_file,list=TRUE)
csv_file<-file_name$Name
##Read CSV File
newly_read_data <- read_csv(unz(attachment_file,csv_file))
错误出现在这里:
file_name<-unzip(attachment_file,list=TRUE)
Error in unzip(attachment_file, list = TRUE) :
zip file 'C:\Temp\Rtmp86Gnzp\file29904a23387b' cannot be opened
任何帮助都会很棒,谢谢!