Last active
May 26, 2018 14:25
-
-
Save tmokmss/46412fda002bc3ccad3495b8b74207b2 to your computer and use it in GitHub Desktop.
GASによる永続化方法
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 main() { | |
var count_before = loadCount(); | |
var count_today = getCountToday(); | |
var count_sum = count_before + count_today; | |
Logger.log(count_sum); | |
saveCount(count_sum); | |
} | |
function getCountToday() { | |
return Math.floor(Math.random() * 10); | |
} |
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
var properties = PropertiesService.getScriptProperties(); | |
//var properties = PropertiesService.getUserProperties(); | |
//var properties = PropertiesService.getDocumentProperties(); | |
function loadCount() { | |
var count_s = properties.getProperty("Count"); | |
if (count_s == null) return 0; | |
var count = parseInt(count_s, 10); | |
if (isNaN(count)) return 0; | |
return count; | |
} | |
function saveCount(count) { | |
properties.setProperty("Count", count); | |
} |
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
var sheetUrl = "スプレッドシートのURL"; // https://docs.google.com/spreadsheets/d/~~~/edit#gid=0 みたいなの | |
var ss = SpreadsheetApp.openByUrl(sheetUrl); | |
function fetchListColumn(sheetName, column) { | |
var sheet = ss.getSheetByName(sheetName); | |
var num = Math.ceil(sheet.getRange("A1").getValue()) | |
var query = Utilities.formatString("%s1:%s%d",column,column,num); | |
var raw = sheet.getRange(query).getValues(); | |
var list = Array(num); | |
for (var i=0; i<list.length; i++) { | |
list[i] = raw[i][0]; | |
} | |
return list; | |
} | |
function readSettings() { | |
var key = fetchListColumn("Settings", "B"); | |
var value = fetchListColumn("Settings", "C"); | |
var settings = {}; | |
for (var i=0; i<key.length; i++) { | |
settings[key[i]] = value[i]; | |
} | |
return settings; | |
} | |
function writeSettings(settings) { | |
var keys = Object.keys(settings); | |
var num = keys.length; | |
var values = Array(num); | |
for (var i=0; i<num;i++){ | |
values[i] = settings[keys[i]]; | |
} | |
var content = [keys, values]; | |
content = transpose(content); | |
var range = Utilities.formatString("B1:C%d",num); | |
var sheet = ss.getSheetByName("Settings"); | |
sheet.getRange(range).setValues(content); | |
} | |
function transpose(arr) { | |
var row = arr.length; | |
var col = arr[0].length; | |
var res = Array(col); | |
for (var i = 0; i < col; i++) { | |
res[i] = Array(row); | |
} | |
for (var i = 0; i < row; i++) { | |
for (var j=0; j<col; j++) { | |
res[j][i] = arr[i][j]; | |
} | |
} | |
return res; | |
} | |
var settings = readSettings(); | |
function loadCount() { | |
var value = settings["Count"]; | |
if (!value) return 0; | |
return parseInt(value, 10); | |
} | |
function saveCount(count) { | |
settings["Count"] = count; | |
writeSettings(settings); | |
} |
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
var jsonName = "sample.json"; | |
var settings = readJson(jsonName); | |
function getFile() { | |
var query = 'title = "' + jsonName + '"'; | |
var files = DriveApp.searchFiles(query); | |
if (files.hasNext()) { | |
return files.next(); | |
} | |
return DriveApp.createFile(name, ""); | |
} | |
function readJson() { | |
var file = getFile(jsonName); | |
var content = file.getBlob().getDataAsString(); | |
if (!content) return {}; | |
var json = JSON.parse(content); | |
return json; | |
} | |
function writeJson() { | |
var content = JSON.stringify(settings); | |
var file = getFile(jsonName); | |
file.setContent(content); | |
} | |
function loadCount() { | |
var count = settings["Count"]; | |
if (!count) return 0; | |
return count; | |
} | |
function saveCount(count) { | |
settings["Count"] = count; | |
writeJson(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment