Notifications
Announcements
Hi everyone and dear @abm ,I have a SharePoint folder with lots of Excel files which anyone has one similar work sheet which name is "Major". I want to delete all other worksheets in each excel file with a flow in power automate.thanks a lot
What should I write in Worksheet part?and another problem is that the spell of major is different in different excels. (Major, major, MaJor and ....)
Hi, @Mhn_Va, you can do this using the actions available from the SharePoint and Excel connectors in Power Automate.
I should first state that it would really help us to assist your flow building for you to embed (not attach, as many cannot download files) screenshots of your flow as it is right now. This will aide anyone wishing to assist you in providing much needed context.
Plus, if you really feel like adding incentive then you can include some dummy data! But that is not strictly necessary. ‌😅‌
I will post a full solution soon, but you need to look at the SharePoint action named 'Send an HTTP request to SharePoint' to make custom requests. Then use that in conjunction with the V2 Sharepoint API (which is 'Graph' endpoints) to work with the Excel data in each sheet.
You will be mostly concerned with the DELETE worksheet Graph API endpoint to do this, but it isn't easy to 'guess' the path needed to get there, so I will help you build it.
In my solution post (to come) I will edit in, eventually, a copy / paste Scope action which you can use CTRL+V to paste into your flow by going to the 'My clipboard' tab in the old designer. I am unaware of how to paste an action in the new designer, sorry.
OK, solution time, @Mhn_Va !! 🙂
As I said before, once I have finished editing this I will place a block of code which you can paste into your flow. Currently I only know how to do this using the old designer, so I will leave instructions of how to paste actions in the below spoiler.
I would also recommend that you send an email containing a report on any files that *do not* contain the "Major" worksheet by performing a separate filter on the worksheets to check FOR the "Major" sheet. If the length() of the filter body is 0 then there was no "Major" sheet.
I have assumed that this will be working from an automated flow running on a 'When a file is created or modified (properties only)' trigger.
I have not assumed anything else, since the information is not present. There may be many things which change how this is appropriate, but being as that information is not in the original question, it is hard to work with that.
Here is an image of the 3 actions that you need in order to remove all worksheets other than the one named "Major" which should work just fine for you:
I will break down each action below.
NB: The only important thing that I would say here is to ensure that you are selecting a SharePoint library, here, not anything else. Otherwise none of this works.
I have purposefully edited sensitive information from my examples, but you can ensure that this is all filled out correctly.
If you are placing this ALL within an 'Apply to each' or 'For each' this is where anything information from that will be necessary to edit in.
This filters out any sheets named "Major" by checking the 'Name' field of every worksheet listed by the previous action, and ensuring that when it does it checks it in lower case.
You can see the expression below to convert the name to lower case for the condition.
toLower(item()?['name'])
This is simply an 'Apply to each' or 'For each' action using the output of the 'Filter array' action as the source to iterate over.
This is the only action inside the loop, which will remove the sheet.
You will note a few functions / expressions in the Uri path, which I have gone into some detail in the below spoiler.
This expression pulls the Site ID from the Get worksheets action:
split( trim( replace( actions('Get_worksheets')?['inputs/parameters/source'], 'sites/', '' ) ), ',' )[1]
From the inside out you can see this:
This expression pulls the Drive ID from the Get worksheets action:
actions('Get_worksheets')?['inputs/parameters/drive']
As you can see this uses the same action as the previous expression but directly to get the exact value of the the required key.
This expression pulls the Item ID from the Get worksheets action:
actions('Get_worksheets')?['inputs/parameters/file']
Almost exactly the same expression as the Drive ID one, except here it is pointing at the 'file' key instead.
This expression pulls the Worksheet ID from the Get worksheets action:
encodeUriComponent( items('ForEverySheet')?['id'] )
Since the Worksheet ID is the one *output* of the Get worksheets action it is something that we must take from our loop. So, here, you can see:
Below will be code that you can paste into your Power Automate flow using the instructions which I noted at the start. You will be asked to ensure that the connections are valid.
{ "id": "f24900bb-a2ac-46a4-99e0-5d42e300b372", "brandColor": "#8C3900", "connectionReferences": { "shared_excelonlinebusiness": { "connection": { "id": "/providers/Microsoft.PowerApps/apis/shared_excelonlinebusiness/connections/shared-excelonlinebu-PING-PONG" } }, "shared_office365groups_1": { "connection": { "id": "/providers/Microsoft.PowerApps/apis/shared_office365groups/connections/shared-office365grou-PING-PONG" } }, "shared_sharepointonline-1": { "connection": { "id": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline/connections/shared-sharepointonl-PING-PONG" } } }, "connectorDisplayName": "Control", "icon": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=", "isTrigger": false, "operationName": "Scope_Delete_Worksheets", "operationDefinition": { "type": "Scope", "actions": { "Filter_array": { "type": "Query", "inputs": { "from": "@outputs('Get_worksheets')?['body/value']", "where": "@not(equals(toLower(item()?['name']), 'major'))" }, "runAfter": { "Get_worksheets": [ "Succeeded" ] }, "metadata": { "operationMetadataId": "236ec8f2-a903-43a3-a099-460109d82100" } }, "Get_worksheets": { "type": "OpenApiConnection", "inputs": { "host": { "connectionName": "shared_excelonlinebusiness", "operationId": "GetAllWorksheets", "apiId": "/providers/Microsoft.PowerApps/apis/shared_excelonlinebusiness" }, "parameters": { "source": null, "drive": null, "file": null }, "authentication": { "type": "Raw", "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']" } }, "runAfter": {}, "metadata": { "operationMetadataId": "adfefe06-03a6-4c87-b8f1-a3cbae5c954e" } }, "ForEverySheet": { "type": "Foreach", "foreach": "@body('Filter_array')", "actions": { "HTTPS_DELETE_Worksheet": { "type": "OpenApiConnection", "inputs": { "host": { "connectionName": "shared_sharepointonline-1", "operationId": "HttpRequest", "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline" }, "parameters": { "dataset": "https://graph.microsoft.com/", "parameters/method": "DELETE", "parameters/uri": "v1.0/sites/%7B@{split(trim(replace(actions('Get_worksheets')?['inputs/parameters/source'], 'sites/', '')), ',')[1]}%7D/drives/@{actions('Get_worksheets')?['inputs/parameters/drive']}/items/@{actions('Get_worksheets')?['inputs/parameters/file']}/workbook/worksheets(%27@{encodeUriComponent(items('ForEverySheet')?['id'])}%27)", "parameters/headers": { "Content-type": "application/json", "Accept": "application/json" } }, "authentication": { "type": "Raw", "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']" } }, "runAfter": {}, "metadata": { "operationMetadataId": "047a8141-43ea-438b-9db8-d25e29b631ee" } } }, "runAfter": { "Filter_array": [ "Succeeded" ] }, "metadata": { "operationMetadataId": "66d3f384-4323-4236-b40c-3d0ba618e025" } } }, "runAfter": { "Initialize_FileIdGraphVAR": [ "Succeeded" ], "Initialize_FolderIdGraphVAR": [ "Succeeded" ], "Initialize_WorksheetIdVAR": [ "Succeeded" ] }, "metadata": { "operationMetadataId": "1254234f-cd75-4ec6-bf28-8891a1f2b9bb" } } }
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.
In our never-ending quest to improve we are simplifying the forum hierarchy…
We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Michael E. Gernaey 501 Super User 2025 Season 2
Tomac 323 Moderator
abm abm 237 Most Valuable Professional