Imagine that you day week has to get data from some databases, add this in an
Excel file that is then e-mailed to someone. How could you automate this in
Logic Apps? Also, is it worth it? Maybe.
Strategy
In short, the logic app will go thru these steps:
Create an empty Excel file. Believe it or not, this is was the hardest problem
for me to solve.
Prepare the Excel-file by creating a new sheet and a new table.
Populate the table with some data.
Send the file via e-mail.
In real life, there are some more steps involved. If you follow this tutorial
you will end up with these steps:
Start by creating an empty Logic app. Normally I use the When a HTTP request
is received trigger when I do experiment like this, but feel free to use
whatever you like.
Create an empty Excel file
There are ready to use connectors for working with Excel files. All these
assumes that the files exist on OneDrive, so we will use that. The files will
just be temporary.
But unfortunately, the are no functions that let us use easily create an empty
Excel file. Not sure why, I find this to be a bit weird to be honest.
One way to solve this is to have a template file ready and then use the OneDrive
connector to copy to new file. Easy to do and totally works. But I find it a bit
annoying that need a file on my OneDrive just for that. If I accidentally remove
that file, the app will stop working.
But the approach we will use is to have the content inside the app and then
write it to a real file. Since Excel files are binary files, we will work with
the file content as a base64 string. If you have a file ready that you want to
use, you could run this PowerShell command to get the content in the right
format:
PowerShell
Otherwise, just use the sample I have below.
In your Logic app, select to add a new step.
Search for Variables.
Then select Initialize variable.
Let Name be EmptyFileContent, and the Type should be an Object. The
Value should be this massive string:
EmptyFileContent
Now add a new step. I will use OneDrive for Business, but I think ordinary
OneDrive will work too.
Select the Create file action.
Pick any folder you think is appropriate. I will use a temp folder.
The file name could be whatever you like. I prefer to use a file name that will
be unique, so I have added the date with the expression:
Date expression
And the time with the expression:
Time expression
The file content should be the variable the created in the previous step. If you
cannot see the variable in the dynamic content list, just use this expression
instead:
File content expression
The configuration should look like this:
Prepare the Excel file
Now when we have an Excel file to play with, we could start to modify it.
We start by creating a new worksheet. Add a new step and select Excel Online
(Business).
Select the action Create worksheet.
The Location should be OneDrive for Business, and the Document Library should have the value OneDrive.
The file parameter should be the file we previously created. Add this as a dynamic expression. Select the File locator value.
Lastly, let the Name be Players.
Now we will create a table on the worksheet we just created. Add a new step again. Select OneDrive for Business, and select the action Create table.
Setup up Location, Document Library and File just like you did in the previous step.
In the Table range parameter enter:
Table range expression
Also add the Table name and Column names parameters.
Let Table name have the value TablePlayers.
Let Columns names have the value:
Column names
The configuration should look like this:
Adding some data
Now it is time to add some data. Normally you will get the data from a SQL database or something similar. There are connectors for this that is easy to use. But to keep this tutorial simple we will hard code some values instead.
Add a new step and search for Data Operations.
Select the action Parse JSON.
Add this as Content:
JSON Data
Also click on Use sample payload to generate schema and paste the data and press OK.
Now we will iterate thru all this data. Add a new step and select the Control connector.
Select the For each action.
In Select an output from previous step select Players from Dynamic content.
Now select add an action inside the For each controller. Select Excel
Online (Business) and then select the action Add a row into a table.
Setup up Location, Document Library and File just like you did in the previous step.
In Table enter TablePlayers.
The Body parameter should be an JSON object. Configure it like this:
You find the variables in the Dynamic content.
In the documentation of the Excel Online (Business)
connector
you find that there is a limit of 100 calls per minute. If you do more than this
your app will stop working. So, in the For each loop it might be a good idea
to add a short delay after each row has been written. With the sample data we
are using this is not an issue so we will skip that.
Also, when all the rows have been written it might be a good idea to add a delay
for a minute or so. I have noticed that sometimes some data has been missing in
the e-mail that we soon will be send.
Sending file as an E-mail attachment
Now when the file is created, we will send the file via e-mail. Before we do that, we need to read the file content.
Add a new step and select the connector OneDrive for Business.
Select the action Get file content.
Let the File parameter be the File locator variable that you have use many times before.
Now, add a step. This time use the connector Office 365 Outlook.
Use the action Send an email.
The parameters To, Subject and Body is obvious so enter whatever you like in the.
Then add Attachments parameter.
In the Attachments Name enter playerreport.xlsx.
In the Attachments Content use the value from the previous Get file content step. You might need to click on See more.
That is all! You Logic app is now completed. Run the app to see if it works.
Summary
I think this was a fun little experiment and it works OK once all pieces are in place. I struggled a long time on how to create the Excel file, but once that was solve things were pretty much straightforward.
All this said, there are a couple of downsides with this solution:
The temporary file that is created is never removed. I did this intentionally to keep the app as simple as possible. It is easy to add a new step that remove the file via the OneDrive for Business connector. Note that you may need to add a delay for several minutes before you remove the file.
The first worksheet in the Excel file is empty. Unfortunately, there is no support to delete existing sheets when I write this. You could workaround this by using another template and not adding a worksheet in the logic app.
All columns in the table is just 64 pixels wide or so. I have not an elegant solution for this. Again, you could workaround this with a different template were all tables and sheets are premade.
Writing rows is due the limit of 100 actions per minute a bit slow. So large reports could take a couple of hours to generate. Might not be a problem be is something to be aware of.
I am not totally satisfied with the solution. Generating Excel is nothing new to me, there are some good NuGet packages out there that make this pretty easy. If you do this you have better control of everything, and it will be way faster. But I could see where a solution with a logic app would be good enough, so it is good have this this in the toolbox.