Skip to content

Commit ba1828a

Browse files
Merge pull request #59 from contributorpw/edits
2 parents e4b48bc + 1296d06 commit ba1828a

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"timeZone": "Europe/Moscow",
3+
"exceptionLogging": "STACKDRIVER",
4+
"runtimeVersion": "V8"
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "container-bound-sheet",
3+
"src": []
4+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file webpack.config.js
3+
* @author Alex Ivanov
4+
* @email ai@contributor.pw
5+
*
6+
* The snippet processes the list of sheets of the spreadsheet.
7+
* It protects the sheets.
8+
* It excludes ranges from protection if there is the specific list.
9+
*
10+
* Protection of multiple sheets
11+
*
12+
*/
13+
14+
const SHEET_LIST = ['Sheet1', 'Sheet2', 'Sheet3', 'Sheet23'];
15+
const UNPROTECTED_ADDRS = ['A1:C9'];
16+
17+
/**
18+
* Runs the snippet
19+
*/
20+
function userActionRunSnippet() {
21+
const book = SpreadsheetApp.getActive();
22+
const sheets = book
23+
.getSheets()
24+
.filter((sheet) => SHEET_LIST.includes(sheet.getName()));
25+
26+
sheets.forEach((sheet) => {
27+
const unprotected = UNPROTECTED_ADDRS.map((addr) => sheet.getRange(addr));
28+
protect_(sheet, unprotected);
29+
});
30+
}
31+
32+
/**
33+
* Protects the sheet
34+
*
35+
* @param {GoogleAppsScript.Spreadsheet.Sheet} sheet
36+
* @param {Array.<GoogleAppsScript.Spreadsheet.Range>}
37+
* @return {GoogleAppsScript.Spreadsheet.Sheet}
38+
*/
39+
function protect_(sheet, unprotected = []) {
40+
const protection = sheet.protect().setDescription('Auto protection');
41+
if (unprotected.length) protection.setUnprotectedRanges(unprotected);
42+
var me = Session.getEffectiveUser();
43+
protection.addEditor(me);
44+
protection.removeEditors(protection.getEditors());
45+
if (protection.canDomainEdit()) protection.setDomainEdit(false);
46+
return sheet;
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: 'Protection of multiple sheets'
3+
date: '2021-03-04'
4+
description: 'Protects the list of sheets'
5+
tags: ['SpreadsheetApp', 'Protection']
6+
categories: ['snippets']
7+
---
8+
9+
## Protection of multiple sheets
10+
11+
The snippet processes the list of sheets of the spreadsheet. It protects the sheets. It excludes ranges from protection if there is the specific list.
12+
13+
- {{< externalLink >}}
14+
- {{< commentLink >}}
15+
16+
{{< codeFromFile "index.js" >}}
17+
{{< codeFromFile "appsscript.json" >}}

0 commit comments

Comments
 (0)