【问题标题】:How to freeze a range of cells when a checkbox is clicked?单击复选框时如何冻结一系列单元格?
【发布时间】:2019-08-22 02:15:51
【问题描述】:

工作表的屏幕截图:

我只需要有人帮我编写简单的代码来在单击某个复选框时冻结一系列单元格。

我希望当我点击“完成”复选框时,它上面的所有复选框都无法再编辑或更改。 Vise Versa 当未选中“完成”复选框时,上述复选框是可编辑的。就这么简单。

这张表的目的是为了上课。当我完成考勤后,我不想再更改它(或冒险点击错误的复选框)。这就是完整按钮存在的原因。

谁能帮我写代码?

(冻结或密封或保护)

此代码不起作用(我是初学者,非常抱歉)

function onEdit() {
  var sheet = SpreadsheetApp.getActive();;
  var completedRow = sheet.getDataRange();  
  
  for (i = 2; i < 18; i++){
    var isComplete = source.getRange(countRow, i).getValue();
    
    if (isComplete === true){
      source.getRange(2, i, countRow-1).protect();
    }
  }
  
}

【问题讨论】:

  • 欢迎。 StackOverflow 不是定制的编码服务。你说“谁能帮我写代码,好吗?” - 这里不受欢迎的要求。另一方面,您还说“我只是需要有人帮我写简单的代码”,您会发现很多用户乐于提供帮助。我建议您编辑您的问题以包含您已经编写的任何代码/您所做的任何研究。
  • 想法:i) 使用 onEdit(e) 获取编辑的行、列和值 - 可能类似于:var edittedRow = e.range.rowStart;var edittedColumn = e.range.columnStart;var newValue = e.value;。 ii) 使用IF 语句检查编辑是否在第 20 行并且复选框的值是否为“true” - 可能类似于:if (edittedRow === 20 &amp;&amp; newValue === "TRUE"){。 iii) 定义一个范围并保护它——也许像:var protectRange = sheet.getRange(1,edittedColumn,19,1);, var protection = protectRange.protect().setDescription('Sample protected range').setWarningOnly(true);.
  • 感谢您提供您的代码。

标签: javascript google-apps-script google-sheets


【解决方案1】:

您的代码反映了基本逻辑,尽管存在一些语法缺陷。希望这个答案能帮助您理解和适应这种语法。

  • 代码没有利用onEdit(e) 可用的Event Objects,其中包括已编辑单元格的行、列和值。使用 Event 对象不是强制性的,但它们确实让生活更轻松。
  • countRow 未定义;并且因为您正在使用有限长度(20 行)的电子表格;这可能是不必要的。但允许更大的电子表格是一个明智的想法。也许像var countRow = sheet.getLastRow(); 这样的东西会是一个不错的选择Doc Ref
  • isComplete - 我们知道这总是在第 20 行;我们也知道它将具有“true”或“false”的值。因此,您不需要循环来定义这一行。
  • 在某个阶段,您可能想要“取消保护”列;在新学期或新学期开始时说;因此,检查第 20 行的“false”值可能会很有用。

您的目标可能可以通过多种方式实现。以下应被视为只是一种选择。

  • 主要功能设置在onEdit(e)simple trigger中。
  • 我还设置了一个custom menu(使用onOpen),让您可以查看所有受保护的列,并在需要时取消保护。
  • 我还在代码中留下了一些Logger.log 语句,可以让您在代码的关键阶段检查某些字段的值。
  • 总而言之,此代码遵循与您的代码相同的逻辑,但更详细。
  • 最后一件事,此代码旨在通过 var sheet = ss.getSheetByName(sheetname); 在特定工作表上工作,但您可以轻松地将其更改为 var sheet = SpreadsheetApp.getActiveSheet(); 以使其在电子表格中的多个工作表上工作。

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "Sheet1";
  var sheet = ss.getSheetByName(sheetname);

  // set variable for last column


  //Logger.log(JSON.stringify(e))
  // set variables for edited cells, 
  var edittedRow = e.range.rowStart;
  var edittedColumn = e.range.columnStart;
  var newValue = e.value;
  var headerrange = sheet.getRange(1, edittedColumn);
  var headervalue = headerrange.getDisplayValue();
  //Logger.log("DEBUG: The header range is "+headerrange.getA1Notation()+", and the value is "+headervalue);

  // test if edit row =20, and the checkbox was ticked
  if (edittedRow === 20 && newValue === "TRUE") {
    //Logger.log("DEBUG: The 'ON' leg applies");
    //Logger.log("DEBUG: edittedRow = "+edittedRow+", Editted column = "+edittedColumn+", and value = "+newValue);

    // define the range to protect
    var protectRangeOn = sheet.getRange(1, edittedColumn, 19, 1);
    // protect the range - warning only.
    protectRangeOn.protect().setDescription(headervalue)
      .setWarningOnly(true);
    //Logger.log("DEBUG1: protection set for "+protectRangeOn.getA1Notation());
  }

  //test if edit row=20, and the checkbox was unticked
  if (edittedRow === 20 && newValue === "FALSE") {
    //Logger.log("DEBUG: The 'OFF' leg applies");
    //Logger.log("DEBUG: edittedRow = "+edittedRow+", Editted column = "+edittedColumn+", and value = "+newValue);

    // define the range to unprotect
    var protectRangeOff = sheet.getRange(1, edittedColumn, 19, 1);
    var protections = sheet.getProtections(SpreadsheetApp
      .ProtectionType.RANGE)
    for (var i = 0; i < protections.length; i++) {

      Logger.log("protections range name = " + protections[i]
        .getDescription() + " - Header value = " + headervalue);
      if (protections[i].getDescription() === headervalue) {
        //Logger.log("DEBUG: OFF matches")
        protections[i].remove();
      }
    }
    //Logger.log("DEBUG2: protection unset for "+protectRangeOff.getA1Notation());
  }


}

// Add a custom menu to the active spreadsheet to access Utilities
function onOpen(e) {
  SpreadsheetApp.getUi()
    .createMenu('Protection Utilities')
    .addItem('Show all protections', 'uigetprotections')
    .addItem('Remove all protections', 'removeallprotections')
    .addToUi();
}

function removeallprotections() {
    // remove all protections
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "Sheet1";
  var sheet = ss.getSheetByName(sheetname);
  var protections = ss.getProtections(SpreadsheetApp.ProtectionType
    .RANGE);
  Logger.log(protections);
  for (var i = 0; i < protections.length; i++) {
    var protection = protections[i];
    Logger.log(protection.getEditors())
    if (protection.canEdit()) {
      protection.remove();
    }
  }
  // Display confirmation dialog
  var ui = SpreadsheetApp.getUi();
  var response = ui.alert('REMOVE ALL PROTECTION',
    'Confirmed: Removed all protections', ui.ButtonSet.OK);
}

function uigetprotections() {
    // generate a list of all RANGE protections
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "Sheet1";
  var sheet = ss.getSheetByName(sheetname);
  var protections = ss.getProtections(SpreadsheetApp.ProtectionType
    .RANGE);
  //Logger.log(protections);
  var ui = SpreadsheetApp.getUi();
  var protectioninfo = "";
  if (protections.length != 0) {
    for (var p = 0; p < protections.length; p++) {
      //Logger.log("DEBUG: Date = "+protections[p].getDescription()+", Range = "+protections[p].getRange().getA1Notation());
      protectioninfo = protectioninfo + "Date: " + protections[p]
        .getDescription() + ", Range = " + protections[p].getRange()
        .getA1Notation() + "\n";
    }
    var response = ui.alert('SHOW ALL PROTECTIONS', protectioninfo, ui
      .ButtonSet.OK);
  } else {
    var response = ui.alert('SHOW ALL PROTECTIONS',
      "There were no protected ranges", ui.ButtonSet.OK);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多