Last active
December 16, 2017 08:16
-
-
Save iwathi/1447c2e315e17efc7010ce3f46a5cc5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Google SpreadSheetに追加された行数が何行になったら定期通知するか | |
var SENDCOUNTS = 12; | |
var lastValue = 0; | |
function main(){ | |
// チャートをラインへ送信する | |
getCCAPI("btc_jpy",1); | |
getCCAPI("xrp_jpy",1); | |
// チャートをラインへ送信しない | |
getCCAPI("xem_jpy",0); | |
getCCAPI("eth_jpy",0); | |
getCCAPI("bch_jpy",0); | |
getCCAPI("lsk_jpy",0); | |
} | |
// LINE Notifyのトークンを指定 | |
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | |
var lineToken = "【LINE NotifyのIDを入力】"; | |
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | |
function getCCAPI(type,sendFlag) { | |
//var defType ="btc_jpy"; | |
var feedURL = "https://coincheck.com/api/rate/"; | |
// Google SpreadSheetを指定 | |
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | |
var CCSSid = "【Google SpreadSheetのIDを入力】"; | |
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | |
var CCSS = SpreadsheetApp.openById(CCSSid); | |
Logger.log("Row : " + feedURL + type); | |
var jsonStr = parseJSON(feedURL + type); | |
lastValue = jsonStr["rate"]; | |
setCC2SS(CCSS,type,jsonStr["rate"],sendFlag); | |
} | |
// グラフを送信 | |
function sendGraphImage(type,ss){ | |
var sheet=ss.getSheetByName(type); | |
var charts = sheet.getCharts(); | |
var chartImage = charts[0].getBlob().getAs('image/png').setName("graph.png"); | |
var token = lineToken; | |
var payload = {'message' : type + ": " + lastValue +"円", 'imageFile' : chartImage}; | |
var options = | |
{ | |
"method" : "post", | |
"payload" : payload, | |
"headers" : {"Authorization" : "Bearer "+ token} | |
}; | |
UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options); | |
} | |
// Google SpreadSheetへ書き込み | |
function setCC2SS(SS,type,value,sendFlag){ | |
//GSSへ記事を保管 | |
/* シート1の準備*/ | |
var sheet=SS.getSheetByName(type); | |
var row1=sheet.getDataRange().getLastRow(); | |
var lastCount=sheet.getRange(row1,1).getValue(); | |
var date = new Date(); | |
var month = date.getMonth() + 1 ; | |
var formattedDate = Utilities.formatDate(new Date(), "Asia/Tokyo", "yyyy/MM/dd' 'HH:mm:ss"); | |
Logger.log("Row : " + value); | |
sheet.getRange(row1 + 1,1).setValue(lastCount + 1); | |
sheet.getRange(row1 + 1,2).setValue(value); | |
sheet.getRange(row1 + 1,3).setValue(formattedDate); | |
if(sendFlag == 1){ | |
if(((lastCount + 1) % SENDCOUNTS) == 0){ | |
sendGraphImage(type,SS); | |
} | |
} | |
} | |
function parseJSON(URL){ | |
// JSONを参照 | |
var response = UrlFetchApp.fetch(URL).getContentText(); | |
var jsonStr = JSON.parse(response); | |
//Logger.log(jsonStr); | |
return jsonStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment