英文:
Unprotect Sheet(Password Protected) in Office scripts
问题
我正在尝试取消保护工作表,以便可以删除自动筛选并删除一些行。从那里,我的计划是在 Power Automate 中创建一个表,以便可以将其读入 Power Apps。
如何取消保护工作表?
我正在运行以下代码,我认为它无法正常工作是因为代码中没有密码。如何格式化代码以接受密码?
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
selectedSheet.getProtection().unprotect();
// 切换选定工作表上的自动筛选
selectedSheet.getAutoFilter().remove();
// 删除选定工作表上的范围1:6
selectedSheet.getRange("1:6").delete(ExcelScript.DeleteShiftDirection.up);
}
错误:第4行:WorksheetProtection unprotect:参数无效、缺失或格式不正确。
我再次假设这是因为我没有将密码输入到脚本中。我有密码,但不确定在哪里输入它。
我查看了这个示例,但找不到输入密码的地方。
https://stackoverflow.com/questions/64393820/protect-worksheet-in-office-scripts-with-options
更新的错误:
错误消息 { "message": "我们无法运行脚本。请重试。\n未找到工作簿。\r\nclientRequestId: b29bf86c-8a12-4ab3-b639-2350a0022b89", "logs": [] }
任何帮助将不胜感激。
英文:
I am trying to unprotect my sheet so I can remove autofilter and delete some rows. My plan from there is to create a table in power automate so i can read it into Power Apps.
How can I unprotect my sheet?
I am running the below, I assume it isnt working due to not having the password in the code. How can I format this to accept a password?
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
selectedSheet.getProtection().unprotect();
// Toggle auto filter on selectedSheet
selectedSheet.getAutoFilter().remove();
// Delete range 1: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.
Again, I am assuming this is due to me not having the password in the script. I have the password but not sure where to input it.
I look at this example but no where to enter password .
https://stackoverflow.com/questions/64393820/protect-worksheet-in-office-scripts-with-options
Updated error:
error msg {
"message": "We were unable to run the script. Please try again.\nWorkbook not found.\r\nclientRequestId: b29bf86c-8a12-4ab3-b639-2350a0022b89",
"logs": []
}
any help would be appreciated.
答案1
得分: 2
为了使用额外的参数,你需要从Power Automate中进行调用,但显然,你首先需要将该参数添加到你的脚本中。
所以对于你的脚本...
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);
}
一旦你将其添加到你的脚本中,然后你可以从Power Automate/LogicApps中引用它...
请注意:你的代码使用workbook.getActiveWorksheet()
可能会让你遇到问题,如果我是你,我会更具体,因为它需要从PA/LA执行。显然,我假设你的工作簿中不止一个工作表。
英文:
In order to use additional parameters, you need to make the call from Power Automate but obviously, you first need to add the parameter to your script.
https://learn.microsoft.com/en-us/office/dev/scripts/develop/power-automate-parameters-returns
So for 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 you've added it to your script, you can then reference it from PowerAutomate/LogicApps ...
Note: Your code to use the workbook.getActiveWorksheet()
may bring you unstuck, I'd be more specific if I were you given it needs to be executed from PA/LA. Obviously, I am making an assumption that you have more than one worksheet in your workbook.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论