Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google spreadsheets で遊んでみた http://www.baldanders.info/spiegel/log2/000693.shtml
//********************************************************************************************
//note用 目次生成関数
// noteのRSSデータから Title, URL, Date Created 情報を抽出し(Google spreadsheets で抽出する)
// 目次用の HTML 要素を抽出する
//
// Written and revised by Spiegel,2014; http://www.baldanders.info/spiegel/profile/
// but, this document code is under CC0.
// https://creativecommons.org/publicdomain/zero/1.0/
//********************************************************************************************
function getNoteTocHTML(){
//RSSデータを取得し行列データを生成
var noteRss = createMatrix(arguments, true);
//日付要素(Date Created)をデータ型に置き換える
for(var row in noteRss) {
var cols = noteRss[row];
cols["Date Created"] = new Date(cols["Date Created"]);
}
//日付の昇順にソート
noteRss.sort(
function(a,b){
if( a["Date Created"] < b["Date Created"] ) return -1;
if( a["Date Created"] > b["Date Created"] ) return 1;
return 0;
}
);
//目次用のテーブルに変換
var noteToc = new NoteToc();
for(var row in noteRss) {
var cols = noteRss[row];
noteToc.push(cols["Date Created"], cols["Title"], cols["URL"]);
}
//HTMLに変換して出力
return noteToc.toStringHTML();
}
//********************************************************************************************
//目次情報クラス
//********************************************************************************************
function NoteToc() {
this.yearList = [];
//要素の追加
this.push = function(dt, title, url) {
if (dt instanceof Date) {
var year = this.getYearString(dt);
var month = this.getMonthString(dt);
var day = this.getDayString(dt);
for (var i in this.yearList) {
if (this.yearList[i].year == year) {
this.yearList[i].push(month, day, title, url);
return;
}
}
var yearInfo = new YearOfToc(year);
yearInfo.push(month, day, title, url);
this.yearList.push(yearInfo);
}
}
//HTML文字列へのシリアライズ
this.toStringHTML = function() {
var html = "<div class='note-toc'>";
for (var i in this.yearList) {
html += "<h2>Topics " + this.yearList[i].year + "</h2>\n";
html += this.yearList[i].toStringHTML();
}
html += "</div>";
return html;
}
//年を文字列として取得する
this.getYearString = function(dt) {
var str = "0000" + dt.getFullYear();
return str.substr(str.length - 4, 4);
}
//月を文字列として取得する
this.getMonthString = function(dt) {
var str = "00" + (dt.getMonth() + 1);
return str.substr(str.length - 2, 2);
}
//日を文字列として取得する
this.getDayString = function(dt) {
var str = "00" + dt.getDate();
return str.substr(str.length - 2, 2);
}
}
//********************************************************************************************
//目次情報「年」クラス
//********************************************************************************************
function YearOfToc(year) {
this.year = year;
this.monthList = [];
//要素の追加
this.push = function(month, day, title, url) {
for (var i in this.monthList) {
if (this.monthList[i].month == month) {
this.monthList[i].push(day, title, url);
return;
}
}
var monthInfo = new MonthOfToc(month);
monthInfo.push(day, title, url);
this.monthList.push(monthInfo);
}
//HTML文字列へのシリアライズ
this.toStringHTML = function() {
var html = "<dl>\n";
for (var i in this.monthList) {
html += "<dt>" + this.year + "" + this.monthList[i].month + "月</dt>\n";
html += this.monthList[i].toStringHTML();
}
html += "</dl>\n";
return html;
}
}
//********************************************************************************************
//目次情報「月」クラス
//********************************************************************************************
function MonthOfToc(month) {
this.month = month;
this.dayList = [];
//要素の追加
this.push = function(day, title, url) {
for (var i in this.dayList) {
if (this.dayList[i].day == day) {
this.dayList[i].push(title, url);
return;
}
}
var dayInfo = new DayOfToc(day);
dayInfo.push(title, url);
this.dayList.push(dayInfo);
}
//HTML文字列へのシリアライズ
this.toStringHTML = function() {
var html = "<dd><ul>\n";
var days = [];
for (var i in this.dayList) {
days.push(this.dayList[i].toStringHTML());
}
html += days.join("");
html += "</ul></dd>\n";
return html;
}
}
//********************************************************************************************
//目次情報「日」クラス
//********************************************************************************************
function DayOfToc(day) {
this.day = day;
this.titleList = [];
//要素の追加
this.push = function(title, url) {
for (var i in this.titleList) {
if (this.titleList[i].url == url) {
this.titleList[i].title = title;
return;
}
}
var titleInfo = new TitleOfToc(title, url);
this.titleList.push(titleInfo);
}
//HTML文字列へのシリアライズ
this.toStringHTML = function() {
var html = "<li>";
html += this.day + "日 : ";
var titles = [];
for (var i in this.titleList) {
titles.push(this.titleList[i].toStringHTML());
}
html += titles.join(" / ");
html += "</li>\n";
return html;
}
}
//********************************************************************************************
//目次情報「タイトル」クラス
//********************************************************************************************
function TitleOfToc(title, url) {
this.title = title;
this.url = url;
//目次情報「タイトル」クラス:HTML文字列へのシリアライズ
this.toStringHTML = function() {
return "<a href='" + this.url + "'>" + this.title + "</a>";
}
}
//********************************************************************************************
//行列テーブルの生成関数
// 複数の行配列の集合 [[0-0,0-1],[1-0,1-1]] から列配列の集合 [[0-0,1-0],[0-1,1-1]] を生成する
//********************************************************************************************
//複数の行配列の集合から行列テーブルを生成する
// ※第2引数が true なら列方向は連想配列にする(第1行目を hash 名とする),可能なら
function createMatrix(rowLists, hashed){
var matrix = [];
var hashNames = [];
if (rowLists.length > 0) {
//var rowLength = rowsLengthMax(rowLists); //複数の行配列の最大値
var rowLength = rowsLengthMin(rowLists); //複数の行配列の最小値
if (rowLength == 0) {
return matrix;
} else if (hashed && rowLength < 2) {
hashed = false; //強制的にフラグ解除
}
var row=0;
if (hashed) {
for(var col in rowLists) {
hashNames[col] = rowLists[col][0];
}
row++;
} else {
for(var col in rowLists) {
hashNames[col] = col;
}
}
for(; row < rowLength; row++) {
var cols = [];
var flag = true;
for(var col in rowLists) {
var rows = rowLists[col];
if (rows[row] == "") {
flag = false
}
if (flag == true) {
if (row < rows.length) {
cols[hashNames[col]] = rows[row];
} else {
cols[hashNames[col]] = null;
}
} else{
break;
}
}
if (flag == true) {
matrix.push(cols);
}
}
}
return matrix;
}
//複数の行配列の最小値を取得
function rowsLengthMin(rowLists){
var ret = 0;
if (rowLists.length > 0) {
for (var i in rowLists) {
var rows = rowLists[i];
if (ret == 0 || rows.length < ret) {
ret = rows.length;
}
}
}
return ret;
}
//複数の行配列の最大値を取得
function rowsLengthMax(rowLists){
var ret = 0;
if (rowLists.length > 0) {
for (var i in rowLists) {
var rows = rowLists[i];
if (rows.length > ret) {
ret = rows.length;
}
}
}
return ret;
}
//おまけ:回転行列ごっこ(笑)
function rotation_matrix(){
return [["cosθ","-sinθ"],
["sinθ","cosθ"]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.