-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetDomainInfo.gs
71 lines (63 loc) · 2.79 KB
/
getDomainInfo.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
function getDomainInfo() {
try {
// Create a Customers list query request
const customerDomain = "my_customer";
const domainInfo = AdminDirectory.Customers.get(customerDomain);
// Get the active spreadsheet
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheets = spreadsheet.getSheets();
const lastSheetIndex = sheets.length;
// Delete the "General Account Settings" sheet if it exists
let existingSheet = spreadsheet.getSheetByName('General Account Settings');
if (existingSheet) {
spreadsheet.deleteSheet(existingSheet);
}
// Create the "General Account Settings" sheet at the last index
const generalSheet = spreadsheet.insertSheet('General Account Settings', lastSheetIndex);
// Set up the sheet with headers, formatting, and column sizes
generalSheet.getRange('A1:M1').setValues([['Customer Workspace ID', 'Primary Domain', 'Organization Name', 'Language', 'Customer Contact', 'Address1', 'Address2', 'Postal Code', 'Country Code', 'Region', 'Locality', 'Phone number', 'Alternate Email']]);
generalSheet.getRange('A1:M1').setFontWeight('bold').setBackground('#fc3165').setFontColor('#ffffff');
generalSheet.autoResizeColumns(1, 13);
generalSheet.setColumnWidth(1, 150);
generalSheet.setColumnWidth(2, 186);
generalSheet.setColumnWidth(6, 150);
generalSheet.setColumnWidth(7, 186);
generalSheet.getRange('2:2').setBorder(true, true, true, true, true, true, '#000000', SpreadsheetApp.BorderStyle.SOLID);
// Append a new row with customer data
generalSheet.appendRow([
domainInfo.id,
domainInfo.customerDomain,
domainInfo.postalAddress.organizationName,
domainInfo.language,
domainInfo.postalAddress.contactName,
domainInfo.postalAddress.addressLine1,
domainInfo.postalAddress.addressLine2,
domainInfo.postalAddress.postalCode,
domainInfo.postalAddress.countryCode,
domainInfo.postalAddress.region,
domainInfo.postalAddress.locality,
domainInfo.phoneNumber,
domainInfo.alternateEmail,
]);
// Delete cells N-Z and rows 3-1000
generalSheet.deleteColumns(14, 13);
generalSheet.deleteRows(3, 998);
generalSheet.autoResizeColumns(1, generalSheet.getLastColumn());
} catch (e) {
// Check if the error is due to insufficient permissions
if (e.message.indexOf("Not Authorized to access this resource/api") > -1) {
// Display a modal dialog box for the error message
const ui = SpreadsheetApp.getUi();
ui.alert(
'Insufficient Permissions',
'You need Super Admin privileges to use this feature.',
ui.ButtonSet.OK
);
// Log the detailed error for debugging
Logger.log(e);
} else {
// For other errors, re-throw the exception
throw e;
}
}
}