我試圖鎖定列“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);
}
}
uj5u.com熱心網友回復:
改裝要點:
- 在您的回圈中,
row當sheet.getRange(row, col).getValue() < 3is時加 1true。在這種情況下,row增長為1, 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);
}
}
});
}
參考:
- 獲取值()
- forEach()
編輯:
關于您在評論中的以下新問題,
不知道那張紙出了什么問題,但是當我制作了一張新紙時,它可以作業了:) 非常感謝!有沒有辦法檢查單元格是否已被鎖定,因此它不會連續鎖定單元格 20 次?我的猜測是用條件 <3 檢查它,但我對代碼的理解還不是很好 ''' values.forEach(([a], i) => { if (a < 3) && (sheet.getRange( "A" (i 1)).getDescription()='Protected, row ' (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);
}
}
});
}
- When this script is run, at first, the protected row numbers of column "A" are retrieved. And, using the retrieved row numbers, the rows of column "A" are protected. By this, the same rows which have already been protected are not protected.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/325130.html
