Skip to content

Commit c14f6bb

Browse files
SelectとDistinct機能追加
1 parent ac3ac3e commit c14f6bb

File tree

5 files changed

+81
-12
lines changed

5 files changed

+81
-12
lines changed

Release/GSSA.unitypackage

660 Bytes
Binary file not shown.

projects/Assets/GSSA/GoogleAppsScript/GSSA.gs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var Const = {
22
Method : "$mt$",
33
SheetName : "$sn$",
4+
Select: "$sl$",
5+
Distinct: "$di$",
46
Where : "$wh$",
57
ObjectId : "$oi$",
68
Target : "$tg$",
@@ -103,6 +105,8 @@ function FindFunc(sheet,res){
103105
var skip = res[Const.Skip];
104106
var limit = res[Const.Limit];
105107
var where = res[Const.Where];
108+
var select = res[Const.Select];
109+
var distinct = res[Const.Distinct];
106110

107111
var range = sheet.getDataRange();
108112
var sheetData = range.getValues();
@@ -165,8 +169,43 @@ function FindFunc(sheet,res){
165169
});
166170
}
167171
}
172+
173+
//重複を抜く
174+
if(distinct){
175+
var index = headers.indexOf(distinct);
176+
if(index >= 0){
177+
retData = retData.filter(function (x, i, self) {
178+
var distinctTarget = x.value[index];
179+
for(var findIndex = 0;findIndex < i;findIndex++){
180+
if(self[findIndex].value[index] == distinctTarget)return false;
181+
}
182+
return true;
183+
});
184+
}
185+
}
186+
168187
if(skip)retData = retData.slice(skip);
169188
if(limit)retData = retData.slice(0,limit);
189+
190+
//返却データを全データではなく、selectで指定されている項目だけにしてあげる
191+
if(select)
192+
{
193+
var selects = JSON.parse(select);
194+
195+
for(var headerIndex = 0;headerIndex < headers.length;){
196+
var key = headers[headerIndex];
197+
var findIndex = selects.indexOf(key);
198+
if(findIndex < 0){
199+
for(var d in retData)
200+
{
201+
retData[d].value.splice(headerIndex,1);
202+
}
203+
headers.splice(headerIndex, 1); // indexのところを削除
204+
continue;
205+
}
206+
headerIndex++;
207+
}
208+
}
170209

171210
return {values:retData,keys:headers};
172-
}
211+
}

projects/Assets/GSSA/Scripts/SpreadSheetConst.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public static class SpreadSheetConst
55
//予約語。使わないでね。
66
public const string Method = "$mt$";
77
public const string SheetName = "$sn$";
8+
public const string Select = "$sl$";
89
public const string Where = "$wh$";
910
public const string ObjectId = "$oi$";
1011
public const string Target = "$tg$";
@@ -14,5 +15,6 @@ public static class SpreadSheetConst
1415
public const string Limit = "$li$";
1516
public const string Skip = "$sk$";
1617
public const string IsDesc = "$id$";
18+
public const string Distinct = "$di$";
1719
}
1820
}

projects/Assets/GSSA/Scripts/SpreadSheetQuery.cs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public SpreadSheetQuery(string sheetName = null)
3434
private int? _skip;
3535
private string _orderKey;
3636
private bool _isDesc;
37+
private string[] selectStrings;
38+
private string _distinctKey;
3739

3840
/// <summary>
3941
/// 返却されるリストの先頭から指定した数を上限として取得
@@ -92,6 +94,30 @@ public SpreadSheetQuery ClearOrderBy()
9294
return this;
9395
}
9496

97+
/// <summary>
98+
/// 返却されるリストの項目を指定
99+
/// "id,name"のようにしてもよいし、"id","name"のように引数を増やしてもよい
100+
/// </summary>
101+
/// <param name="selects"></param>
102+
/// <returns></returns>
103+
public SpreadSheetQuery Select(params string[] selects)
104+
{
105+
selectStrings = selects.SelectMany(s => s.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)).Select(s => s.Trim()).ToArray();
106+
return this;
107+
}
108+
109+
/// <summary>
110+
/// 指定したキーの重複が無い状態で返却。
111+
/// 重複がある場合は先に発見された方が使用されるため、OrderByAscendingもしくはOrderByDescendingとの併用が望ましい
112+
/// </summary>
113+
/// <param name="key"></param>
114+
/// <returns></returns>
115+
public SpreadSheetQuery Distinct(string key = null)
116+
{
117+
_distinctKey = key;
118+
return this;
119+
}
120+
95121
/// <summary>
96122
/// 返却されるリストのフィルタ条件
97123
/// </summary>
@@ -156,7 +182,6 @@ public SpreadSheetQuery AndWhere(string target, string op, object value)
156182
return AndWhere(target, compareType, value);
157183
}
158184

159-
160185
/// <summary>
161186
/// AND検索条件
162187
/// </summary>
@@ -166,7 +191,7 @@ public SpreadSheetQuery AndWhere(string target, string op, object value)
166191
/// <returns></returns>
167192
public SpreadSheetQuery AndWhere(string target, CompareData.CompareType op, object value)
168193
{
169-
var compare = new CompareData{target = target, value = value, compare = op};
194+
var compare = new CompareData { target = target, value = value, compare = op };
170195
if (compare.compare != CompareData.CompareType.NONE) _compareList.Add(compare);
171196
return this;
172197
}
@@ -181,23 +206,26 @@ public SpreadSheetQuery AndWhere(string target, CompareData.CompareType op, obje
181206
public CustomYieldInstruction FindAsync(Action<List<SpreadSheetObject>> callback = null)
182207
{
183208
var complete = false;
184-
SpreadSheetSetting.Instance.Enqueue(()=>FindAsyncIterator(callback,b => complete = b));
209+
SpreadSheetSetting.Instance.Enqueue(() => FindAsyncIterator(callback, b => complete = b));
185210
return new WaitUntil(() => complete);
186211
}
187212

188-
private IEnumerator FindAsyncIterator(Action<List<SpreadSheetObject>> callback,Action<bool> endAction)
213+
private IEnumerator FindAsyncIterator(Action<List<SpreadSheetObject>> callback, Action<bool> endAction)
189214
{
190215
var form = new WWWForm();
191216
form.AddField(SpreadSheetConst.Method, "Find");
192217
form.AddField(SpreadSheetConst.SheetName, sheetName);
193218
var output = Json.Serialize(_compareList.Select(data => data.ToDictionary()).ToList());
194219
form.AddField(SpreadSheetConst.Where, output);
195-
if(_skip.HasValue)form.AddField(SpreadSheetConst.Skip, _skip.Value);
196-
if(_limit.HasValue)form.AddField(SpreadSheetConst.Limit, _limit.Value);
220+
if (_skip.HasValue) form.AddField(SpreadSheetConst.Skip, _skip.Value);
221+
if (_limit.HasValue) form.AddField(SpreadSheetConst.Limit, _limit.Value);
222+
if (selectStrings != null && selectStrings.Any()) form.AddField(SpreadSheetConst.Select, Json.Serialize(selectStrings));
223+
if (string.IsNullOrEmpty(_distinctKey) == false) form.AddField(SpreadSheetConst.Distinct, _distinctKey);
224+
197225
if (string.IsNullOrEmpty(_orderKey) == false)
198226
{
199-
form.AddField(SpreadSheetConst.OrderBy,_orderKey);
200-
form.AddField(SpreadSheetConst.IsDesc,_isDesc ? -1 : 1);
227+
form.AddField(SpreadSheetConst.OrderBy, _orderKey);
228+
form.AddField(SpreadSheetConst.IsDesc, _isDesc ? -1 : 1);
201229
}
202230

203231
using (var www = UnityWebRequest.Post(SpreadSheetSetting.Instance.SpreadSheetUrl, form))
@@ -244,11 +272,11 @@ private IEnumerator FindAsyncIterator(Action<List<SpreadSheetObject>> callback,A
244272
public CustomYieldInstruction CountAsync(Action<int> callback = null)
245273
{
246274
var complete = false;
247-
SpreadSheetSetting.Instance.Enqueue(()=>CountAsyncIterator(callback,b => complete = b));
275+
SpreadSheetSetting.Instance.Enqueue(() => CountAsyncIterator(callback, b => complete = b));
248276
return new WaitUntil(() => complete);
249277
}
250278

251-
private IEnumerator CountAsyncIterator(Action<int> callback,Action<bool> endAction)
279+
private IEnumerator CountAsyncIterator(Action<int> callback, Action<bool> endAction)
252280
{
253281
var form = new WWWForm();
254282
form.AddField(SpreadSheetConst.Method, "Count");
@@ -291,7 +319,7 @@ public enum CompareType
291319

292320
public Dictionary<string, object> ToDictionary()
293321
{
294-
return new Dictionary<string, object>{{SpreadSheetConst.Target, target},{SpreadSheetConst.Value, value},{SpreadSheetConst.Compare, compare}};
322+
return new Dictionary<string, object> { { SpreadSheetConst.Target, target }, { SpreadSheetConst.Value, value }, { SpreadSheetConst.Compare, compare } };
295323
}
296324
}
297325
}
16 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)