Skip to content

Instantly share code, notes, and snippets.

@enue
Last active April 9, 2019 06:21
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save enue/287ded5dc5fd533e45f2bdab7bc81be3 to your computer and use it in GitHub Desktop.
かんたんスプレッドシートapi
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