无法捕获“您正在尝试编辑受保护的单元格或对象。”尝试通过燃气编辑GSHEET时。如何验证编辑访问?
我正在尝试通过应用程序脚本编辑Google Sheet,但是每当代码试图编辑受保护范围
您正在尝试编辑受保护的单元格或对象。如果需要编辑,请联系电子表格所有者以删除保护。
尽管使用尝试并捕获脚本仍无法捕获错误。请建议捕获此错误的方法。
以下是我使用的代码: 如果您希望测试该方案,下面使用的表格具有一般访问权限,并且范围MasterData!A2:G3受到保护。
function myFunction() {
try{
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1TCSNpk4zUhoHo_fUDYbr-jtXCI8U1lWJlhqwNP9C5V4/edit#gid=0");
var backendsheet=ss.getSheetByName("MasterData");
backendsheet.getRange(2,2).setValue("ABC");
}
catch(err)
{
Logger.log("Error Name:" +err.name);
Logger.log("Error Message:" +err.message);
}
}
I am trying to edit a Google Sheet via Apps script, but whenever the code tries to edit a protected range the execution stops with an error
You are trying to edit a protected cell or object. Please contact the spreadsheet owner to remove protection if you need to edit.
Despite using try and catch the script fails to catch the error. Please suggest ways to catch this error.
Below is the code that I have used:
In case you wish to test the scenario, The sheet used below has general access and the range MasterData!A2:G3 is protected.
function myFunction() {
try{
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1TCSNpk4zUhoHo_fUDYbr-jtXCI8U1lWJlhqwNP9C5V4/edit#gid=0");
var backendsheet=ss.getSheetByName("MasterData");
backendsheet.getRange(2,2).setValue("ABC");
}
catch(err)
{
Logger.log("Error Name:" +err.name);
Logger.log("Error Message:" +err.message);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,我认为为了确认您是否可以编辑范围,
canedit
方法可能可以使用。当这反映在您的脚本中时,如下所示。修改后的脚本:
BackendSheet.getRange(2,2)
的范围是受保护的范围,您不能编辑,range.canedit()
。参考:
In your situation, I thought that in order to confirm whether you can edit the range,
canEdit
method might be able to be used. When this is reflected in your script, it becomes as follows.Modified script:
backendsheet.getRange(2, 2)
is protected range and you cannot edit,range.canEdit()
returnfalse
.Reference: