无法捕获“您正在尝试编辑受保护的单元格或对象。”尝试通过燃气编辑GSHEET时。如何验证编辑访问?

发布于 2025-02-14 01:49:48 字数 625 浏览 1 评论 0原文

我正在尝试通过应用程序脚本编辑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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2025-02-21 01:49:48

在您的情况下,我认为为了确认您是否可以编辑范围,canedit方法可能可以使用。当这反映在您的脚本中时,如下所示。

修改后的脚本:

function myFunction() {
  var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1TCSNpk4zUhoHo_fUDYbr-jtXCI8U1lWJlhqwNP9C5V4/edit#gid=0");
  var backendsheet=ss.getSheetByName("MasterData");
  
  // I modified below script.
  var range = backendsheet.getRange(2, 2);
  if (range.canEdit()) {
    console.log("Your account can edit this range.");
  } else {
    console.log("Your account cannot edit this range.");
  }
}
  • 例如,当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:

function myFunction() {
  var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1TCSNpk4zUhoHo_fUDYbr-jtXCI8U1lWJlhqwNP9C5V4/edit#gid=0");
  var backendsheet=ss.getSheetByName("MasterData");
  
  // I modified below script.
  var range = backendsheet.getRange(2, 2);
  if (range.canEdit()) {
    console.log("Your account can edit this range.");
  } else {
    console.log("Your account cannot edit this range.");
  }
}
  • For example, when the range of backendsheet.getRange(2, 2) is protected range and you cannot edit, range.canEdit() return false.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文