Created
December 29, 2019 06:44
-
-
Save yoshiyoshifujii/aba9dfeddbb3cd22a284ea6addd5aab9 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
function openOauthAuth() { | |
const text = _getOauthAuthText(); | |
_openDialog(text); | |
} | |
function getOauthToken() { | |
const sheet = SpreadsheetApp.getActiveSheet(); | |
const code = sheet.getRange(1,2).getValue(); | |
const tokenJson = _getOauthToken(code); | |
sheet.getRange(1,4).setValue(tokenJson['access_token']); | |
} | |
function getInnerScan() { | |
const sheet = SpreadsheetApp.getActiveSheet(); | |
const accessToken = sheet.getRange(1,4).getValue(); | |
const lastRow = sheet.getLastRow(); | |
const lastRowDate = _getRowDate(sheet, lastRow); | |
const from = lastRowDate + "00"; | |
const json = _getInnerScanJson(accessToken, from); | |
const dateKeyDataValueMap = _convertDateKeyDataValueMap(json); | |
const tagColumnList = _getTagColumnList(sheet); | |
var row = lastRow; | |
const dateList = Object.keys(dateKeyDataValueMap) | |
.sort() | |
.filter(function(date) { | |
return date !== lastRowDate; | |
}) | |
.map(function(date) { | |
row++; | |
sheet.getRange(row, 1).setValue(date); | |
const dict = dateKeyDataValueMap[date]; | |
tagColumnList.forEach(function(tag) { | |
sheet.getRange(row, tag.col).setValue(dict[tag.value]); | |
}); | |
return date; | |
}); | |
if (dateList.length === 0) { | |
SpreadsheetApp.getUi().alert("No data"); | |
return; | |
} | |
} | |
function _getTagColumnList(sheet) { | |
// B2:I2までを読み込む | |
var startCol = 2; | |
const range = sheet.getRange(2, startCol, 1, 8); | |
return range.getValues()[0].map(function(v) { | |
return { | |
'col': startCol++, | |
'value': v | |
}; | |
}); | |
} | |
function _convertDateKeyDataValueMap(json) { | |
return json.data.reduce(function(acc, d) { | |
const dict = (d.date in acc) ? acc[d.date] : {}; | |
dict[d.tag] = d.keydata; | |
acc[d.date] = dict; | |
return acc; | |
}, {}); | |
} | |
function _getRowDate(sheet, row) { | |
return sheet.getRange(row, 1).getValue(); | |
} | |
function _openDialog(text) { | |
const html = HtmlService.createHtmlOutput(text); | |
SpreadsheetApp.getUi().showModalDialog(html, "OAuth auth"); | |
} | |
function _getOauthAuthText() { | |
const url = "https://www.healthplanet.jp/oauth/auth"; | |
const params = { | |
'method': "post", | |
'payload': { | |
'client_id': _getClientId(), | |
'redirect_uri': "https://www.healthplanet.jp/success.html", | |
'scope': "innerscan", | |
'response_type': "code" | |
} | |
}; | |
const res = UrlFetchApp.fetch(url, params); | |
const text = res.getContentText("shift_jis"); | |
return text; | |
} | |
function _getOauthToken(code) { | |
const url = "https://www.healthplanet.jp/oauth/token"; | |
const params = { | |
'method': "post", | |
'payload': { | |
'client_id': _getClientId(), | |
'client_secret': _getClientSecret(), | |
'redirect_uri': "https://localhost", | |
'code': code, | |
'grant_type': "authorization_code" | |
} | |
}; | |
return _fetchToJson(url, params); | |
} | |
function _getInnerScanJson(accessToken, from) { | |
const url = "https://www.healthplanet.jp/status/innerscan.json?" | |
const data = { | |
'access_token': accessToken, | |
'date': 1, | |
'tag': "6021,6022,6023,6024,6025,6026,6027,6028,6029" | |
}; | |
if (from) { | |
data['from'] = from; | |
} | |
const queryString = _generateQueryString(data); | |
const params = { | |
'muteHttpExceptions': true, | |
'method': "get" | |
}; | |
return _fetchToJson(url+queryString, params); | |
} | |
function _fetchToJson(url, params) { | |
const res = UrlFetchApp.fetch(url, params); | |
const text = res.getContentText("shift_jis"); | |
if (res.getResponseCode() === 200) { | |
try { | |
const json = JSON.parse(text); | |
} catch (e) { | |
console.error(text); | |
throw e; | |
} | |
return json; | |
} else { | |
console.error(res); | |
throw new Error(res); | |
} | |
} | |
function _getClientId() { | |
return PropertiesService.getScriptProperties().getProperty("ClientId"); | |
} | |
function _getClientSecret() { | |
return PropertiesService.getScriptProperties().getProperty("ClientSecret"); | |
} | |
function _generateQueryString(data) { | |
const params = []; | |
for (var d in data) | |
params.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d])); | |
return params.join('&'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment