-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (78 loc) · 2.18 KB
/
index.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
97
98
const { google } = require("googleapis");
const GoogleSheet = function (cred) {
const AUTH = new google.auth.JWT(cred.client_email, null, cred.private_key, [
"https://www.googleapis.com/auth/spreadsheets",
]);
const gsapi = google.sheets({ version: "v4", auth: AUTH });
function listAPI() {
const header = async (conf) => {
let header = await gsapi.spreadsheets.values.get(conf);
let col_sheet = [];
let sheet_obj = header.data.values[0];
let len = sheet_obj.length;
for (var i = 0; i < len; i++) {
col_sheet[i] = (i + 1 + 9).toString(36).toUpperCase();
}
conf.range = `${conf.range.substring(0, conf.range.lastIndexOf("!"))}!${col_sheet[0]}2:${col_sheet.pop()}`;
let obj = {
abc: col_sheet,
item: header.data.values[0],
range: conf.range,
};
return obj;
};
const rows = async (conf, header) => {
let rows = await gsapi.spreadsheets.values.get(conf);
let data = rows.data.values;
let len = rows.data.values.length;
let beauty_row = [];
data.forEach((element, i) => {
var obj = {};
for (s in header) {
obj[header[s]] = element[s];
}
beauty_row.push(obj);
});
let obj = {
'rows': beauty_row,
'len': len
}
return obj;
};
return {
header,
rows,
};
}
this.header = function (spreadID, conf) {
try {
const { sheet } = opt
const conf = {
spreadsheetId: spreadID,
range: `${sheet}!A1:ZZ1`,
majorDimension: "ROWS",
};
let header = listAPI().header(conf);
return header;
} catch (err) {
console.log("ERROR", err);
}
};
this.allRows = async function (spreadID, opt) {
try {
const { sheet } = opt
const conf = {
spreadsheetId: spreadID,
range: `${sheet}!A1:ZZ1`,
majorDimension: "ROWS",
};
let header = await listAPI().header(conf);
conf.range = header.range;
let rows = await listAPI().rows(conf, header.item);
return rows;
} catch (err) {
console.log("ERROR", err);
}
};
};
module.exports = GoogleSheet;