-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.gs
220 lines (192 loc) · 5.58 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// SyncPermissions
// A tiny tool to synchronize permissions
//
// constant
const gmailColNumber = 1;
const resourceFirstColNumber = 2;
const resourceNameRowNumber = 3;
const resourceIdRowNumber = 4;
const dataFirstRowNumber = 6;
const logRangeCell = 'D1';
// these data are set by loadData()
let selectionStartRowIndex;
let selectionStartColIndex;
let data;
let rowCount;
let colCount;
let logRange;
/**
* On click Run button
*/
function onClickRun() {
let ui = SpreadsheetApp.getUi();
var html = HtmlService.createHtmlOutputFromFile('dialog')
.setWidth(300)
.setHeight(160);
ui.showModalDialog(html, "Confirm");
}
/**
* Sync permissions
*/
function syncPermissions(useSelection) {
let sheet = SpreadsheetApp.getActiveSheet();
let dataRange = sheet.getDataRange();
let params = [];
if ( useSelection ){
let selection = sheet.getSelection();
if ( selection == null ){
return;
}
let selectionRange = selection.getActiveRange();
params.push((selectionRange.getRow() - 1).toString()); // selectionStartRowIndex
params.push((selectionRange.getColumn() - 1).toString()); // selectionStartColIndex
params.push((selectionRange.getLastRow() - 1).toString()); // selectionEndRowIndex
params.push((selectionRange.getLastColumn() - 1).toString()); // selectionEndColIndex
}
else{
params.push("0"); // selectionStartRowIndex
params.push("0"); // selectionStartColIndex
params.push((dataRange.getLastRow()-1).toString()); // selectionEndRowIndex
params.push((dataRange.getLastColumn()-1).toString()); // selectionEndColIndex
}
// detect how many cells to process
loadData(params);
if ( rowCount <= 0 || colCount <= 0 ){
return;
}
// execute via LongRun
executeLongRun("syncPermissionsMain", rowCount * colCount, params, "syncPermissionsInit", "syncPermissionsFinalizer");
}
/**
* Loads data
*/
function loadData(params) {
log('Loading the data...');
// read params
selectionStartRowIndex = parseInt(params[0], 10);
selectionStartColIndex = parseInt(params[1], 10);
let selectionEndRowIndex = parseInt(params[2], 10); // not need to be global
let selectionEndColIndex = parseInt(params[3], 10); // not need to be global
// load data range
let sheet = SpreadsheetApp.getActiveSheet(); // this works even starting by trigger
data = sheet.getDataRange().getValues();
if ( data.length == 0 ){
rowCount = 0;
colCount = 0;
log('no data to process');
return;
}
// fix selection
selectionStartRowIndex = Math.max(selectionStartRowIndex, dataFirstRowNumber - 1);
selectionStartColIndex = Math.max(selectionStartColIndex, resourceFirstColNumber - 1);
selectionEndRowIndex = Math.min(selectionEndRowIndex, data.length - 1);
selectionEndColIndex = Math.min(selectionEndColIndex, data[0].length - 1);
// set rowCount, colCount
rowCount = selectionEndRowIndex - selectionStartRowIndex + 1;
if ( rowCount <= 0 ) {
console.log('no data to process');
return;
}
colCount = selectionEndColIndex - selectionStartColIndex + 1;
if ( colCount <= 0 ){
console.log('no data to process');
return;
}
}
/**
* Initializer
*/
function syncPermissionsInit(startIndex, params) {
// LongRun.instance.setMaxExecutionSeconds(10); // for test
loadData(params);
}
/**
* A function executed for each cell.
*/
function syncPermissionsMain(index) {
try {
if ( rowCount <= 0 || colCount <= 0 ){
return;
}
let rowIndex = Math.trunc(index / colCount);
let colIndex = index % colCount;
let actualRowIndex = selectionStartRowIndex + rowIndex;
let actualColIndex = selectionStartColIndex + colIndex;
// get data for setting a permission.
let resourceName = data[resourceNameRowNumber - 1][actualColIndex];
let resourceId = data[resourceIdRowNumber - 1][actualColIndex];
let targetRow = data[actualRowIndex];
let gmail = targetRow[gmailColNumber - 1];
let permissionLetter = targetRow[actualColIndex];
log('Processing ' + (rowIndex+1) + '/' + rowCount + ': [' + gmail + '][' + resourceName + '][' + permissionLetter + '] ...');
let role;
if ( permissionLetter === 'R' ){
role = 'reader';
}
else if ( permissionLetter === 'W' ){
role = 'writer';
}
let permissionId = Drive.Permissions.getIdForEmail(gmail).id;
let currentPermission;
try{
currentPermission = Drive.Permissions.get(resourceId, permissionId);
}
catch {}
// set permission
if ( role != null ){
if ( currentPermission != null ){
Drive.Permissions.remove(resourceId, permissionId);
}
Drive.Permissions.insert(
{
'role': role,
'type': 'user',
'value': gmail
},
resourceId,
{
'sendNotificationEmails': false,
'supportsAllDrives': true,
});
}
// delete permission
else {
if ( currentPermission != null ){
Drive.Permissions.remove(resourceId, permissionId);
}
}
}
catch (e){
log('Error: ' + e.message);
}
}
/**
* Finalizer
*/
function syncPermissionsFinalizer(isFinished) {
if ( isFinished ){
log('Done');
}
else {
log('Pausing for long processing time...');
}
}
/**
* Outputs a message to a cell for logging.
*/
function log( message ) {
console.log(message);
try{
if ( logRange == null ){
logRange = SpreadsheetApp.getActiveSheet().getRange(logRangeCell);
}
if( logRange != null ){
logRange.setValue(message); // For now, only the most recent line will be displayed.
SpreadsheetApp.flush();
}
}
catch(e){
console.log(e.message);
}
}