Reputation: 762
every day I get an email listing sales in a shop for the previous day. My email looks like this:
Date of report:,15-JAN-2022
Time report produced:,16-JAN-2022 00:25
Service:
$$s
Apples,5
Grapes,8
Grapefruit,6
Lemons,5
Oranges,1
Pears,2
Total,27
I've been able to put something together that runs through my outlook, finds the values in the email body and puts it all into a datatb:
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()
getsales_vals <- function(email) {
body <- email$body()
date <- str_extract(body, "\\d{2}-[:alpha:]{3}-\\d{4}")[1] %>%
as.character()
data <- read.table(text = body, sep = ",", skip = 7, strip.white = T) %>%
pivot_wider(names_from = V1, values_from = V2) ## this line doesn't seem to do anything
mutate("Date" = date)
return(data)
}
info <- sapply(results, getsales_vals, simplify = F) %>%
bind_rows()
print(info)
When the 'print' runs I get this in the console:
$$s Date
Apples...1 5 15-SEP-2021
Grapes...2 8 15-SEP-2021
Grapefruit...3 6 15-SEP-2021
Lemons...4 5 15-SEP-2021
Oranges...5 1 15-SEP-2021
Pears...6 2 15-SEP-2021
Total...7 27 15-SEP-2021
And if I write info to an xlsx I get this (| represents division between A and B column):
$$s | Date
5 | 15-JAN-2022
8 | 15-JAN-2022
6 | 15-JAN-2022
5 | 15-JAN-2022
1 | 15-JAN-2022
2 | 15-JAN-2022
27 | 15-JAN-2022
What I'd like to get is this (with the '$$s' dropped entirely):
Date | Apples | Grapes | Grapefruit | Lemons | Oranges | Pears | Total
15-JAN-2022| 5 | 8 | 6 | 5 | 1 | 2 | 27
I thought the use of 'pivot_wider' might achieve the desired result, but for some reason it doesn't seem to be having the desired effect.
Any advice will be greatly appreciated.
Upvotes: 2
Views: 72