Googleドキュメントのスクリプトで効率化!

ここ最近、Googleドキュメントの数値をスクリプトでHTML化してブログで使用する事が増えている。。(GoogleドキュメントのJSで確実、効率化!)

スクリプトであるサーバーサイドJavaScriptを使って、
先日立ち上げた「株式運用ダイアリー2012」のHTMLを作成する関数を作ってみた!

この七めんどい計算をスクリプトに任せる事で、その日の運用資金・日経平均・TOPIXの3つを記録すれば、その他の数値は計算で出せる!

そんなわけでちょっとスクリプトをお披露目~♪
あまりキレイに書いてないけど^^

[javascript]
function mk_html(n255_today, topix_today, funds_today, funds_zenjitsu_hi, n255_zenjitsu_hi, topix_zenjitsu_hi, nt_today, nt_yesterday ) {

//——————————————————————–
//★変数初期セット
//——————————————————————–
var html = {};
var num = {};

html.main = ”;
num.funds_first = 10000; //年初時の運用資金額
num.funds_today = funds_today;
num.n255_first = 8455.35; //年初時の日経平均
num.n255_today = n255_today;
num.topix_first = 728.61; //年初時のTOPIX
num.topix_today = topix_today;
num.nt_first = 11.60; //年初時のNT倍率(※割り算して四捨五入処理がめんどいから明示的に入れた^^)
num.nt_today = nt_today;
num.nt_yesterday = nt_yesterday;

//——————————————————————–
//★数字の四捨五入処理
//——————————————————————–
//————————————————
//◆運用成績
//————————————————
//年初比
num.funds_nensho_hi = Math.round(((num.funds_today – num.funds_first) / + num.funds_today) * 10000)
num.funds_nensho_hi = num.funds_nensho_hi / 100;
//前日比
num.funds_zenjitsu_hi = Math.round((funds_zenjitsu_hi) * 10000);
num.funds_zenjitsu_hi = num.funds_zenjitsu_hi / 100;

//————————————————
//◆日経平均
//————————————————
//年初比
num.n255_nensho_hi = Math.round(((num.n255_today – num.n255_first) / + num.n255_today) * 10000)
num.n255_nensho_hi = num.n255_nensho_hi / 100;
//前日比
num.n255_zenjitsu_hi = Math.round((n255_zenjitsu_hi) * 10000);
num.n255_zenjitsu_hi = num.n255_zenjitsu_hi / 100;

//————————————————
//◆TOPIX
//————————————————
//年初比
num.topix_nensho_hi = Math.round(((num.topix_today – num.topix_first) / + num.topix_today) * 10000)
num.topix_nensho_hi = num.topix_nensho_hi / 100;
//前日比
num.topix_zenjitsu_hi = Math.round((topix_zenjitsu_hi) * 10000);
num.topix_zenjitsu_hi = num.topix_zenjitsu_hi / 100;

//————————————————
//◆NT倍率
//————————————————
//年初比
num.nt_nensho_hi = Math.round((num.nt_today – num.nt_first) * 100);
num.nt_nensho_hi = num.nt_nensho_hi / 100;
//前日比
num.nt_zenjitsu_hi = Math.round((num.nt_today – num.nt_yesterday) * 100);
num.nt_zenjitsu_hi = num.nt_zenjitsu_hi / 100;
//本日のNT倍率
num.nt_today = Math.round(num.nt_today * 100);
num.nt_today = num.nt_today / 100;

//————————————————
//◆運用成績(基準比較)
//————————————————
//日経平均基準(年初比)
num.n255_nensho_hi_kijyun = Math.round((num.funds_nensho_hi – num.n255_nensho_hi) * 100);
num.n255_nensho_hi_kijyun = num.n255_nensho_hi_kijyun / 100;
//TOPIX基準(年初比)
num.topix_nensho_hi_kijyun = Math.round((num.funds_nensho_hi – num.topix_nensho_hi) * 100);
num.topix_nensho_hi_kijyun = num.topix_nensho_hi_kijyun / 100;
//運用成績の日経平均基準(前日比)
num.n255_zenjitsu_hi_kijyun = Math.round((num.funds_zenjitsu_hi – num.n255_zenjitsu_hi) * 100);
num.n255_zenjitsu_hi_kijyun = num.n255_zenjitsu_hi_kijyun / 100;
//運用成績のTOPIX基準(前日比)
num.topix_zenjitsu_hi_kijyun = Math.round((num.funds_zenjitsu_hi – num.topix_zenjitsu_hi) * 100);
num.topix_zenjitsu_hi_kijyun = num.topix_zenjitsu_hi_kijyun / 100;

//——————————————————————–
//★プラスマイナス記号付け
//——————————————————————–
//運用成績
num.funds_nensho_hi = num_plus_minus(num.funds_nensho_hi); //年初比
num.funds_zenjitsu_hi = num_plus_minus(num.funds_zenjitsu_hi); //前日比
//日経平均
num.n255_nensho_hi = num_plus_minus(num.n255_nensho_hi); //年初比
num.n255_zenjitsu_hi = num_plus_minus(num.n255_zenjitsu_hi); //前日比
//TOPIX
num.topix_nensho_hi = num_plus_minus(num.topix_nensho_hi); //年初比
num.topix_zenjitsu_hi = num_plus_minus(num.topix_zenjitsu_hi); //前日比
//NT倍率
num.nt_nensho_hi = num_plus_minus(num.nt_nensho_hi); //年初比
num.nt_zenjitsu_hi = num_plus_minus(num.nt_zenjitsu_hi); //前日比
//運用成績の日経平均基準
num.n255_nensho_hi_kijyun = num_plus_minus(num.n255_nensho_hi_kijyun); //年初比
num.n255_zenjitsu_hi_kijyun = num_plus_minus(num.n255_zenjitsu_hi_kijyun); //前日比
//運用成績のTOPIX基準
num.topix_nensho_hi_kijyun = num_plus_minus(num.topix_nensho_hi_kijyun); //年初比
num.topix_zenjitsu_hi_kijyun = num_plus_minus(num.topix_zenjitsu_hi_kijyun); //前日比

/*
//デバッグ用
html.main += num.funds_nensho_hi + “\r\n”;
html.main += num.funds_zenjitsu_hi + “\r\n”;
html.main += num.n255_nensho_hi + “\r\n”;
html.main += num.n255_zenjitsu_hi + “\r\n”;
html.main += num.topix_nensho_hi + “\r\n”;
html.main += num.topix_zenjitsu_hi + “\r\n”;
html.main += num.nt_nensho_hi + “\r\n”;
html.main += num.nt_zenjitsu_hi + “\r\n”;
html.main += num.n255_nensho_hi_kijyun + “\r\n”;
html.main += num.topix_nensho_hi_kijyun + “\r\n”;
html.main += num.n255_zenjitsu_hi_kijyun + “\r\n”;
html.main += num.topix_zenjitsu_hi_kijyun + “\r\n”;
*/

html.main += ‘★本日の結果’ + “\r\n”;
html.main += ” + “\r\n”;
html.main += ‘ ◆運用成績’ + “\r\n”;
html.main += ‘  年初比:‘ + num.funds_nensho_hi + ‘% (日経平均基準:‘ + num.n255_nensho_hi_kijyun + ‘% / TOPIX基準:‘ + num.topix_nensho_hi_kijyun + ‘%)’ + “\r\n”;
html.main += ‘  前日比:‘ + num.funds_zenjitsu_hi + ‘% (日経平均基準:‘ + num.n255_zenjitsu_hi_kijyun + ‘% / TOPIX基準:‘ + num.topix_zenjitsu_hi_kijyun +’%)’ + “\r\n”;
html.main += ” + “\r\n”;
html.main += ‘ ◆日経平均’ + “\r\n”;
html.main += ‘  ‘+ num.n255_today + ‘‘ + “\r\n”;
html.main += ‘   年初比:‘ + num.n255_nensho_hi + ‘%‘ + “\r\n”;
html.main += ‘   前日比:‘ + num.n255_zenjitsu_hi + ‘%‘ + “\r\n”;
html.main += ” + “\r\n”;
html.main += ‘ ◆TOPIX’ + “\r\n”;
html.main += ‘  ‘+ num.topix_today + ‘‘ + “\r\n”;
html.main += ‘   年初比:‘ + num.topix_nensho_hi + ‘%‘ + “\r\n”;
html.main += ‘   前日比:‘ + num.topix_zenjitsu_hi + ‘%‘ + “\r\n”;
html.main += ” + “\r\n”;
html.main += ‘ ◆NT倍率’ + “\r\n”;
html.main += ‘  ‘ + num.nt_today + ‘倍(年初比:‘ + num.nt_nensho_hi +’ / 前日比:‘ + num.nt_zenjitsu_hi +’)’ + “\r\n”;

return html.main;

}

function num_plus_minus(num){

var ret;

if(num < 0){ //ret = '-' + num; ret = num; }else if(num == 0){ ret = '±' + num; }else if(num > 0){
ret = ‘+’ + num;
}else{
//
}

return ret;

}
[/javascript]

スポンサードリンク

コメント

タイトルとURLをコピーしました