web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / how to add special row...
Power Automate
Answered

how to add special rows with office scripts

(0) ShareShare
ReportReport
Posted on by 68

I am working on a project with power automate. 

And there is a related online excel which is not in a table as below structure:

Invoice NumberProduct IDAmount

106

P1100
106P1200
106P2200
107P3300
107P3150

I want to add two rows, one is a subtotal row for the same Invoice Number and same product ID, the other one is an empty row. 

The result should be like:

Invoice NumberProduct IDAmount

106

P1100
106P1200
 Total:300
   
106P2200
 Total:200
   
107P3300
107P3150
 Total:450

How can I achieve the goal with Office Scripts?

 

Categories:
  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    Hi @Anya0601 ,

     

    I've made a test for your reference:

    1\My Excel table:

    vbofengmsft_0-1693203919507.png

    2\Office Script

    function main(workbook: ExcelScript.Workbook) {
    	let selectedSheet = workbook.getActiveWorksheet();
    	let getAmmounts = Number(selectedSheet.getRange('C2').getValues());
    	for(let i=2;i<1000;i++)
    	{
    		if (selectedSheet.getRange('B' + (i)).getValue() == "") { break };
    		let ProductID1 = String(selectedSheet.getRange('B' + i).getValues());
    		let ProductID2 = String(selectedSheet.getRange('B' + (i+1)).getValues());
    		if (ProductID1 == ProductID2) 
    		 {
    			getAmmounts = getAmmounts + Number(selectedSheet.getRange('C' + (i + 1)).getValues())
    		 }else{
    			selectedSheet.getRange((i + 1) + ':' + (i + 1)).insert(ExcelScript.InsertShiftDirection.down);
    			selectedSheet.getRange((i + 1) + ':' + (i + 1)).insert(ExcelScript.InsertShiftDirection.down);
    			selectedSheet.getRange('B' + (i + 1)).setValue("Total:");
    			selectedSheet.getRange('C' + (i + 1)).setValue(getAmmounts);
    			getAmmounts = Number(selectedSheet.getRange('C' + (i + 3)).getValue());
    			i=i+2
    		 }
    
    	}
    }

    Result:

    vbofengmsft_1-1693203955284.png

    Best Regards,
    Bof

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 430

#2
11manish Profile Picture

11manish 341 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 282

Last 30 days Overall leaderboard