This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelper.gs
67 lines (59 loc) · 1.73 KB
/
helper.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
66
67
function request(url, method, payload, json = true){
let header = {
'Content-Type': 'application/json'
};
let options = {
method: method,
headers: header
};
if (payload) options['payload'] = JSON.stringify(payload);
let response;
try {
response = UrlFetchApp.fetch(url, options);
let code = response.getResponseCode();
if (code != 200 && code != 204) throw { error: code };
console.log(`endpoint: ${url} http response: ${code}`);
if(code==204) return null;
if (json) return JSON.parse(fixCharset(response.getContentText("UTF-8")));
return response.getContentText("UTF-8");
} catch (error) {
console.error(error);
}
}
function getArrayFromSheets(sheetName, columns = 0) {
let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
let lastrow = ss.getLastRow();
var values;
try {
if (columns == 0) columns = ss.getLastColumn();
values = ss.getRange(1, 1, ss.getLastRow(), columns).getValues();
} catch (e) {
return null;
}
console.log(values);
return values;
}
Array.prototype.contains = function (needle) {
for (i in this) {
if (this[i] == needle) return true;
}
return false;
}
function fixCharset(string) {
//Corrige erros de decodificação de string para ç, ^, `, &, e '
return string
.replace(/'/g, "'")
.replace(/ç/g, "ç")
.replace(/&/g, "&")
.replace(/ /g, " ")
.replace(/ã/g, "ã")
.replace(/õ/g, "õ")
.replace(/á/g, "á")
.replace(/é/g, "é")
.replace(/í/g, "í")
.replace(/ó/g, "ó")
.replace(/ú/g, "ú")
.replace(/â/g, "â")
.replace(/ê/g, "ê")
.replace(/ô/g, "ô");
}