Forum Discussion
SamGin
Feb 23, 2023Copper Contributor
Need to append Excel data from daily email attachment into a single Excel sheet in Sharepoint
Each morning, I receive an email with an Excel attachment that includes our sales data for the previous day. There is only 1 tab. The email always has the same number of columns, with headers, but t...
G_Vijai_Kumar
Feb 23, 2023Copper Contributor
You can do this in two ways using Power Automate OR using PowerShell. I would prefer using Power Automate. Here are the high-level steps
- Create a flow in Power Automate and select "When a new email arrives" as the trigger.
- Configure the trigger to filter for emails with the subject "sales data". You can do this by setting the "Subject filter" field to "sales data".
- Followed by "Get email" action and Get attachments action to the flow and configure it to get the details of the email.
Finally you can read row by row and create item in SharePoint list with necessary SharePoint actions.
Here is the PowerShell if you are interested in
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.Session.GetDefaultFolder(6).Items | Where-Object {$_.Attachments.Count -gt 0 -and $_.Subject -eq "sales data"} | Select-Object -First 1
$Attachment = $Mail.Attachments.Item(1)
# Read Excel
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($Attachment.Path)
$Worksheet = $Workbook.Worksheets.Item(1)
$Range = $Worksheet.UsedRange
$Data = $Range.Value()
# Connect to SharePoint site
$SiteURL = "https://sharepointurl"
$ListName = "provide list name here"
$Username = "provide user name"
$Password = "provide password"
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePassword)
Connect-PnPOnline -Url $SiteURL -Credentials $Credentials
foreach ($Row in $Data) {
$ItemProperties = @{
"Column1" = $Row[0]
"Column2" = $Row[1]
"Column3" = $Row[2]
"Column4" = $Row[3]
}
Add-PnPListItem -List $ListName -Values $ItemProperties
}
- Create a flow in Power Automate and select "When a new email arrives" as the trigger.
- Configure the trigger to filter for emails with the subject "sales data". You can do this by setting the "Subject filter" field to "sales data".
- Followed by "Get email" action and Get attachments action to the flow and configure it to get the details of the email.
Finally you can read row by row and create item in SharePoint list with necessary SharePoint actions.
Here is the PowerShell if you are interested in
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.Session.GetDefaultFolder(6).Items | Where-Object {$_.Attachments.Count -gt 0 -and $_.Subject -eq "sales data"} | Select-Object -First 1
$Attachment = $Mail.Attachments.Item(1)
# Read Excel
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($Attachment.Path)
$Worksheet = $Workbook.Worksheets.Item(1)
$Range = $Worksheet.UsedRange
$Data = $Range.Value()
# Connect to SharePoint site
$SiteURL = "https://sharepointurl"
$ListName = "provide list name here"
$Username = "provide user name"
$Password = "provide password"
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePassword)
Connect-PnPOnline -Url $SiteURL -Credentials $Credentials
foreach ($Row in $Data) {
$ItemProperties = @{
"Column1" = $Row[0]
"Column2" = $Row[1]
"Column3" = $Row[2]
"Column4" = $Row[3]
}
Add-PnPListItem -List $ListName -Values $ItemProperties
}
EmmaFox
Oct 13, 2023Copper Contributor
Thank you for sharing, but this does not solve the problem, which was to add the data to an existing workbook, not a SharePoint Online list.
Do you have a solution for adding the data to an existing workbook?
Cheers,
Emma