Forum Discussion

SamGin's avatar
SamGin
Copper Contributor
Feb 23, 2023

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 there are a dynamic number of rows.

 

I manually open the Excel attachment, and copy all of the data - excluding the header row - and then paste those results into an Excel sheet in Sharepoint.  This provides me a running total for historical reporting.

 

How can I go about automating this process so when the email is received, the data within the attachment is automatically appended to the central sheet in Sharepoint?

 

Thanks for your time!

  • G_Vijai_Kumar's avatar
    G_Vijai_Kumar
    Copper 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
    }
    • EmmaFox's avatar
      EmmaFox
      Copper Contributor

      G_Vijai_Kumar

       

      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 

Resources