I created this peace of PHP code to connect to googlew sheet and fill html file on my server:
<?php
// Vaš Google Sheets API ključ
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// URL vašeg Google Sheet-a
$sheet_url = 'https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxxx';
// Kreiranje URL-a za zahtev
$request_url = $sheet_url . '?key=' . $api_key;
// Povezivanje sa Google Sheets API-jem pomoću cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Parsiranje JSON odgovora
$data = json_decode($response);
// Popunjavanje podataka u HTML dokument
$html = file_get_contents('test.html');
foreach ($data as $datapoint) {
// Provjera da li se datum u koloni podudara sa trenutnim datumom
if ($datapoint['datum'] == date('d.m')) {
$html = str_replace('{{IME}}', $datapoint['Ime'], $html);
$html = str_replace('{{PREZIME}}', $datapoint['Prezime'], $html);
$html = str_replace('{{ULICA}}', $datapoint['Ulica'], $html);
$html = str_replace('{{BROJ}}', $datapoint['E-mail'], $html);
}
}
// Postavljanje HTTP zaglavlja za datoteku
header('Content-Disposition: attachment; filename="popunjeno.html"');
// Prikazivanje HTML dokumenta sa popunjenim podacima
echo $html;
?>
But I got this error: Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /home/xxxxxxx/public_html/sheet2html.php:26 Stack trace: #0 {main} thrown in /home/xxxxxxx/public_html/sheet2html.php on line 26
This script should populate HTML file from google sheet on with data from today only.
Maybe because it is late, maybe my knowledge is limited, I googled couple og hours, but mainly found this errors in JSON calls for example. Any help appreciated