Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何在google电子表格中使用保护设置warningOnly(warningOnly)_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 如何在google电子表格中使用保护设置warningOnly(warningOnly)

Google apps script 如何在google电子表格中使用保护设置warningOnly(warningOnly),google-apps-script,google-sheets,Google Apps Script,Google Sheets,我在这里看到了谷歌开发者发布的说明。 2015年8月4日,他们说“在电子表格服务中添加了以下方法,让脚本控制“基于警告的”电子表格范围保护” *Protection.isWarningOnly() *保护。仅设置警告(仅警告) 我正在尝试用谷歌应用程序脚本制作基于警告的保护表。(应该有一些未受保护的单元格) 我试过这样做,但没用 var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().set

我在这里看到了谷歌开发者发布的说明。 2015年8月4日,他们说“在电子表格服务中添加了以下方法,让脚本控制“基于警告的”电子表格范围保护”

*Protection.isWarningOnly() *保护。仅设置警告(仅警告)

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

我试过这样做,但没用

  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)
是否有人成功地使用了仅警告的方法?
请告诉我如何使用此功能。

有几种方法可以实现此功能。如果只想设置单元格的保护值,可以使用以下选项:

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");
    }
  }
}
函数addNewWarning(){
var ss=SpreadsheetApp.getActive();
//测试以查看单元格是否已具有受保护的范围
//如果受保护,请删除旧规则并添加警告。
//如果它尚未受到保护,请跳过它。
var protections=ss.getProtections(电子表格应用程序保护类型范围)

for(var i=0;我需要帮助!现在我了解了setWarningOnly的工作原理!您是对的,它需要设置“true”!再次感谢您!