Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Created July 20, 2021 06:19
Show Gist options
  • Save freddiefujiwara/85986774f114119230265ab47792dd7c to your computer and use it in GitHub Desktop.
Save freddiefujiwara/85986774f114119230265ab47792dd7c to your computer and use it in GitHub Desktop.
{
"timeZone": "Asia/Tokyo",
"dependencies": {
},
"webapp": {
"access": "ANYONE_ANONYMOUS",
"executeAs": "USER_DEPLOYING"
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
function doGet(e) {
Logger.log(e);
let json = "";
Logger.log(e.parameter.version);
if (e && e.parameter && "2" == e.parameter.version) {
json = getJSONv2();
} else {
json = getJSON();
}
let output = ContentService.createTextOutput();
if (typeof e.parameter.callback !== "undefined") {
output.setMimeType(ContentService.MimeType.JAVASCRIPT);
output.setContent(e.parameter.callback + "&&" + e.parameter.callback + "(" + json + ");");
} else if (typeof e.parameter.cron !== "undefined") {
cron();
output.setMimeType(ContentService.MimeType.TEXT);
output.setContent("OK");
} else {
output.setMimeType(ContentService.MimeType.JSON);
output.setContent(json);
}
return output;
}
function cron() {
const response = JSON.parse(UrlFetchApp.fetch("https://script.google.com/macros/s/AKfycbzvNpTeChTG7z2dPk_ARdRX8e9LeWQ7Bdy_qxegnurH0_uQnTI4/exec?version=2")
.getContentText());
const IFTTT = "http://a.ze.gs/google-home-speaker-wrapper/-h/192.168.1.22/-v/60/-s/%E6%9C%80%E6%96%B0%E3%81%AE%E5%B7%9D%E5%8F%A3%E5%B8%82%E3%81%AE%E6%96%B0%E5%9E%8B%E3%82%B3%E3%83%AD%E3%83%8A%E3%82%A6%E3%82%A3%E3%83%AB%E3%82%B9%E6%84%9F%E6%9F%93%E7%97%87%E3%81%AE";
// get data from spread sheet
const res = UrlFetchApp.fetch(IFTTT + encodeURIComponent(response.lastUpdate.replace(/\s/, " ")) + "%E6%99%82%E7%82%B9%E3%81%AE%E7%99%BA%E7%94%9F%E7%8A%B6%E6%B3%81%E3%82%92%E3%81%8A%E4%BC%9D%E3%81%88%E3%81%97%E3%81%BE%E3%81%99%E3%80%82" + encodeURIComponent("新規に" + response.main_summary.attr + "は" + response.inspections_summary.data.pop()["小計"] + "名増加、" +
"新規" + response.main_summary.children[0].attr + "は" + response.patients_summary.data.pop()["小計"] + "名増加") + "%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82%E3%81%9D%E3%81%AE%E7%B5%90%E6%9E%9C%E5%85%A8%E5%90%88%E8%A8%88%E3%81%A8%E3%81%97%E3%81%A6" + encodeURIComponent(response.main_summary.attr + "は" + response.main_summary.value + "名、" +
response.main_summary.children[0].attr + "は" + response.main_summary.children[0].value + "名、" +
response.main_summary.children[0].children[0].attr + "は" + response.main_summary.children[0].children[0].value + "名、" +
response.main_summary.children[0].children[1].attr + "は" + response.main_summary.children[0].children[1].value + "名、" +
response.main_summary.children[0].children[2].attr + "は" + response.main_summary.children[0].children[2].value + "名") + "%E3%81%A8%E3%81%AA%E3%82%8A%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82", {
'muteHttpExceptions': true,
'method': 'get'
});
Logger.log("Response:" + res);
}
function getJSON() {
const sheet = SpreadsheetApp.openById("1bCV6OVFJjOJ-D2TCXLv4bU0LOqgG2kWYUPL1FyWanHk").getSheetByName("v1");
const data = sheet.getDataRange().getValues();
const headers = data.shift();
let ret = [];
data.forEach((r, i) => {
const row = {};
headers.forEach((header, h) => {
if (String(header) === "URL") {
r[h] = "https:" + r[h];
}
row[String(header)] = r[h];
});
if (row.Text.match(/例目/)) {
ret.push(row);
}
});
return JSON.stringify(ret);
}
function getJSONv2() {
const sheet = SpreadsheetApp.openById("1bCV6OVFJjOJ-D2TCXLv4bU0LOqgG2kWYUPL1FyWanHk").getSheetByName("v2");
const data = sheet.getDataRange().getValues();
const headers = data.shift();
let json = "";
data.forEach((r, i) => {
if ("" !== json) return;
const row = {};
headers.forEach((header, h) => {
if ("" !== json) return;
if (String(header) === "url") {
try {
const res = UrlFetchApp.fetch(r[h], {
muteHttpExceptions: true
}).getContentText();
if (res.match(/JSON.parse/)) {
json = res.replace(/.+JSON.parse\('{"contacts/, '{"contacts').replace(/'\).*$/, "");
}
} catch (e) {}
}
});
});
return json;
}
function fillRawData() {
const sheet = SpreadsheetApp.openById("1bCV6OVFJjOJ-D2TCXLv4bU0LOqgG2kWYUPL1FyWanHk").getSheetByName("rawdata");
const response = JSON.parse(UrlFetchApp.fetch("https://script.google.com/macros/s/AKfycbzvNpTeChTG7z2dPk_ARdRX8e9LeWQ7Bdy_qxegnurH0_uQnTI4/exec?version=2")
.getContentText());
response.patients.data.forEach((record, i) => {
sheet.getRange(i + 2, 1).setValue(record["No"]);
sheet.getRange(i + 2, 2).setValue(record["リリース日"]);
sheet.getRange(i + 2, 3).setValue(record["年代"]);
sheet.getRange(i + 2, 4).setValue(record["性別"]);
sheet.getRange(i + 2, 5).setValue(record["居住地"]);
sheet.getRange(i + 2, 6).setValue(record["date"]);
sheet.getRange(i + 2, 7).setValue(1);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment