1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace REverse\GSheets; |
4
|
|
|
|
5
|
|
|
use REverse\GSheets\Operation\Append; |
6
|
|
|
use REverse\GSheets\Operation\Clear; |
7
|
|
|
use REverse\GSheets\Operation\Delete; |
8
|
|
|
use REverse\GSheets\Operation\Get; |
9
|
|
|
use REverse\GSheets\Operation\Update; |
10
|
|
|
|
11
|
|
|
class SpreadSheets |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Client |
15
|
|
|
*/ |
16
|
|
|
private $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $spreadSheetId; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $range; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Client $client |
30
|
|
|
* @param string $spreadSheetId |
31
|
|
|
* @param string $range |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Client $client, $spreadSheetId, $range = "") |
34
|
|
|
{ |
35
|
|
|
$this->client = $client; |
36
|
|
|
$this->spreadSheetId = $spreadSheetId; |
37
|
|
|
$this->range = $range; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Value $value |
42
|
|
|
* @param $range |
43
|
|
|
* @param $optParams |
44
|
|
|
* |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
|
|
public function update(Value $value, $range, $optParams) |
48
|
|
|
{ |
49
|
|
|
$this->range = $range; |
50
|
|
|
|
51
|
|
|
(new Update($this))->execute($value, $this->range, $optParams); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Value $value |
58
|
|
|
* @param $range |
59
|
|
|
* @param $optParams |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
public function append(Value $value, $range, $optParams) |
64
|
|
|
{ |
65
|
|
|
$this->range = $range; |
66
|
|
|
|
67
|
|
|
(new Append($this))->execute($value, $this->range, $optParams); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $range |
74
|
|
|
* |
75
|
|
|
* @return self |
76
|
|
|
*/ |
77
|
|
|
public function clear($range) |
78
|
|
|
{ |
79
|
|
|
$this->range = $range; |
80
|
|
|
|
81
|
|
|
(new Clear($this))->execute($this->range); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Value $value |
88
|
|
|
* @param $sheet |
89
|
|
|
* @param $row |
90
|
|
|
* @param array $optParams |
91
|
|
|
* |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
|
|
public function updateRow(Value $value, $sheet, $row, $optParams = []) |
95
|
|
|
{ |
96
|
|
|
$this->range = $this->declareRowRange($sheet, $row, $row); |
97
|
|
|
|
98
|
|
|
$this->update($value, $this->range, $optParams); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $sheet |
105
|
|
|
* @param string $row |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function clearRow($sheet, $row) |
110
|
|
|
{ |
111
|
|
|
$this->range = $this->declareRowRange($sheet, $row, $row); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->clear($this->range); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function get($range) |
119
|
|
|
{ |
120
|
|
|
return (new Get($this))->execute($range); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function delete($dimension, $sheet, $startIndex, $endIndex) |
124
|
|
|
{ |
125
|
|
|
return (new Delete($this))->execute($dimension, $sheet, $startIndex, $endIndex); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return Client |
130
|
|
|
*/ |
131
|
|
|
public function getClient() |
132
|
|
|
{ |
133
|
|
|
return $this->client; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getSpreadSheetId() |
140
|
|
|
{ |
141
|
|
|
return $this->spreadSheetId; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $range |
146
|
|
|
* |
147
|
|
|
* @return self |
148
|
|
|
*/ |
149
|
|
|
public function setRange($range) |
150
|
|
|
{ |
151
|
|
|
$this->range = $range; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return last range used to write on spreadsheet |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getRange() |
162
|
|
|
{ |
163
|
|
|
return $this->range; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* This method apply A1 notation to declare row range on sheet chosen |
168
|
|
|
* |
169
|
|
|
* @link https://developers.google.com/sheets/api/guides/concepts |
170
|
|
|
* |
171
|
|
|
* @param string $sheet |
172
|
|
|
* @param int $rowStart |
173
|
|
|
* @param int $rowEnd |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function declareRowRange($sheet, $rowStart, $rowEnd) |
178
|
|
|
{ |
179
|
|
|
return $sheet.'!'.$rowStart.':'.$rowEnd; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|