Tanaike
unread,Oct 11, 2022, 9:56:34 PM10/11/22Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google Apps Script Community
Thank you for replying. If your script is run only one time, how about the following patterns?
Pattern 1
In this pattern, Spreadsheet service (SpreadsheetApp) is used.
function sample1() {
const list = ["A:D", "6:6", "K5:U5", "E5:E5", "4:4", "35:37", "78:80", "121:123", "144:145", "166:167", "188:189", "210:211", "232:233", "253:254", "265:266", "278:281", "E7:X34", "E38:K77", "O38:U77", "E81:L120", "X81:X120", "K124:K143", "R124:R143", "E146:E165", "L146:O165", "X146:X165", "E168:H187", "E190:H209", "E234:O252", "K1:X3", "Y1:Y37", "Y78:Y281"];
SpreadsheetApp.getActiveSheet().getRangeList(list).getRanges().forEach(r => r.protect().setWarningOnly(true));
}
Pattern 2
In this pattern, Sheets API is used. Before you use this script, please enable Sheets API at Advanced Google services.
function sample2() {
const list = ["A:D", "6:6", "K5:U5", "E5:E5", "4:4", "35:37", "78:80", "121:123", "144:145", "166:167", "188:189", "210:211", "232:233", "253:254", "265:266", "278:281", "E7:X34", "E38:K77", "O38:U77", "E81:L120", "X81:X120", "K124:K143", "R124:R143", "E146:E165", "L146:O165", "X146:X165", "E168:H187", "E190:H209", "E234:O252", "K1:X3", "Y1:Y37", "Y78:Y281"];
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const ssId = ss.getId();
const sheetId = sheet.getSheetId();
const ranges = sheet.getRangeList(list).getRanges();
const requests = ranges.map((range, i) => {
var gridRange = {
sheetId,
startRowIndex: range.getRow() - 1,
endRowIndex: range.getRow() - 1 + range.getNumRows(),
startColumnIndex: range.getColumn() - 1,
endColumnIndex: range.getColumn() - 1 + range.getNumColumns(),
};
var data = list[i].match(/(.+):(.+$)/);
if (!data[1].match(/[0-9]/)) delete gridRange.startRowIndex;
if (!data[2].match(/[0-9]/)) delete gridRange.endRowIndex;
return { addProtectedRange: { protectedRange: { range: gridRange, warningOnly: true } } };
});
Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}