【自作!】spread sheet からタイトルとかメタとかを強制的に上書きしてくれるjs
<使い方>
①getmeta.jsを読み込む
②現在ページのファイル名が、spread sheetのtagに該当する名前であれば、
titleとdescriptionを強制的に上書きしてくれる!
<注意>
・多分htmlとかphpとかじゃないと動かない
・jqueryが読み込まれている事
・タイトルタグ、meta descriptionタグが存在している事
・getmeta.js のsheetId をworkSheetIDを変更すれば、恐らく使えます!
<ファイルとか諸々>
スプレットシート
https://docs.google.com/spreadsheets/d/182Rd-il5ClquO0_mKj64VDNzE2XJxonKEabpUhbLmd0/edit#gid=0
実装環境
http://hp.geo-code.jp/ranmu/html/index.html
JS
http://hp.geo-code.jp/ranmu/html/js/getmeta.js
/*******************************
jqueryをインクルードしてること
titleタグとdescriptionタグがソース上に書かれている事
*******************************/
$(window).on('load', function() {
var url = window.location.href;
var filename = url.match(".+/(.+?)\.[a-z]+([\?#;].*)?$")[1];
// URLからIDを読み取ってください
const sheetId = '182Rd-il5ClquO0_mKj64VDNzE2XJxonKEabpUhbLmd0';
// 1ページ目だったらod6で大丈夫なはず!
const workSheetId = 'od6';
$.ajax({
type: 'GET',
url: `https://spreadsheets.google.com/feeds/list/${sheetId}/${workSheetId}/public/basic?alt=json`,
dataType: 'json',
cache: false
}).done((data, textStatus, jqXHR) => {
formatSheetData(data);
let motod = [];
motod = formatSheetData(data.feed.entry);
console.log(motod);
for(let motonum =0; motonum < motod.length; motonum++) {
if(filename == motod[motonum]['tag']){
// spread sheet のtitleをタイトルタグに入れる
document.title = motod[motonum]['title'];
// spread sheet のdescription をメタデェスクリプションに入れる
$("meta[name=description]").attr("content",motod[motonum]['description']);
// spread sheetのpageno を活用し、Gナビの現在位置のところに印をつける
console.log(motod[motonum]['pageno']);
getPageNo = motod[motonum]['pageno'];
if(getPageNo !=9){
$('.pchead_gnav ul').children('li').eq(getPageNo).addClass('_active');
}
return ;
}else{
// spread sheetに該当が無き場合
document.title ="未設定です";
$("meta[name=description]").attr("content","未設定です");
}
}
}).fail((jqXHR, textStatus, errorThrown) => {
console.error('ajax function error!');
});
// console.log(outputData);
function formatSheetData(entry) {
const entryLength = entry.length;
let outputData = [];
for(let i =0; i < entryLength; i++) {
let categorizedString = entry[i].content.$t.split(', ');
let column = {};
for(let j = 0; j < categorizedString.length; j++) {
let keys = categorizedString[j].split(': ');
column[keys[0]] = keys[1];
}
// A1のセルだけは取得できないので手動対応
column['id'] = parseInt(entry[i].title.$t, 10);
outputData.push(column);
}
return outputData;
}
});