首页 > 解决方案 > 谷歌表格不尊重我的脚本中的 IF 条件 - 为什么?

问题描述

我试图锁定 col“A”中的所有单元格,而不是包含低于 3 的数字,但无论条件如何,它都会锁定它经过的每个单元格。也许我在某个地方放错了支架?还是写这个更简单的方法?这是我的第一次尝试。我习惯在 excel 中使用 VBA,但这要困难得多。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Rezervace');

function lockRanges() {
  //First cell to lock
  var row = 1;
  var col = 1;

  // Get last row with data in sheet
  var lastRow = sheet.getLastRow();


  for (var i = row; i < lastrow; i++) {
    //Lock current cell

 if(sheet.getRange(row,col).getValue() < 3) 
 {      
  lockRange(row, col);
      row = row + 1;
  };

  };
}




function lockRange(row, col){
  var range = sheet.getRange(row, col);

  // Create protection object. Set description, anything you like.
  var protection = range.protect().setDescription('Protected, row ' + row);

 // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
 // permission comes from a group, the script will throw an exception upon removing the group.
 var me = Session.getEffectiveUser();
 protection.addEditor(me);
 protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }
}

标签: google-apps-scriptgoogle-sheets

解决方案


修改点:

  • 在您的循环中,isrow时加 1 。在这种情况下,增长为,并且单元格每个 都被锁定。我认为你的问题可能是由于这个。sheet.getRange(row, col).getValue() < 3truerow1, 2, 3,,,row
  • getValue循环中使用时,处理成本会很高。
    • 在此修改中,首先,从列“A”中检索值,并使用检索到的值保护单元格。

当这些点反映到你的脚本中时,它变成如下。

修改后的脚本:

function lockRanges() {
  var me = Session.getEffectiveUser();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Rezervace');
  var values = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
  values.forEach(([a], i) => {
    if (a < 3) {
      var protection = sheet.getRange("A" + (i + 1)).protect().setDescription('Protected, row ' + (i + 1));
      protection.addEditor(me);
      protection.removeEditors(protection.getEditors());
      if (protection.canDomainEdit()) {
        protection.setDomainEdit(false);
      }
    }
  });
}

参考:

编辑:

关于您在评论中的以下新问题,

不知道那张纸出了什么问题,但是当我制作一张新纸时,它的工作原理:)非常感谢!有没有办法检查单元格是否已经被锁定,所以它不会连续锁定单元格 20 次?我的猜测是用 <3 条件检查它,但我对代码的理解还不是很好 ''' values.forEach(([a], i) => { if (a < 3) && (sheet.getRange( "A" + (i + 1)).getDescription()='受保护,行' + (i + 1)) '''

在这种情况下,我认为需要检查“A”列的行是否受到保护。为此,我将上面的脚本更新如下。

修改后的脚本:

function lockRanges() {
  var me = Session.getEffectiveUser();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Rezervace');
  
  // Retrieve the protected rows of the column "A".
  var protectedRows = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE).reduce((ar, p) => {
    var range = p.getRange();
    if (range.getColumn() == 1) ar.push(range.getRow());
    return ar;
  }, []);
  
  // Protect the rows.
  var values = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
  values.forEach(([a], i) => {
    if (a < 3 && !protectedRows.includes((i + 1))) {
      var protection = sheet.getRange("A" + (i + 1)).protect().setDescription('Protected, row ' + (i + 1));
      protection.addEditor(me);
      protection.removeEditors(protection.getEditors());
      if (protection.canDomainEdit()) {
        protection.setDomainEdit(false);
      }
    }
  });
}
  • 运行此脚本时,首先检索列“A”的受保护行号。并且,使用检索到的行号,“A”列的行受到保护。这样,已经被保护的相同行不受保护。

推荐阅读