-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
62 lines (59 loc) · 1.94 KB
/
index.php
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
<?php
require 'config.php';
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
$range = $_GET['range'] ?? null;
if(!empty($range) && preg_match("/^[A-Z]{1,2}[0-9]{1,3}:[A-Z]{1,2}[0-9]{1,3}$/i", $range)) {
$sheetName .= '!'.$range;
}
$response = $service->spreadsheets_values->get($spreadSheetId, $range);
$values = $response->getValues();
if(!empty($values)){
print("<pre>".print_r($values,true)."</pre>");
}
break;
case 'POST':
$operation = $_POST['operation'] ?? 'append';
unset($_POST['operation']);
$params = [
'valueInputOption' => 'RAW'
];
if ($operation === 'append') {
$values = array();
foreach($_POST as $value){
$values[] = array($value);
}
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params['insertDataOption'] = 'INSERT_ROWS';
$result = $service->spreadsheets_values->append(
$spreadSheetId,
$sheetName,
$body,
$params,
);
} elseif ($operation === 'update'){
$range = $_POST['range'] ?? null;
unset($_POST['range']);
if(!empty($range) && preg_match("/^[A-Z]{1,2}[0-9]{1,3}:[A-Z]{1,2}[0-9]{1,3}$/i", $range)) {
$sheetName .= '!'.$range;
}
$values = array();
foreach($_POST as $value){
$values[] = array($value);
}
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$result = $service->spreadsheets_values->update(
$spreadSheetId,
$sheetName,
$body,
$params,
);
}
break;
default:
echo 'Method not Allowed.';
}