1
1
function getDomainInfo ( ) {
2
- // Create a Customers list query request
3
- const customerDomain = "my_customer" ;
4
- const domainInfo = AdminDirectory . Customers . get ( customerDomain ) ;
2
+ try {
3
+ // Create a Customers list query request
4
+ const customerDomain = "my_customer" ;
5
+ const domainInfo = AdminDirectory . Customers . get ( customerDomain ) ;
5
6
6
- // Get the active spreadsheet
7
- const spreadsheet = SpreadsheetApp . getActiveSpreadsheet ( ) ;
7
+ // Get the active spreadsheet
8
+ const spreadsheet = SpreadsheetApp . getActiveSpreadsheet ( ) ;
8
9
9
- const sheets = spreadsheet . getSheets ( ) ;
10
- const lastSheetIndex = sheets . length ;
10
+ const sheets = spreadsheet . getSheets ( ) ;
11
+ const lastSheetIndex = sheets . length ;
11
12
12
- // Delete the "General Account Settings" sheet if it exists
13
- let existingSheet = spreadsheet . getSheetByName ( 'General Account Settings' ) ;
14
- if ( existingSheet ) {
15
- spreadsheet . deleteSheet ( existingSheet ) ;
16
- }
13
+ // Delete the "General Account Settings" sheet if it exists
14
+ let existingSheet = spreadsheet . getSheetByName ( 'General Account Settings' ) ;
15
+ if ( existingSheet ) {
16
+ spreadsheet . deleteSheet ( existingSheet ) ;
17
+ }
18
+
19
+ // Create the "General Account Settings" sheet at the last index
20
+ const generalSheet = spreadsheet . insertSheet ( 'General Account Settings' , lastSheetIndex ) ;
21
+
22
+ // Set up the sheet with headers, formatting, and column sizes
23
+ 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' ] ] ) ;
24
+ generalSheet . getRange ( 'A1:M1' ) . setFontWeight ( 'bold' ) . setBackground ( '#fc3165' ) . setFontColor ( '#ffffff' ) ;
25
+ generalSheet . autoResizeColumns ( 1 , 13 ) ;
26
+ generalSheet . setColumnWidth ( 1 , 150 ) ;
27
+ generalSheet . setColumnWidth ( 2 , 186 ) ;
28
+ generalSheet . setColumnWidth ( 6 , 150 ) ;
29
+ generalSheet . setColumnWidth ( 7 , 186 ) ;
30
+ generalSheet . getRange ( '2:2' ) . setBorder ( true , true , true , true , true , true , '#000000' , SpreadsheetApp . BorderStyle . SOLID ) ;
17
31
18
- // Create the "General Account Settings" sheet at the last index
19
- const generalSheet = spreadsheet . insertSheet ( 'General Account Settings' , lastSheetIndex ) ;
20
-
21
- // Set up the sheet with headers, formatting, and column sizes
22
- 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' ] ] ) ;
23
- generalSheet . getRange ( 'A1:M1' ) . setFontWeight ( 'bold' ) . setBackground ( '#fc3165' ) . setFontColor ( '#ffffff' ) ;
24
- generalSheet . autoResizeColumns ( 1 , 13 ) ;
25
- generalSheet . setColumnWidth ( 1 , 150 ) ;
26
- generalSheet . setColumnWidth ( 2 , 186 ) ;
27
- generalSheet . setColumnWidth ( 6 , 150 ) ;
28
- generalSheet . setColumnWidth ( 7 , 186 ) ;
29
- generalSheet . getRange ( '2:2' ) . setBorder ( true , true , true , true , true , true , '#000000' , SpreadsheetApp . BorderStyle . SOLID ) ;
30
-
31
- // Append a new row with customer data
32
- generalSheet . appendRow ( [
33
- domainInfo . id ,
34
- domainInfo . customerDomain ,
35
- domainInfo . postalAddress . organizationName ,
36
- domainInfo . language ,
37
- domainInfo . postalAddress . contactName ,
38
- domainInfo . postalAddress . addressLine1 ,
39
- domainInfo . postalAddress . addressLine2 ,
40
- domainInfo . postalAddress . postalCode ,
41
- domainInfo . postalAddress . countryCode ,
42
- domainInfo . postalAddress . region ,
43
- domainInfo . postalAddress . locality ,
44
- domainInfo . phoneNumber ,
45
- domainInfo . alternateEmail ,
46
- ] ) ;
47
-
48
- // Delete cells N-Z and rows 3-1000
49
- generalSheet . deleteColumns ( 14 , 13 ) ;
50
- generalSheet . deleteRows ( 3 , 998 ) ;
51
- generalSheet . autoResizeColumns ( 1 , generalSheet . getLastColumn ( ) ) ;
52
- }
32
+ // Append a new row with customer data
33
+ generalSheet . appendRow ( [
34
+ domainInfo . id ,
35
+ domainInfo . customerDomain ,
36
+ domainInfo . postalAddress . organizationName ,
37
+ domainInfo . language ,
38
+ domainInfo . postalAddress . contactName ,
39
+ domainInfo . postalAddress . addressLine1 ,
40
+ domainInfo . postalAddress . addressLine2 ,
41
+ domainInfo . postalAddress . postalCode ,
42
+ domainInfo . postalAddress . countryCode ,
43
+ domainInfo . postalAddress . region ,
44
+ domainInfo . postalAddress . locality ,
45
+ domainInfo . phoneNumber ,
46
+ domainInfo . alternateEmail ,
47
+ ] ) ;
48
+
49
+ // Delete cells N-Z and rows 3-1000
50
+ generalSheet . deleteColumns ( 14 , 13 ) ;
51
+ generalSheet . deleteRows ( 3 , 998 ) ;
52
+ generalSheet . autoResizeColumns ( 1 , generalSheet . getLastColumn ( ) ) ;
53
+
54
+ } catch ( e ) {
55
+ // Check if the error is due to insufficient permissions
56
+ if ( e . message . indexOf ( "Not Authorized to access this resource/api" ) > - 1 ) {
57
+ // Display a modal dialog box for the error message
58
+ const ui = SpreadsheetApp . getUi ( ) ;
59
+ ui . alert (
60
+ 'Insufficient Permissions' ,
61
+ 'You need Super Admin privileges to use this feature.' ,
62
+ ui . ButtonSet . OK
63
+ ) ;
64
+ // Log the detailed error for debugging
65
+ Logger . log ( e ) ;
66
+ } else {
67
+ // For other errors, re-throw the exception
68
+ throw e ;
69
+ }
70
+ }
71
+ }
0 commit comments