Googleスプレッドシートの内容をPHPで表示させるサンプルページ
$file_csv = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vRtoxh9wekGj1-PFIxE9ztpLlyI_hVsoHbT32oPpHOoE-3KiX7v4pSnbxusPTgyuzv8rUZR2fF3Fysd/pub?output=csv';
$file_handle = fopen($file_csv, "r");
$result = array();
if ($file_handle !== FALSE) {
$column_headers = fgetcsv($file_handle);
foreach ($column_headers as $header) {
$result[$header] = array();
}
while (($data = fgetcsv($file_handle)) !== FALSE) {
$i = 0;
foreach ($result as &$column) {
$column[] = $data[$i++];
}
}
fclose($file_handle);
}
$json = json_encode($result);
echo "const data = ${json};";
let html = ''
data['id'].forEach((v, i) => {
const target = /https:\/\/iwb\.jp\//.test(data['url'][i]) ?
'' : ' target="_blank"'
html += `
<li>
${data['date'][i]}
[${data['category'][i]}]
<a href="${data['url'][i]}"${target}>${data['title'][i]}</a>
</li>`
})
const news = document.getElementById('news')
news.innerHTML = html
元記事を表示する