-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.js
690 lines (647 loc) · 25.4 KB
/
Code.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// Written by Andrew Stillman
// Published under GNU General Public License, version 3 (GPL-3.0)
// See restrictions at http://www.opensource.org/licenses/gpl-3.0.html
// Updated by Michael Back
// Github: https://github.com/myxxmikeyxx/Google-Chromebook-Admin-Script
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/chromeosdevices#ChromeOsDevice
var headers = ['Org Unit Path', 'Annotated Location', 'Annotated Asset ID', 'Serial Number', 'Notes', 'Annotated User', 'Recent Users', 'Status', 'OS Version', 'Last Sync', 'Mac Address', 'Ethernet Mac Address', 'etag', 'Platform Version', 'Device ID', 'Last Enrollment', 'Active Time', 'Model Firmware Version', 'Boot Mode', 'Support End Date'];
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Chrome Device Management')
.addItem('First Run', 'menuItem1')
.addSeparator()
.addSubMenu(ui.createMenu('Get Devices')
.addItem('Get Devices', 'menuItem2'))
.addSubMenu(ui.createMenu('Update Devices')
.addItem('Update Device Info', 'menuItem3'))
.addSubMenu(ui.createMenu('Restore from Backup')
.addItem('Restore from Backup', 'menuItem4'))
.addSeparator()
.addSeparator()
.addSubMenu(ui.createMenu('Extra')
.addItem('Remove All Sheets', 'menuItem5')
.addSeparator()
.addItem('Force Update', 'menuItem6')
.addSeparator()
.addItem('Remove and Add Data Val', 'menuItem7')
.addSeparator()
.addItem('Hide Sheets', 'menuItem8'))
.addSeparator()
.addToUi();
}
function menuItem1() {
firstRun();
createSheets();
var ok = Browser.msgBox('Do you want to clear the Device Info and Compare sheet? \\n If not click anything other than OK. \\n\\n This will not clear Useful Formulas content.', Browser.Buttons.OK_CANCEL);
if (ok == "ok") {
clearSheet('Device Info');
clearSheet('Compare');
}
setHeader('Device Info');
filterSheet('Device Info');
// Removed dataVal because it does not have enough rows and throws and error on first run
hideSheet('Compare');
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Device Info').activate();
}
function menuItem2() {
clearSheet('Device Info');
setHeader('Device Info');
listChromeDevices();
setWrap('Device Info');
setHeader('Device Info');
filterSheet('Device Info');
dataVal('Device Info');
// Copies to the backup sheet if the compare sheet is empty (good for first get devices backup)
if (isSheetEmpty(SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Compare'))) {
showSheet('Backup');
clearSheet('Backup');
copyToSheet('Device Info', 'Backup');
setWrap('Backup');
setHeader('Backup');
dataVal('Backup');
hideSheet('Backup');
}
//Now Copy the info to compare sheet
showSheet('Compare');
clearSheet('Compare');
copyToSheet('Device Info', 'Compare');
setWrap('Compare');
setHeader('Compare');
filterSheet('Compare');
dataVal('Compare');
hideSheet('Compare');
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Device Info').activate();
Browser.msgBox("Finished getting devices");
}
function menuItem3() {
updateDevices();
}
function menuItem4() {
restoreDevices();
}
function menuItem5() {
clearAllSheets();
}
function menuItem6() {
forceUpdateDevices();
}
function menuItem7() {
dataVal('Device Info');
}
function menuItem8() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Compare').hideSheet();
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Backup').hideSheet();
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Device Info').activate();
}
function firstRun() {
Browser.msgBox("User must have access to google admin and ability to manage chrome devices." +
"\\nDo not rename the sheets. The script uses the sheets names. \\n If they are changes the script will not work.");
Browser.msgBox("This script should only show 'ACTIVE' Devices.");
Browser.msgBox("Get Devices to update the list of devices before making any changes. It should only change devices that the information if different on the Compare sheet," +
"\\nMeaning if people are in admin changing items it should not change that information unless you are changing it on the sheet as well, then where is the most recent save/push will be kept.")
}
function onEdit(e) {
// Do nothing
}
function sanitizeMacInput(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
//Set MAC to regular text
sheet.getRange(1, letterToColumn('K'), sheet.getLastRow()).setNumberFormat("@");
}
function createSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetsCount = ss.getNumSheets();
var sheets = ss.getSheets();
// Makes the sheet or moves it to index 0
try {
ss.insertSheet('Device Info', 0);
} catch (e) {
ss.setActiveSheet(ss.getSheetByName('Device Info'));
ss.moveActiveSheet(0);
Logger.log("Device Info sheet already exist. Moved to index 0.");
Logger.log(e);
}
// Deletes all sheets that don't match "Device Info", "For Work", "Backup", "Useful Formulas"
for (var i = 0; i < sheetsCount; i++) {
var sheet = sheets[i];
var sheetName = sheet.getName();
Logger.log(sheetName);
if (sheetName != "Device Info") {
if (sheetName != "For Work") {
if (sheetName != "Backup") {
if (sheetName != "Useful Formulas") {
Logger.log("DELETE! " + sheet);
ss.deleteSheet(sheet);
}
}
}
} else {
Logger.log("No sheets to delete.");
}
}
// Makes the sheet or moves it to index 1
try {
ss.insertSheet('Compare', 1);
hideSheet('Compare');
} catch (e) {
ss.setActiveSheet(ss.getSheetByName('Compare'));
ss.moveActiveSheet(1);
hideSheet('Compare');
Logger.log("Compare sheet already exist.");
Logger.log(e);
}
// Makes the sheet or moves it to index 2
try {
//If sheet exist it will throw and error and not do the setvalue.
ss.insertSheet('For Work', 2);
ss.setActiveSheet(ss.getSheetByName('For Work'));
ss.getRange('A1').setValue("This sheet is for you to copy any data you want to work on. It will not be saved or pushed.")
// Put link to video showing a use.
ss.getRange('A2').setValue(" ");
} catch (e) {
// Just moves the sheet to the correct spot if it already exist
ss.setActiveSheet(ss.getSheetByName('For Work'));
ss.moveActiveSheet(2);
Logger.log("For Work sheet already exist.");
Logger.log(e);
}
// Makes the sheet or moves it to index 3
try {
ss.insertSheet('Backup', 3);
hideSheet('Backup');
} catch (e) {
ss.setActiveSheet(ss.getSheetByName('Backup'));
ss.moveActiveSheet(3);
hideSheet('Backup');
Logger.log("Backup sheet already exist.");
Logger.log(e);
}
// Makes the sheet or moves it to index 4
try {
ss.insertSheet('Useful Formulas', 4);
ss.getSheetByName('Useful Formulas');
ss.getRange('A1').setValue("\'=IF(ISNA(VLOOKUP(D39,'For Work'!A:K,6, false)),\"\", VLOOKUP(D39,'For Work'!A:K,6, false))");
ss.getRange('A2').setValue("\'=IF(ISNA(VLOOKUP(D3,'For Work'!A:K,2, false)),\"\", VLOOKUP(D3,'For Work'!A:K,2, false))");
//make an array of formulas that are useful and do a for loop to add them to the sheet
} catch (e) {
ss.setActiveSheet(ss.getSheetByName('Useful Formulas'));
ss.moveActiveSheet(4);
Logger.log("Useful Formulas sheet already exist.");
Logger.log(e);
}
ss.getSheetByName('Device Info').activate();
}
function clearAllSheets() {
// This deletes all sheets that are not "Sheet 1".
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetsCount = ss.getNumSheets();
var sheets = ss.getSheets();
try {
ss.insertSheet('Sheet 1', 0);
} catch (e) {
Logger.log("Sheet 1 already exist.");
Logger.log(e);
}
for (var i = 0; i < sheetsCount; i++) {
var sheet = sheets[i];
var sheetName = sheet.getName();
Logger.log(sheetName);
if (sheetName != "Sheet 1") {
Logger.log("DELETE! " + sheet);
ss.deleteSheet(sheet);
} else {
Logger.log("No sheets to delete.");
}
}
}
function copyToSheet(sheetName, copyTo) {
// This takes the sheet and what sheet you want to copy to.
// It will clear all filters and formats for both sheets
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(copyTo);
sheet.showSheet();
var copyFromSheet = ss.getSheetByName(sheetName);
// Removes all formatting and filters to keep the sheets the same
try {
sheet.clearFormats();
sheet.getFilter().remove();
copyFromSheet.clearFormats();
copyFromSheet.getFilter().remove();
} catch (e) {
}
// This gets an array of all the info from the sheet you want to copy from, then copies it to the copyTo sheet.
var rangeToCopy = copyFromSheet.getRange(1, 1, copyFromSheet.getMaxRows(), copyFromSheet.getMaxColumns());
if (sheet == null) {
// If sheet is null (doesn't exist), then create sheets
createSheets();
}
rangeToCopy.copyTo(sheet.getRange(1, 1));
setHeader('Compare');
setHeader('Device Info');
SpreadsheetApp.flush();
}
function showSheet(sheetName) {
// Show a hidden sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
try {
sheet.activate().showSheet();
} catch (e) {
Logger.log('Sheet already visible')
}
SpreadsheetApp.flush();
}
function hideSheet(sheetName) {
// Hides the given sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
sheet.hideSheet();
}
function clearSheet(sheetName) {
// Clears a sheet's content and formatting and filters
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var maxRow = sheet.getMaxRows();
var maxColumn = sheet.getMaxColumns();
try {
// Clears all content and Formatting
sheet.clearContents();
sheet.clearFormats();
sheet.getFilter().remove();
} catch (e) { }
// Make sure it has at least 100 rows
if (maxRow < 0) {
sheet.insertRows(maxRow, 3 - maxRow);
} else if (maxRow == 3) {
// Do nothing
} else {
//Leaves 3 rows
sheet.deleteRows(3, maxRow - 3);
}
// Makes sure it has all headers and one free space
if (maxColumn < letterToColumn('U')) {
sheet.insertColumns(maxColumn, letterToColumn('U') - maxColumn);
} else if (maxColumn == letterToColumn('U')) {
// Do nothing
} else {
// Range is out of bounds
sheet.deleteColumns(letterToColumn('U'), (maxColumn - letterToColumn('U')));
}
SpreadsheetApp.flush();
}
function setHeader(sheetName) {
// Formats the the headers of the given sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
sheet.clearFormats();
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
sheet.getRange(1, 1, 1, letterToColumn('F')).setFontWeight('bold').setHorizontalAlignment("center").setBackground('#74b9ff');
sheet.getRange(1, letterToColumn('F') + 1, 1, headers.length - letterToColumn('D')).setFontWeight('bold').setHorizontalAlignment("center").setBackground('grey');
sheet.setFrozenRows(1);
sheet.setFrozenColumns(letterToColumn('F'));
sheet.setColumnWidths(1, headers.length, 100);
sheet.setColumnWidths(1, 1, 360);
sheet.setColumnWidth(letterToColumn('B'), 145);
sheet.setColumnWidth(letterToColumn('C'), 130);
sheet.setColumnWidth(letterToColumn('D'), 110);
sheet.setColumnWidth(letterToColumn('T'), 120);
// Hides un-need columns
// Want to show the first 9 and hide the rest
sheet.hideColumns(9, headers.length - 9);
Logger.log("Set Headers for sheet: " + sheetName);
SpreadsheetApp.flush();
}
function setDetails(sheetName) {
// Freezes the columns of the given sheet to column F
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
sheet.setFrozenColumns(letterToColumn('F'));
SpreadsheetApp.flush();
}
function setWrap(sheetName) {
// Sets sheet text to no wrap for all the content.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var maxRow = sheet.getMaxRows();
var maxColumn = sheet.getMaxColumns();
sheet.getRange(1, 1, maxRow, maxColumn).setWrap(false);
Logger.log("Wrap Set to false for sheet: " + sheetName);
SpreadsheetApp.flush();
}
function filterSheet(sheetName) {
// Applies filter to given sheet for Column A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var maxRow = sheet.getMaxRows();
// Updates any current filters
try {
//Remove Filters
sheet.getFilter().remove();
} catch (e) {
Logger.log("No Filters to remove.");
}
//Hard coded the cell for creating the filter
sheet.getRange('A1:A' + maxRow).createFilter();
SpreadsheetApp.flush();
}
function dataVal(sheetName) {
// Does data validation for column A for all the locations. This makes it so you can not miss type a Org unit location
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
// Set the data validation for cell A2 to require a value from A2:A (lastrow - 1).
// The -1 is to not count the header row.
var cell = sheet.getRange('A2:A' + (sheet.getLastRow()));
var range = sheet.getRange('A2:A' + (sheet.getLastRow()));
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build();
cell.clearDataValidations();
cell.setDataValidation(rule);
}
function listChromeDevices() {
// Gets all Chrome devices and writes them to all needed sheets.
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Device Info');
var allDevices = [];
SpreadsheetApp.flush();
var response = AdminDirectory.Chromeosdevices.list('my_customer', { maxResults: 100, projection: "FULL" });
allDevices = allDevices.concat(response.chromeosdevices);
while (response.nextPageToken) {
response = AdminDirectory.Chromeosdevices.list('my_customer', { maxResults: 100, projection: "FULL", pageToken: response.nextPageToken });
allDevices = allDevices.concat(response.chromeosdevices);
}
Logger.log(allDevices);
setRowsData(sheet, allDevices);
SpreadsheetApp.flush();
} catch (err) {
Browser.msgBox("Error: " + err.message);
}
}
function updateDevices() {
var ok = Browser.msgBox('Are you sure? This will update the Organizational Unit, Annotated User, Annotated Location, and Notes for all devices listed in the sheet', Browser.Buttons.OK_CANCEL);
if (ok == "ok") {
Browser.msgBox("After closing this, please wait until another box pops up after this one, \n before changing anything or closing the tab.");
try {
var updatedCount = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Device Info');
var compareTo = ss.getSheetByName('Compare');
try {
var filter = sheet.getFilter().getColumnFilterCriteria(1);
} catch (e) {
Logger.log("Filter is blank: " + e);
}
try {
ss.getSheetByName('Device Info').getRange('A1').clearDataValidations();
ss.getSheetByName('Compare').getRange('A1').clearDataValidations();
ss.getSheetByName('Device Info').getFilter().remove();
ss.getSheetByName('Compare').getFilter().remove();
} catch (e) {
Logger.log("Filter already removed.");
}
try {
sanitizeMacInput('Device Info');
sanitizeMacInput('Compare');
sanitizeMacInput('Backup');
} catch (e) {
Logger.log("Backup Sheet is empty.");
}
compareTo.showSheet();
var updateFailed = false;
if (sheet.getLastRow() > 1) {
var data = getRowsData(sheet);
var compareData = getRowsData(compareTo);
for (var i = 0; i < data.length; i++) {
if (data[i].status === "ACTIVE") {
if (data[i].orgUnitPath != compareData[i].orgUnitPath ||
data[i].annotatedLocation != compareData[i].annotatedLocation ||
data[i].notes != compareData[i].notes ||
data[i].annotatedUser != compareData[i].annotatedUser ||
data[i].annotatedAssetId != compareData[i].annotatedAssetId
) {
data[i].recentUsers = null;
try {
if (data[i].annotatedAssetId == null) {
data[i].annotatedAssetId = '';
}
if (data[i].annotatedLocation == null) {
data[i].annotatedLocation = '';
}
if (data[i].annotatedUser == null) {
data[i].annotatedUser = '';
}
if (data[i].notes == null) {
data[i].notes = '';
}
AdminDirectory.Chromeosdevices.update(data[i], 'my_customer', data[i].deviceId);
//Logger.log("At: " + i + data[i], 'my_customer', data[i].deviceId);
updatedCount++;
// Browser.msgBox(updatedCount + "\\n" + data[i].deviceId);
} catch (e) {
Logger.log("AdminDirectory error at row: " + (i + 2) + "\nError Msg: " + e);
updateFailed = true;
continue;
}
}
}
}
}
if (updateFailed) {
Browser.msgBox("AdminDirectory update error. \\nCheck Logs.");
} else {
if (updatedCount == 1) {
Browser.msgBox(updatedCount + " Chrome device was updated in the inventory...");
Logger.log(updatedCount + " Chrome device was updated in the inventory...");
} else if (updatedCount < 1) {
Browser.msgBox(updatedCount + " Chrome devices were updated in the inventory...");
Logger.log(updatedCount + " Chrome devices were updated in the inventory...");
} else {
Browser.msgBox(updatedCount + " Chrome devices were updated in the inventory...");
Logger.log(updatedCount + " Chrome devices were updated in the inventory...");
}
if (updatedCount > 0) {
//Makes a Backup
copyToSheet('Compare', 'Backup');
//Updates compare so next update it saves time and tries to update only changed device info
copyToSheet('Device Info', 'Compare');
setHeader('Device Info');
setHeader('Compare');
//Applies back the filtered view the user
try {
sheet.getRange('A1:A' + sheet.getLastRow()).createFilter().setColumnFilterCriteria(1, filter);
} catch (e) {
Logger.log("Filter error: " + e);
}
filterSheet('Compare');
dataVal('Device Info');
dataVal('Compare');
dataVal('Backup');
// Hides both the compare and the backup sheet
menuItem8();
}
}
} catch (err) {
Browser.msgBox(err.message);
}
}
}
function getRowsData(sheet, range, columnHeadersRowIndex) {
// This gives an array of all the with headers as index [0...] and all values at index [0...] [0...]
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.getValues(), normalizeHeaders(headers));
}
function restoreDevices() {
var ok = Browser.msgBox('Are you sure you want to restore from backup? This will update the Organizational Unit, Annotated User, Annotated Location, and Notes for all devices back to before the last push.', Browser.Buttons.OK_CANCEL);
var updatedCount = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Backup');
if (!isSheetEmpty(sheet)) {
if (ok == "ok") {
Browser.msgBox("After closing this, please wait until another box pops up after this one, \n before changing anything or closing the tab.");
try {
try {
ss.getSheetByName('Backup').getRange('A1').clearDataValidations();
ss.getSheetByName('Backup').getFilter().remove();
} catch (e) {
Logger.log("Filter already removed on Backup Sheet.");
}
try {
sanitizeMacInput('Backup');
} catch (e) {
}
var updateFailed = false;
if (sheet.getLastRow() > 1) {
var data = getRowsData(sheet);
for (var i = 0; i < data.length; i++) {
if (data[i].status === "ACTIVE") {
//Sets Recent Users to null because it will cause problems if it's not an object
data[i].recentUsers = null;
try {
if (data[i].annotatedAssetId == null) {
data[i].annotatedAssetId = '';
}
if (data[i].annotatedLocation == null) {
data[i].annotatedLocation = '';
}
if (data[i].annotatedUser == null) {
data[i].annotatedUser = '';
}
if (data[i].notes == null) {
data[i].notes = '';
}
AdminDirectory.Chromeosdevices.update(data[i], 'my_customer', data[i].deviceId);
updatedCount++;
// Browser.msgBox(updatedCount + "\\n" + data[i].deviceId);
} catch (e) {
Logger.log("AdminDirectory error at row: " + (i + 2) + "\nError Msg: " + e);
updateFailed = true;
continue;
}
}
}
}
if (updateFailed) {
Browser.msgBox("AdminDirectory update error. \\nCheck Logs.");
} else {
Browser.msgBox(updatedCount + " Chrome devices were updated in the inventory... \\n This does not mean all were changed. Just the ones that did not match in admin were changed. The rest just pushed but did nothing.");
Logger.log(updatedCount + " Force restore backup, count will not be accurate...");
if (updatedCount >= 0) {
hideSheet('Backup');
ss.getSheetByName('Device Info').activate();
var ok = Browser.msgBox('Would you like to get new chrome device info now? This recommended before making any more changes.', Browser.Buttons.OK_CANCEL);
if (ok == "ok") {
menuItem2();
}
}
}
} catch (err) {
Browser.msgBox(err.message);
}
}
} else {
Logger.log("Backup Sheet is empty. " + isSheetEmpty(sheet));
Browser.msgBox("Backup Sheet is empty. \\n" + isSheetEmpty(sheet));
}
}
function isSheetEmpty(sheet) {
// simple check if a sheet is empty or not
return sheet.getDataRange().getValues().join("") === "";
}
function forceUpdateDevices() {
var ok = Browser.msgBox('Are you sure? This will update without regard for changes or backup sheet.', Browser.Buttons.OK_CANCEL);
if (ok == "ok") {
Browser.msgBox("After closing this, please wait until another box pops up after this one, \n before changing anything or closing the tab.");
try {
var updatedCount = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Device Info');
try {
var filter = sheet.getFilter().getColumnFilterCriteria(1);
} catch (e) {
Logger.log("Filter is blank: " + e);
}
try {
ss.getSheetByName('Device Info').getRange('A1').clearDataValidations();
ss.getSheetByName('Device Info').getFilter().remove();
} catch (e) {
Logger.log("Filter already removed.");
}
try {
sanitizeMacInput('Device Info');
} catch (e) {
}
var updateFailed = false;
if (sheet.getLastRow() > 1) {
var data = getRowsData(sheet);
for (var i = 0; i < data.length; i++) {
if (data[i].status === "ACTIVE") {
data[i].recentUsers = null;
try {
if (data[i].annotatedAssetId == null) {
data[i].annotatedAssetId = '';
}
if (data[i].annotatedLocation == null) {
data[i].annotatedLocation = '';
}
if (data[i].annotatedUser == null) {
data[i].annotatedUser = '';
}
if (data[i].notes == null) {
data[i].notes = '';
}
AdminDirectory.Chromeosdevices.update(data[i], 'my_customer', data[i].deviceId);
//Logger.log("At: " + i + data[i], 'my_customer', data[i].deviceId);
updatedCount++;
// Browser.msgBox(updatedCount + "\\n" + data[i].deviceId);
} catch (e) {
Logger.log("AdminDirectory error at row: " + (i + 2) + "\nError Msg: " + e);
updateFailed = true;
continue;
}
}
}
}
if (updateFailed) {
Browser.msgBox("AdminDirectory update error. \\nCheck Logs.");
} else {
Browser.msgBox(updatedCount + " Chrome devices were updated in the inventory... \\n This does not mean all were changed. Just the ones that did not match in admin were changed. They rest just pushed but did nothing.");
Logger.log(updatedCount + " Force Resotre backup, count will not be accurate...");
if (updatedCount >= 0) {
setHeader('Device Info');
//Applies back the filtered view the user
try {
sheet.getRange('A1:A' + sheet.getLastRow()).createFilter().setColumnFilterCriteria(1, filter);
} catch (e) {
Logger.log("Filter error: " + e);
}
dataVal('Device Info');
}
}
} catch (err) {
Browser.msgBox(err.message);
}
}
}