【问题标题】:How to use protection setWarningOnly(warningOnly) in google spreadsheet如何在谷歌电子表格中使用保护 setWarningOnly(warningOnly)
【发布时间】:2015-09-05 14:51:14
【问题描述】:

我看到谷歌开发者在这里发布了 Note。 https://developers.google.com/apps-script/releases/ 在 2015 年 8 月 4 日,他们说“向电子表格服务添加了以下方法,以让脚本控制电子表格范围的“基于警告”的保护”

*Protection.isWarningOnly() *Protection.setWarningOnly(warningOnly)

我正在尝试使用谷歌应用脚​​本制作基于警告的保护表。(它应该有一些不受保护的单元格)

我试过这样,但它不起作用。

  var sheet = SpreadsheetApp.getActiveSheet();
 var protection = sheet.protect().setDescription('Sample protected sheet').isWarningOnly()
 var unprotected = sheet.getRange('B2:C5');
 protection.setUnprotectedRanges([unprotected]);
 protection.setWarningOnly(warningOnly)

有人成功使用warningOnly 方法吗? 请告诉我如何使用它。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    有几种方法可以实现这一点。如果您只想设置单元格的保护值,可以使用以下内容:

    function warningOnly() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var cell = sheet.getActiveCell();
    
      // setWarningOnly is a boolean value, which means you have to set
      // it in the code as true or false.
      cell.protect().setWarningOnly(true);
    }
    

    如果您的工作表具有受保护的范围,并且您想更改它们以包含警告,您可以对单元格运行快速测试以找出:

    function addNewWarning() {
      var ss = SpreadsheetApp.getActive();
    
      // Test to see if a cell has a range protected already
      // If it is protected, remove the old rule and add the warning.
      // If it's not protected already, skip it.
    
      var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE)
      for(var i=0;i<protections.length;i++) {
        var protection = protections[i];
    
        // If it's protected and you can edit, remove it and add a warning.
        if (protection.canEdit()) {
          protection.remove();
          protection.getRange().protect().setWarningOnly(true);
          Logger.log("Warning added");
        } else {
          Logger.log("Not protected, no warning added");
        }
      }
    }
    

    我找不到更改警告消息的方法,但它会得到您正在寻找的行为。希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助!现在我了解了 setWarningOnly 的工作原理!你是对的,它需要设置“真”!再次感谢你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多