This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Utilities.js
96 lines (84 loc) · 3.24 KB
/
Utilities.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
/***********************************************************************
Copyright 2018 Google Inc.
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
http://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.
Note that these code samples being shared are not official Google
products and are not formally supported.
************************************************************************/
// Constants for DV360 API scopes and Rest API URLs.
var API_URL_SDF =
'https://www.googleapis.com/doubleclickbidmanager/v1/sdf/download';
var userProperties = PropertiesService.getUserProperties();
var ui = SpreadsheetApp.getUi();
/**
* Calls the DV360 API function to download SDF files.
* @param {string} filterType The type of filter to apply.
* @param {string[]} filterIds Array of IDs to filter on.
* @param {string} fileTypes The type of SDF file to download.
* @param {string=} sdfVersion The SDF version to use (defaults to 3).
* @return {Object} The parsed JSON object of the API response.
* @private
*/
function getSdf_(filterType, filterIds, fileTypes, sdfVersion) {
var sdfV = sdfVersion || '3';
var requestParameters = {
"filterType": filterType,
"filterIds": filterIds,
"fileTypes": fileTypes,
"version": sdfV
};
var response = callApi_(API_URL_SDF, 'POST', requestParameters);
return JSON.parse(response);
}
/**
* Calls the DV360 API via an HTTP call, previously checking if the DBM API
* service is available and authorized, otherwise prompting the user to
* open the correct URL to authorize the API access.
* @param {string} url The URL of the REST API call to make.
* @param {string} methodType Value for the "method" option (GET/POST/...).
* @param {Object} requestBody The object containing the request parameters.
* @return {Object} The API call response.
* @private
*/
function callApi_(url, methodType, requestBody) {
var headers = {
'Content-Type': 'application/json',
'Accept' :'application/json',
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
};
var options = {
method: methodType,
headers : headers,
muteHttpExceptions: true
};
if (requestBody) {
options.payload = JSON.stringify(requestBody);
}
return UrlFetchApp.fetch(url, options);
}
/**
* Given a source sheet, returns an object with the column names as key
* and an array of the column values as value.
* @params {Spreadsheet}
* @return {Object} An Object mapped to the input sheet content.
*/
function populateObject_(sheet) {
var resultObject = {};
var allRange = sheet.getDataRange();
var allValues = allRange.getValues();
for (var col=0; col<allRange.getLastColumn(); col ++) {
var contentArray = [];
for (var row=1; row<allRange.getLastRow(); row ++) {
contentArray.push(allValues[row][col]);
}
resultObject[allValues[0][col]] = contentArray;
}
return resultObject;
}