-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-example.gs
More file actions
138 lines (118 loc) · 3.04 KB
/
Copy pathapi-example.gs
File metadata and controls
138 lines (118 loc) · 3.04 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
137
138
/**
* @author Volodymyr Melnychuk <540991@i.ua>
* https://github.com/MelnixDev
*/
/**
* Copy the ID from your spreadsheet URL:
* https://docs.google.com/spreadsheets/d/{SPREADSHEET_ID}/edit
*/
const SPREADSHEET_ID = '15q8896q5FambSL0j9NVwpzRagoN9hKPWrBwO4ph4V30';
const SHEET_NAME = 'API-example';
const DATA_COLUMN_COUNT = 6;
/**
* Base method for getting info from Google SpreadSheet.
*
* @param {string} [gsId=SPREADSHEET_ID]
* Google Spreadsheet ID.
* @return {{ss: *, apiExample: *}}
*/
function prepareGsData(gsId = SPREADSHEET_ID) {
// Get all data from Google Spreadsheet. The default value also makes direct
// execution from the Apps Script editor safe.
const ss = SpreadsheetApp.openById(gsId);
const apiExample = ss.getSheetByName(SHEET_NAME);
if (!apiExample) {
throw new Error(
`Sheet "${SHEET_NAME}" was not found. Check the SHEET_NAME constant.`,
);
}
return {
apiExample,
ss,
};
}
/**
* Base method for running Google app script.
*
* @return string
* Return Json with trade-in devices data.
*/
function doGet() {
// Public Google spreadsheet ID.
const gsId = SPREADSHEET_ID;
// Prepare Google Sheets data.
const gsData = prepareGsData(gsId);
// Load data in the object.
const apiExampleData = getApiExampleData(gsData.apiExample);
// Generate the JSON response.
return convertToJson(apiExampleData);
}
/**
* Run this function from the Apps Script editor to verify the setup.
*/
function testApi() {
console.log(doGet().getContent());
}
/**
* Convert object to Json string.
*
* @return string
* Return converted json.
*/
function convertToJson(data) {
const jsonEncode = JSON.stringify(data);
return ContentService
.createTextOutput(jsonEncode)
.setMimeType(ContentService.MimeType.JSON);
}
/**
* Prepare products data.
*
* @return
* Object with product data.
*/
function getApiExampleData(data) {
const lastRow = data.getLastRow();
const lastColumn = data.getLastColumn();
// A sheet with headers only is a valid empty API response.
if (lastRow < 2) {
return {};
}
if (lastColumn < DATA_COLUMN_COUNT) {
throw new Error(
`Sheet "${SHEET_NAME}" must contain at least ${DATA_COLUMN_COUNT} columns.`,
);
}
// Read data without the header row. Restrict the range to the six mapped
// columns so unrelated cells do not alter the response.
const apiExampleData = data
.getRange(2, 1, lastRow - 1, DATA_COLUMN_COUNT)
.getValues();
/**
* Describe mapping:
* 0 ID
* 1 Product Name
* 2 Size
* 3 Unit
* 4 Price Unit
* 5 Price
*/
const products = {};
// Mapping values.
for (const row of apiExampleData) {
const productId = row[0];
// Ignore empty rows and keep the first row for each product ID.
if (productId === '' || productId === null || products[productId]) {
continue;
}
products[productId] = {
id: productId,
product_name: row[1],
size: row[2],
unit: row[3],
priceUnit: row[4],
price: row[5],
};
}
return products;
}