-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleSheets.lua
89 lines (76 loc) · 1.92 KB
/
GoogleSheets.lua
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
-- version: 0.1.2
local HTTP = game:GetService("HttpService")
local Letters = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):split("")
local URL_base = "https://docs.google.com/spreadsheets/d/%s/gviz/tq?tqx=out:json&gid="
type GST = {
URL: string,
IDs: {
Document: string,
Page: number
},
Information: {
status: string,
["table"]: {
cols: {
[number]: {
["id"]: string,
["label"]: string,
["type"]: string
}
},
rows: {
[number]: {
c: {[number]: {v: string}?}
}
},
}
},
-- Methods --
GetByCell: (Letter: string, Number: number) -> (string),
Update: () -> (),
}
-- Methods --
local GoogleSheets = {}
function GoogleSheets:GetByCell(Letter: string, Number: number)
local self = self :: GST
if not self.Information.table.rows[Number] then return nil end
local LetterIndex = table.find(Letters, Letter)
local Info = self.Information.table.rows[Number].c[LetterIndex]
return (if Info then Info.v else nil)
end
function GoogleSheets:Update()
local self = self :: GST
-- Get from web --
local success, data = pcall(HTTP.GetAsync, HTTP, self.URL)
if not success or not data then return warn("GoogleSheets error:", data) end
-- Get JSON --
local success, JSON = pcall(HTTP.JSONDecode, HTTP, data:sub(48, #data - 2))
if not success then warn("GoogleSheets error:", JSON) end
if JSON and JSON.status == "ok" then
self.Information = JSON
else
self.Information = {
status = "not load",
["table"] = {
cols = {},
rows = {},
}
}
end
end
-- Constructor --
function GoogleSheets.new(Document: string, Page: string|number)
local self = setmetatable({} :: GST, GoogleSheets)
self.IDs = {
["Document"] = Document,
["Page"] = Page
}
self.URL = URL_base:format(Document) .. Page
self:Update()
return self
end
-- Return class --
GoogleSheets.__index = GoogleSheets
return GoogleSheets :: {
new: (Document: string, Page: string|number) -> GST
}