Hi there, each morning I get an email listing the sales of fruit and veg for the day.
The structure of the emails is as follows:
Date of report:,22-JAN-2022
Time report produced:,23-JAN-2022 00:11
Service:
$$s
Apples,5
Grapes,8
Grapefruit,6
Lemons,5
Oranges,1
Pears,2
Total,27
I've recently been able to put together some code that goes through my emails, finds values in the body and puts them into a data table:
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")
search <- OutApp$AdvancedSearch("Inbox", "urn:schemas:httpmail:subject = 'FRUIT SALES'")
Sys.sleep(5)
results <- search$Results()
results$Count()
get_vals <- function(email) {
body <- email$body()
date <- str_extract(body, "\\d{2}-[:alpha:]{3}-\\d{4}") %>%
as.character()
data <- read.table(text = body, sep = ",", skip = 10, strip.white = T) %>%
row_to_names(1) %>%
mutate("Date" = date)
return(data)
}
info <- sapply(results, get_vals, simplify = F) %>%
bind_rows()
print(info)
If I write 'info' to excel, I end up with a s/s with the following format (| represents division between A and B column, Service and Date and emboldened at the top of the s/s):
Service| Date
$$s | 22-JAN-2022
5 | 22-JAN-2022
8 | 22-JAN-2022
6 | 22-JAN-2022
5 | 22-JAN-2022
1 | 22-JAN-2022
2 | 22-JAN-2022
27 | 22-JAN-2022
What I'd like to do is have the data structured differently so that it doesn't drop the fruit it relates to. I'd like to achieve this format (dropping the $$s entirely):
Date | Apples | Grapes | Grapefruit | Lemons | Oranges | Pears | Total
22-JAN-2022| 5 | 8 | 6 | 5 | 1 | 2 | 27
Can anyone advise on how to achieve this result?