Javascript 单击复选框时如何冻结单元格范围?
工作表的屏幕截图: 我只需要有人帮我写一些简单的代码,在点击某个复选框时冻结一系列单元格 我希望这样,当我点击“完成”复选框时,上面的所有内容都不能再编辑或更改。反之亦然,如果未选中“完成”复选框,则上面的复选框是可编辑的。那很简单 这张单子的目的是记录上课的情况。当我完成考勤后,我不想再更改它,也不想冒险点击错误的复选框。这就是为什么有完整的按钮 有人能帮我写代码吗 冻结、密封或保护 这个代码不起作用,我是初学者,很抱歉 函数onEdit{ var sheet=电子表格应用程序。getActive;; var completedRow=sheet.getDataRange; 对于i=2;i<18;i++{ var isComplete=source.getRangecountRow,i.getValue; 如果isComplete==真{ source.getRange2,i,countRow-1.protect; } }Javascript 单击复选框时如何冻结单元格范围?,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,工作表的屏幕截图: 我只需要有人帮我写一些简单的代码,在点击某个复选框时冻结一系列单元格 我希望这样,当我点击“完成”复选框时,上面的所有内容都不能再编辑或更改。反之亦然,如果未选中“完成”复选框,则上面的复选框是可编辑的。那很简单 这张单子的目的是记录上课的情况。当我完成考勤后,我不想再更改它,也不想冒险点击错误的复选框。这就是为什么有完整的按钮 有人能帮我写代码吗 冻结、密封或保护 这个代码不起作用,我是初学者,很抱歉 函数onEdit{ var sheet=电子表格应用程序。getAct
} 您的代码反映了基本逻辑,尽管存在一些语法缺陷。希望这个答案能帮助您理解并适应这种语法 代码没有利用onEdite可以使用的元素,这些元素包括已编辑单元格的行、列和值。使用事件对象不是强制性的,但它们确实让生活变得更轻松。 countRow未定义;因为你正在使用有限长度的20行电子表格;这可能是不必要的。但考虑到更大的电子表格是一个明智的想法。可能类似于var countRow=sheet.getLastRow;这将是一个很好的选择。 isComplete-我们知道这总是在第20行;我们也知道它的值是真是假。因此,不需要循环来定义此行。 在某些阶段,您可能希望取消对列的保护;比如在新学期或新学年开始时;因此,检查第20行的值false可能很有用。 你的目标可能可以通过多种方式实现。以下仅应视为一种选择 主要功能是在onEdite中设置的。 我还使用onOpen设置了一个,它允许您查看所有受保护的列,并在需要时删除保护。 我还在代码中留下了一些Logger.log语句,可以让您在代码的关键阶段检查某些字段的值。 总而言之,此代码遵循与代码相同的逻辑,但有一些更详细的内容。 最后一件事,由于var sheet=ss.getSheetByNamesheetname,此代码设计用于处理特定的工作表;但您也可以轻松地将其更改为var sheet=SpreadsheetApp.getActiveSheet;使其在电子表格中的多个工作表上工作。
欢迎StackOverflow不是定制的编码服务。你说谁能帮我写代码这是不受欢迎的要求在这里。另一方面,你也说过我只需要有人帮我写简单的代码,你会发现很多用户非常乐意帮忙。我建议您编辑您的问题,以包括您已经编写的任何代码/您所做的任何研究。想法:我使用onEdite获取编辑的行、列和值-可能类似于:var edittedRow=e.range.rowStart;,var edittedColumn=e.range.columnStart;和var newValue=e.value;。ii使用IF语句检查编辑是否在第20行,复选框的值是否为true-可能类似于:IF edittedRow==20&&newValue==true{.iii定义一个范围并保护它-可能像:var protectRange=sheet.getRange1,editedColumn,19,1;,var protection=protectRange.protect.setDescription'Sample protected range'.setWarningOnlytrue;。感谢您包含您的代码。
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);
}
}