-
Notifications
You must be signed in to change notification settings - Fork 0
/
property: 環境変数.gs
73 lines (61 loc) · 2.46 KB
/
property: 環境変数.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function __debug_flag() {
// __property("DEBUG").valueでも同じ値を取れるが、true->false/false->true両方で__debugが動いてしまうためやらない
let flag = (PropertiesService.getScriptProperties().getProperty("DEBUG") == "true")
? "false"
: "true";
__property("DEBUG", flag);
}
function __debug(message) {
// propertyからdebugを呼んでいるため、無限ループ回避のため平打ちする
if(PropertiesService.getScriptProperties().getProperty("DEBUG") == "true")
Logger.log(message);
}
/* 開発中に間違えて実行すると悲惨なので、コメントアウトしておく(1敗)
function initialize() {
__property("SSID", "spreadsheet_id", true)
__property("SSNAME", "spreadsheet_name", true);
__property("SSID_DEBUG", "spreadsheet_id", true)
__property("SSNAME_DEBUG", "spreadsheet_name", true);
__property("API_COUNTER", "0", true);
__property("DEBUG", "false", true);
}
// */
const GUARD_KEYS = [
"SSID",
"SSNAME"
];
// get/setに対応
function __property(key, value, force_flag=false) {
let message =
// [stop]keyもvalueもない
(key == undefined && value == undefined)
? "[Stop:property.gs/property] Required key(get). Optional value(set)"
// [get]keyだけの場合
: (key != undefined && value == undefined)
? PropertiesService.getScriptProperties().getProperty(key)
// [set]keyとvalueが存在する場合で、上書き禁止ではないもの
: (key != undefined && value != undefined && ! GUARD_KEYS.includes(key))
? true
// [set]keyとvalueが存在する場合で、上書き禁止の可能性があるものを強制的に書き換える場合
: (key != undefined && value != undefined && GUARD_KEYS.includes(key) && force_flag)
? true
// [stop]keyがなくてvalueがあるなど、想定外あるいは不正な処理の場合
: "[Skip] key is protected."
// set時のみ
result = "Failed";
if (message === true) {
PropertiesService.getScriptProperties().setProperty(key, value);
message = "[Complate:local_set] key:" +key + " / value:" + PropertiesService.getScriptProperties().getProperty(key);
result = "Success";
}
// get時も実行結果としては成功なので別途判定
if( message == PropertiesService.getScriptProperties().getProperty(key) ) {
result = "Success";
}
// 入力したプロパティが表示されればOK
__debug("[property.gs/property] " + message);
return {
result: result,
value: message
};
}