You will have probably used the Populate a Microsoft Word Template (Premium) action in Power Automate before but will be saddened to learn that Excel doesn’t offer the same functionality. Via Tables in Excel you can add and update rows in Excel but it can be quite complex to achieve anything more. However, are you aware of Office Scripts in Excel On the Web which is currently in preview (March 2021)?
I’ve build a TypeScript routine in Office Scripts that will intuitively accept dynamic data via Power Automate, including arrays and it dynamically fills out an Excel Template Invoice. Not only will it complete the customer details, name, address, invoice number, date etc, it will dynamically complete the rows that make up the item order. If you haven’t discovered Excel Templates it is worth a look. I downloaded the Blue Invoice for my proof of concept.
Before I get started, I will admit that in the past 24 hours, I have had to learn TypeScript from scratch. I apologies to the seasoned programmers for any obvious mistakes in my code but what I have achieved is a working proof of concept ready for UAT.
The concept
I have the blue invoice template stored on my OneDrive and will take a copy of this file each time my Power Automate runs, saving a new copy with dynamic name, allowing multiple invoices to be saved. I have prepared two arrays, one for the customer data and another containing the multiple line order details. I also supply my Action with several other data fields, including invoice number, date, vat rate amongst others, all of which can be supplied dynamically if you require. I have prepared my two arrays as follows:
The Cloud flow is currently manually triggered, it copies the template file from One Drive, Composes the two Arrays, and Runs the Excel Script which is in Preview Mode. The flow looks like follows:
So where does the script live? Your One Drive for Business Files under Documents -> Office Scripts. This allows you to easily backup and share scripts that you have built, in fact if you read on and reshare this article online and tag me in your tweet or linkedin mention, I will send you a copy of the code. Thanks.
Within Excel Online, and only Online, as you cannot edit in the Excel Desktop Client from what I can tell, you can access Automate and All Scripts.
The Power Automate takes literally a few seconds to run and the Excel Template is populated and ready to send on to your client via the next action or file away for when you next need it.
What do you think about this? Is it something your business could make use of in Power Automate? Leave your comments below and please share the idea!
Want to read more about Power Automate Ideas? Click here for more!
P.S. The script file is saved on my site. If you search twitter for DamoBird365 #TypeScript you will find the link to the file. Don’t forget to like and retweet.
Hi Damien,
Wow, what an excellent solution you have shared. I have been searching for an efficient way to fill out an excel template from a PowerApps for quite some time, without success so far. Some of these solutions use Http request and I have been left halfway due to tenant restrictions in my company. But this solution that you propose is elegant, efficient and very flexible.
A few minutes ago I shared this video through Twitter, I would greatly appreciate it if you could share the Excel Script with me so I can modify it and apply it to my solution. Thank you very much in advance.
Hi Alexander, glad this has helped. Take a look here https://github.com/DamoBird365/PowerAutomate/tree/main/ExcelInvoiceDemo
Thanks a lot Damien,
I just saw your answer in twitter.
Regarding my requeriment.. it is very close to your example. I have 2 Galleries in my app. Top gallery contains fields for invoice header : customer name, date , record id, status and a botom gallery that contains the detailed information from selected row at top gallery : Items, Description, Category, Pakahe Unit, Discounts, Prices, Final Price, etc. I currently have a “CreateaPdf” Powerautomate based on Html but i requiere Excel too. I’m wondering what is the best option (thinking of Excel Scripting). Re-use html as starting code or get the records from 2 Dataverse Tables i’m populating with the same records or crreate a Json from each gallery. What would you do in my case?
Thanks a lot again for your support.
I would probably attempt JSON to Power Automate. I saw this video that might help https://youtu.be/-Udz15-jiKg. Might attempt my own in time 😉
Hi Damien, please is posible your help on this?. Do you have experience passing Json code from powerapps to powerautomate.
I have replicated your example but using ask PowerApps input parameter instead of Json code directly.
When i use compose action using Json code directly, the flow runs with no problem, but using input parameter from powerapps the Excel script doesn’t execute correctly and rows not filled the excel table.
This is the code i implemented to obtain data from 2 galleries and generate 2 Json codes to feed my Excel invoice using your example. The invoice header an table header populate successfully but as i mentioned the Invoice details doesn’t reflect in the table unless i use the Json code directly in compose action.
I really appreciate any help you can give me.
Regards-
ForAll(
Gallery1_2.AllItems,
If(
RegionalTimestampKey = Gallery1_2.Selected.RegionalTimestampKey,
Collect(
colCustomerHeader,
{
InvoiceID: ThisRecord.RegionalTimestampKey,
Hospital: ThisRecord.Hospital,
Distribuidor: ThisRecord.Distributor,
Validade: ThisRecord.Validity,
CNPJ: ThisRecord.CNPJ,
Estado: ThisRecord.State,
Status: Button4_2.Text
}
)
)
);
ForAll(
Gallery4.AllItems,
Collect(
colRecordDetails,
{
Codigo: ThisRecord.CodigoItem3M,
Descripcion: ThisRecord.Description3M,
DescripcionComercial: ThisRecord.CommercialDescription,
Division: ThisRecord.Division,
UnidadEmbalage: ThisRecord.PackingUnit,
QtyEmbalage: ThisRecord.PackagingQty,
ConsumoMedioMensual: ThisRecord.MonthlyAvgConsumption,
PrecioFinalHospital: ThisRecord.HospitalFinalPrice,
PrecioFinalHospitalUnitario: ThisRecord.HospitalFinalPrice
}
)
);
FillTemplateMSDBR3.Run(
JSON(
colCustomerHeader,
JSONFormat.IgnoreUnsupportedTypes & JSONFormat.IgnoreBinaryData & JSONFormat.Compact & JSONFormat.IndentFour
),
JSON(
colRecordDetails,
JSONFormat.IgnoreUnsupportedTypes & JSONFormat.IgnoreBinaryData & JSONFormat.Compact & JSONFormat.IndentFour
)
);
Hi Damien, me again.
I just figured out that ExcelScript V1 approach works correctly in my case due “let ObjSales = JSON.parse(varInvoiceDetails)”. Conversely, Script V2 Approach doesn’t include Json parsing and i think this could affecting the integration whe i use Input Parameter from PowerApps.
Hi Damien! Thanks so much for this tutorial, it’s super helpful. I am trying to do something very similar, but have the data in the two arrays come from another Excel file or a SQL server. Is it possible to modify the “Compose” action to pull in data from another source?
Yes it is. Let me know how you get on.
hi please help me. how to get all the gallery item in excel. my output always get the 1st row only and if i change range eq a1:a2 the output always the same value in excel
Are you using a Power App? Take a look at this video
https://youtu.be/L67m8wgd4Ak
Hi Damien
Great work, i have followed your guide. it works great and its very efficient.
Question: is it possible to insert lines in a table dynamically so that lines below are pushed downward in excel. I have some calculations below the table, that in some cases get written over.
The normal excel action add lines to a table works this way. Same functionality with office script would be awesome.
Hi Michael, glad you got it working ok. You can take a look at https://learn.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.table?view=office-scripts#excelscript-excelscript-table-addrow-member(1) alternatively try using the recording tool and apply it to the loop. As you write a row, add a row. Good luck and let me know how you get on 👍