-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport.gs
65 lines (56 loc) · 1.75 KB
/
Export.gs
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
function publishDataToGitHub() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Final");
const data = sheet.getDataRange().getValues();
const GITHUB_TOKEN = PropertiesService.getScriptProperties().getProperty('GITHUB_TOKEN');
const repo = 'ungaul/777-spots';
const path = 'assets/js/data.json';
const branch = 'main';
const jsonData = [];
for (let i = 1; i < data.length; i++) {
const columnB = data[i][1];
const url = data[i][2];
const lat = data[i][3];
const lng = data[i][4];
if (url && lat && lng) {
const item = {
url: url,
coordinates: { lat: parseFloat(lat), lng: parseFloat(lng) }
};
if (columnB && columnB.includes('https')) {
item.note = columnB;
}
jsonData.push(item);
}
}
const content = Utilities.base64Encode(JSON.stringify(jsonData, null, 2));
const apiUrl = `https://api.github.com/repos/${repo}/contents/${path}`;
const payload = {
message: "Mise à jour des données JSON",
content: content,
branch: branch
};
const headers = {
Authorization: `token ${GITHUB_TOKEN}`,
"Content-Type": "application/json"
};
try {
const response = UrlFetchApp.fetch(apiUrl, { method: 'GET', headers: headers });
const jsonResponse = JSON.parse(response.getContentText());
if (jsonResponse.sha) {
payload.sha = jsonResponse.sha;
}
} catch (e) {
Logger.log("Creating File.");
}
const options = {
method: 'PUT',
headers: headers,
payload: JSON.stringify(payload)
};
try {
const result = UrlFetchApp.fetch(apiUrl, options);
Logger.log('File Successully Publicated : ' + result.getContentText());
} catch (e) {
Logger.log('Error While Publicating : ' + e.message);
}
}