Last active
April 9, 2019 06:21
-
-
Save enue/287ded5dc5fd533e45f2bdab7bc81be3 to your computer and use it in GitHub Desktop.
かんたんスプレッドシートapi
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
using System.Linq; | |
// https://developers.google.com/sheets/guides/concepts?hl=ja | |
namespace TSKT | |
{ | |
public class SpreadSheet | |
{ | |
public string[][] Cells { get; private set; } | |
public static IEnumerator LoadCoroutine(string apiKey, string sheetId, string range, System.Action<SpreadSheet> callback) | |
{ | |
var api = "https://sheets.googleapis.com/v4/spreadsheets/" + sheetId + "/values/" + range + "?key=" + apiKey; | |
using (var request = UnityWebRequest.Get(api)) | |
{ | |
yield return request.SendWebRequest(); | |
if (request.isHttpError) | |
{ | |
callback(null); | |
yield break; | |
} | |
if (request.isNetworkError) | |
{ | |
callback(null); | |
yield break; | |
} | |
var json = MiniJSON.Json.Deserialize(request.downloadHandler.text); | |
var rows = (List<object>)((Dictionary<string, object>)json)["values"]; | |
var cells = rows.Cast<List<object>>() | |
.Select(_ => _.Cast<string>().ToArray()) | |
.ToArray(); | |
var result = new SpreadSheet(); | |
result.Cells = cells; | |
callback(result); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment