-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
139 lines (124 loc) · 4.22 KB
/
app.ts
File metadata and controls
139 lines (124 loc) · 4.22 KB
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
/**
* @license
* Copyright 2024 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview The main entry point for User List Export application.
*/
import {AdManagerClient} from 'gam_apps_script/ad_manager_client';
import {AdManagerUserHandler} from './ad_manager_user_handler';
import {SpreadsheetHandler} from './spreadsheet_handler';
const TEMPLATE_SHEET = 'USER_LIST_TEMPLATE';
const NAMED_RANGE_NETWORK_CODE = 'NETWORK_CODE';
const NAMED_RANGE_API_VERSION = 'API_VERSION';
let spreadsheetHandler: SpreadsheetHandler;
function getSpreadsheetHandler(): SpreadsheetHandler {
if (!spreadsheetHandler) {
spreadsheetHandler = new SpreadsheetHandler(
SpreadsheetApp.getActiveSpreadsheet(),
SpreadsheetApp.getUi(),
);
}
return spreadsheetHandler;
}
function createAdManagerUserHandler(
networkCode: string, apiVersion: string): AdManagerUserHandler {
const adManagerClient = new AdManagerClient(
ScriptApp.getOAuthToken(),
'gam_user_list',
networkCode,
apiVersion,
);
const userService = adManagerClient.getService('UserService');
return new AdManagerUserHandler(userService);
}
/**
* Exports users from Ad Manager to a new sheet.
* @param spreadsheetHandler The spreadsheet handler.
* @param userHandler The Ad Manager user handler.
* @param dateString The current date as a string.
*/
export function exportUsers(
spreadsheetHandler: SpreadsheetHandler,
userHandler: AdManagerUserHandler,
dateString: string,
) {
// export users
const users = userHandler.getAllUsers();
const userList = users.map((user) => [
String(user.id),
String(user.name),
String(user.email),
String(user.roleName),
String(user.isActive),
]);
let values = [['ID', 'Name', 'Email', 'Role', 'Active']];
values = values.concat(userList);
// create new sheet with values
const newSheetName = `[${dateString}] Network code: ${userHandler.networkCode}`;
let attempt = 0;
let sheetCreated = false;
while (!sheetCreated) {
try {
const sheetName = attempt ? `${newSheetName} (${attempt})` : newSheetName;
spreadsheetHandler.createSheetFromTemplate(
sheetName,
TEMPLATE_SHEET,
values,
);
sheetCreated = true;
} catch {
attempt++;
}
}
}
export function onExportUsersSelected() {
const spreadsheetHandler = getSpreadsheetHandler();
const networkCode = spreadsheetHandler.getValueFromNamedRangeOrThrow(
NAMED_RANGE_NETWORK_CODE,
);
const didUserConfirm = spreadsheetHandler.showYesNoDialog(
`Export users`,
`Network code: ${networkCode}\n\n` +
'Please be aware that exported data will be visible to anyone with ' +
'access to this Google Sheets file regardless of whether or not ' +
'they have access to the data within Google Ad Manager. Do you wish' +
' to continue?',
);
if (!didUserConfirm) return;
const apiVersion = spreadsheetHandler.getValueFromNamedRangeOrThrow(
NAMED_RANGE_API_VERSION,
);
const userHandler = createAdManagerUserHandler(networkCode, apiVersion);
const dateString = Utilities.formatDate(new Date(), 'GMT+0', 'M/d/yy');
exportUsers(spreadsheetHandler, userHandler, dateString);
}
/**
* Creates a menu entry in the Google Sheets UI.
* @param spreadsheetHandler The spreadsheet handler.
*/
export function createMenu(spreadsheetHandler: SpreadsheetHandler) {
spreadsheetHandler.createMenu('Ad Manager', [
{itemName: 'Export users', functionName: 'onExportUsersSelected'},
]);
}
/**
* Main entry point for the app. Triggered when the spreadsheet is opened.
*/
export function onOpen() {
const spreadsheetHandler = getSpreadsheetHandler();
createMenu(spreadsheetHandler);
}
global.onOpen = onOpen;