This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathappsScript.js
96 lines (92 loc) · 3.03 KB
/
appsScript.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
// make POST request to https://api.github/com/repos/deriv-pocs/custom-terminology/dispatches
// with the following -H "Authorization: token your-token-here"
// -H "Accept: application/vnd.github+json" \
// and the following -d '{"event_type": "translation_updated"}'
function sendCustomDictionary() {
// App Script read each sheet and combine them into one file
// map [English, Portugese, French, Spanish, Indonesian]
// to [ en, pt, fr, es, id ]
// https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html
const languageCodes = {
English: "en",
Portugese: "pt",
French: "fr",
Spanish: "es",
Indonesian: "id",
Vietnamese: "vi",
Italian: "it",
Russian: "ru",
};
// get first row of first sheet
const SpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
const allSheets = SpreadSheet.getSheets();
const sheet1 = allSheets[0];
const languageNamesRow = sheet1.getDataRange().getValues()[0];
const languageCodesRow = languageNamesRow.map((languageName) => {
return languageCodes[languageName];
});
const combineSheets = (sheets) => {
const data = [];
sheets.forEach((sheet) => {
const sheetData = sheet.getDataRange().getValues();
// delete the first row
sheetData.shift();
// for each cell in the sheet enclose it in quotes
sheetData.forEach((row) => {
if (row.length < languageCodesRow.length) {
const emptyCells = languageCodesRow.length - row.length;
for (let i = 0; i < emptyCells; i++) {
row.push(row[0]);
}
}
row.forEach((cell, index) => {
// row[index] = `"${cell}"`; if cell contains a comma
if (cell.includes(",")) {
row[index] = `"${cell}"`;
}
// remove white space or tab from cell and check if it and set to first cell of the row
if (cell.trim() === "") {
row[index] = row[0];
}
});
});
data.push(sheetData);
});
// flatten data
const flatData = data.reduce((acc, curr) => {
return acc.concat(curr);
}, []);
return flatData;
}
const prepareTerminologyCSV = () => {
const data = combineSheets(allSheets);
// make languageCodesRow the first row
data.unshift(languageCodesRow);
// convert [[],[]] data to csv
const csv = data.join("\n");
return csv;
}
const terminlogy = prepareTerminologyCSV();
const url =
"https://api.github.com/repos/binary-com/deriv-custom-terminology/dispatches";
// get github token from GITHUB_TOKEN script property
const token =
PropertiesService.getScriptProperties().getProperty("GITHUB_TOKEN");
const headers = {
Authorization: `token ${token}`,
Accept: "application/vnd.github+json",
"Content-Type": "application/json",
};
const body = {
event_type: "terminology_updated",
client_payload: {
terminology: terminlogy,
},
};
const options = {
method: "POST",
headers: headers,
payload: JSON.stringify(body),
};
UrlFetchApp.fetch(url, options);
}