-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-example.gs
More file actions
98 lines (85 loc) · 2.21 KB
/
api-example.gs
File metadata and controls
98 lines (85 loc) · 2.21 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
/**
* @author Volodymyr Melnychuk <540991@i.ua>
* https://github.com/animan01
*/
/**
* Base method for getting info from Google SpreadSheet.
*
* @return {{ss: *, apiExample: *}}
*/
function prepareGsData(gsUrl) {
// Get all data from Google SpreadSheet.
let ss = SpreadsheetApp.openByUrl(gsUrl);
let apiExample = ss.getSheetByName("API-example");
return {
'apiExample': apiExample,
'ss': ss,
};
}
/**
* Base method for running Google app script.
*
* @return string
* Return Json with trade-in devices data.
*/
function doGet() {
// Public Google spreadsheets URL.
let gsUrl = 'https://docs.google.com/spreadsheets/d/15q8896q5FambSL0j9NVwpzRagoN9hKPWrBwO4ph4V30/edit';
// Prepering GS data.
let gsData = prepareGsData(gsUrl);
// Load data in the object.
let apiExampleData = getApiExampleData(gsData.apiExample, gsData.ss);
// Run generate Json responce.
return convertToJson(apiExampleData);
}
/**
* Convert object to Json string.
*
* @return string
* Return converted json.
*/
function convertToJson(data) {
// Encoding JSON.
let jsonEncode = JSON.stringify(data);
return ContentService.createTextOutput(jsonEncode).setMimeType(ContentService.MimeType.JSON);
}
/**
* Prepare products data.
*
* @return
* Object with product data.
*/
function getApiExampleData(data, ss) {
// Data without the first row.
let apiExampleData = data.getRange(2, 1, data.getLastRow() - 1, data.getLastColumn()).getValues();
/**
* Describe mapping:
* 0 ID
* 1 Product Name
* 2 Size
* 3 Unit
* 4 Price Unit
* 5 Price
*/
let products = {};
// Mapping values.
for (let i = 0, rows = apiExampleData.length; i < rows; i++) {
let productId = apiExampleData[i][0];
if (!products[productId]) {
let currentProduct = {
[productId]: {
"id": apiExampleData[i][0],
"product_name": apiExampleData[i][1],
"size": apiExampleData[i][2],
"unit": apiExampleData[i][3],
"priceUnit": apiExampleData[i][4],
"price": apiExampleData[i][5],
}
};
products = Object.assign(products, currentProduct);
Logger.log(currentProduct);
}
}
Logger.log(products);
return products;
}