SourceSheet (sample data) is available: SourceSheet
Trying to implement protection rules:
- The ranges B2:B10 and B13:B21 can be edited by everyone.
- Ranges D2:D10 and F2:F10 can only be edited by users specified by email account.
- All the other remaining cells are protected and nobody can edit them.
function setEditPermissions() {
var sheetUrl = 'https://docs.google.com/spreadsheets/d/1r3nQZ1ZS0Y9zUv1ZfdjQlWIVHSqf-wF4neNDksCse6Y/edit?usp=drive_link';
var sheetName = 'Sheet1';
protectAllCells(sheetUrl, sheetName )
setEditable(sheetUrl, sheetName)
setEmailAccess(sheetUrl, sheetName)
}
function protectAllCells(sheetUrl, sheetName ) {
var ss = SpreadsheetApp.openByUrl(sheetUrl);
var sheet = ss.getSheetByName(sheetName);
// Защищаем все ячейки на листе
var protection = sheet.protect().setDescription('Защита всего листа');
// Устанавливаем права доступа на редактирование
var me = Session.getEffectiveUser();
protection.addEditor(me);
}
function setEditable(sheetUrl, sheetName) {
var ss = SpreadsheetApp.openByUrl(sheetUrl);
var sheet = ss.getSheetByName(sheetName);
var editableRanges = ['B2:B10', 'B13:B21'];
editableRanges.forEach(function(rangeStr) {
var range = sheet.getRange(rangeStr);
var protection = range.protect().setDescription('Защита диапазона ' + rangeStr);
protection.remove();
});
}
function setEmailAccess(sheetUrl, sheetName) {
var ss = SpreadsheetApp.openByUrl(sheetUrl);
var sheet = ss.getSheetByName(sheetName);
// Диапазоны, которые нужно защитить
var rangesToProtect = ['D2:D10', 'F2:F10'];
// Пользователи, которым разрешено редактировать диапазоны
var allowedUsers = ['[email protected]'];
rangesToProtect.forEach(function(rangeStr) {
var range = sheet.getRange(rangeStr);
var protection = range.protect().setDescription('Защита диапазона ' + rangeStr);
// Удаляем все предыдущие редакторы
protection.removeEditors(protection.getEditors());
// Запрещаем редактирование всем, кроме указанных пользователей
protection.addEditors(allowedUsers);
// Разрешаем только указанным пользователям редактировать
//protection.setWarningOnly(true);
});
}
First step (protecting the sheet worked by function protectAllCells). The next 2 steps (functions: setEditable, setEmailAccess) did not worked. But logic seems to be correct.
Is it possible to implement it using Google Apps Script and Google Sheets by codes?

I believe your goal is as follows.
['B2:B10', 'B13:B21'].To achieve this goal, I prepared the following flow. To test my script, please do the following flow.
var editableRanges = ['B2:B10', 'B13:B21']can be edited by "user A", "user B" and me.var rangesToProtect = ['D2:D10', 'F2:F10']can be edited by "user A" and me.var editableRanges = ['B2:B10', 'B13:B21']andvar rangesToProtect = ['D2:D10', 'F2:F10']can be edited by only me.The sample script is as follows.
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet. And, please set your variables.
In this script, the email address of "user A" is
var allowedUsers = ['###'];.['B2:B10', 'B13:B21']and['D2:D10', 'F2:F10']are set as the unprotected ranges. After this, the ranges['D2:D10', 'F2:F10']is protected with the email.Testing:
When this script is run, the following result is obtained.
['B2:B10', 'B13:B21']can be edited by "user A", "user B" and the owner of Spreadsheet.['D2:D10', 'F2:F10']can be edited by "user A" and the owner of the Spreadsheet.['B2:B10', 'B13:B21']and['D2:D10', 'F2:F10']can be edited by only the owner of the Spreadsheet.