Unlocking Password-Protected Worksheet in Microsoft Office Scripts

I am in the process of removing protection from my spreadsheet so that I can eliminate autofilter and delete certain rows. My ultimate goal is to create a table in power automate, allowing me to import it into Power Apps.

How do I go about unprotecting my sheet?

The code snippet below is not working as expected, likely because it does not include the necessary password. How can I modify it to accept a password?

function main(workbook: ExcelScript.Workbook) {

    let selectedSheet = workbook.getActiveWorksheet();
    selectedSheet.getProtection().unprotect();
    // Turn off autofilter on selectedSheet
    selectedSheet.getAutoFilter().remove();
    // Delete rows 1 through 6 on selectedSheet
    selectedSheet.getRange("1:6").delete(ExcelScript.DeleteShiftDirection.up);
}

Error:

Line 4: WorksheetProtection unprotect: The argument is invalid or missing or has an incorrect format.
It seems like the issue may be related to the absence of the password in the script. I have the password but unsure where to input it.

I reviewed this example but could not find a place to enter the password.

Protect Worksheet in Office Scripts with Options

Updated error:

https://i.sstatic.net/4Dg6f.png

https://i.sstatic.net/3vY9R.png

Error message:

{ "message": "We were unable to run the script. Please try again.\nWorkbook not found.\r\nclientRequestId: b29bf86c-8a12-4ab3-b639-2350a0022b89", "logs": [] }

Any assistance would be greatly appreciated.

Answer №1

If you want to utilize additional parameters, the key is to initiate the call from Power Automate. However, before doing so, ensure that you integrate the parameter into your script.

https://learn.microsoft.com/en-us/office/dev/scripts/develop/power-automate-parameters-returns

Here is an example of how to incorporate it into your script:

function main(workbook: ExcelScript.Workbook, password: string) {
    let selectedSheet = workbook.getActiveWorksheet();
    selectedSheet.getProtection().unprotect(password);

    // Toggle auto filter on selectedSheet
    selectedSheet.getAutoFilter().remove();
    
    // Delete range 1:6 on selectedSheet
    selectedSheet.getRange("1:6").delete(ExcelScript.DeleteShiftDirection.up);
}

Once integrated into the script, you can easily refer to it from PowerAutomate or LogicApps.

https://i.sstatic.net/ISWOI.png

Note: When using workbook.getActiveWorksheet(), be cautious as it must be executed correctly within PA/LA, especially if there are multiple worksheets in the workbook.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Delete information based on its unique identifier

Currently, I am trying to remove data from a list but encountering a small issue with the following code. Component Code removeSelectedRows(){ console.log(this.selection.selected.map(item => item.userId)) const selectedRowIds = this.selection. ...

Creating recursive interfaces in TypeScript allows for defining reusable structures that reference themselves within

Defining a Modular Type System Struggling to create a modular type system that meets my needs in an efficient manner. The core issue revolves around needing two variations of the same type system for frontend and backend purposes. This requirement stems f ...

Enforce numerical input in input field by implementing a custom validator in Angular 2

After extensive research, I was unable to find a satisfactory solution to my query. Despite browsing through various Stack Overflow questions, none of them had an accepted answer. The desired functionality for the custom validator is to restrict input to ...

Tips on displaying hyperlinks within a text area in an Angular application

In my Angular application, I am facing an issue with displaying hyperlinks within a text area box for dynamic content. The hyperlinks are not recognized and therefore cannot be clicked. Can someone please advise on how to properly display hyperlinks with ...

How to calculate the sum of all values in a FormArray in Angular

I am trying to retrieve the input values from each row and then calculate the sum of these rows. Here is my HTML code: <ng-container formArrayName="cap_values"> <tbody *ngFor="let item of capValues.controls; let i=index" [formGroupName]="i"& ...

How can I center align my loader inside app-root in Angular2+?

I've successfully added a basic spinner to my <app-root> in the index.html file. This gives the appearance that something is happening behind the scenes while waiting for my app to fully load, rather than showing a blank white page. However, I& ...

Learn how to display data from the console onto an HTML page using Angular 2

I am working on a page with 2 tabs. One tab is for displaying active messages and the other one is for closed messages. If the data active value is true, the active messages section in HTML should be populated accordingly. If the data active is false, th ...

Steps for assigning a value from an enumerated type

I searched extensively online and came across resources like this: https://www.typescriptlang.org/docs/handbook/enums.html, but none provided an answer to my specific inquiry. Within the enum generated by typescript-generator, I have the following: type ...

Typescript error in React: The element is implicitly of type any because a string expression cannot be used to index type {}

I'm currently working on grouping an array by 'x' in my React project using TypeScript, and I've encountered the following error message: Element implicitly has an 'any' type because expression of type 'string' can&a ...

What is causing the failure of this polymorphic assignment within a typed observableArray in Knockout?

Consider a scenario where we have a class A and its subclass B. The goal is to assign an array of instances of class B to an array of instances of class A. While this works with normal arrays, the same operation fails when using ko.ObservableArray. import ...

Angular TSLint: Proceed to the following stage despite any encountered errors

I'm facing issues with TSLint in my Azure Devops Build Pipeline. Despite encountering lint errors, I need the build pipeline to proceed to the next step. How can I achieve this? Command Line: - script: | npm run lint > tsLintReport.txt ...

Error encountered when attempting to retrieve token from firebase for messaging

I am currently working on implementing web push notifications using Firebase. Unfortunately, when attempting to access messaging.getToken(), I encounter an error stating "messaging is undefined." Below is the code snippet I am utilizing: private messaging ...

How to conditionally make a property optional in Typescript depending on the value of another property

I'm a newcomer to Typescript and I've encountered a scenario that has been difficult for me to find a solution for. Any suggestions would be greatly appreciated. My goal is to have the property options be optional when the type is either SHORT_T ...

Angular - Executing a function in one component from another

Within my Angular-12 application, I have implemented two components: employee-detail and employee-edit. In the employee-detail.component.ts file: profileTemplate: boolean = false; contactTemplate: boolean = false; profileFunction() { this.profileTempla ...

Simulating chained responses in Express using JEST

I am relatively new to using jest and typescript, currently working on creating a unit test for a controller function in jest import { Request, Response } from 'express'; const healthCheck = (_req: Request, _res: Response) => { const value ...

Customizing font color upon hover in Next.js and Tailwind.css

Recently, I developed a Navbar component that displays a purple link when navigating to pages like Home or Projects. The issue arises when the background color is light; in this case, the link turns green on hover instead of staying purple. How would I adj ...

How to display a TypeScript map in a React JSX component

I am currently working with a map object: const myMap = new Map<number,string>([ [10, "text1"], [15, "text2"], [20, "text3"] ]) Could anyone provide me with guidance on how to iterate over this map in React JSX ...

Having difficulty getting the useParams hook in React to update when the URL changes

I am experiencing difficulties updating the pageNum value using useParams in response to URL changes. My hooks are structured as follows: let {pageNum = '1'} = useParams<GetMoviesParams>(); let history = useHistory(); const [mov ...

Loop through JSON results in Ionic using Angular

I am struggling to retrieve data from a JSON file in Object format using Typescript. When I try to fetch the data from the API, it doesn't display as expected. Typescript this.http.get('http://example.com/api') .subscribe((data) => { ...

Tips for showcasing JSON data in an HTML table or list

I am seeking a way to dynamically display changing JSON object data in a table using Angular. The structure of the JSON object is subject to change due to server updates. JSON Object: { "This item has 1 value":1, "Another":30, "It ...