Example #1
0
        public static async Task <CsvData> FetchAsync(string spreadSheetId, string title, string apiKey)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("https://sheets.googleapis.com/v4/spreadsheets/");
            sb.Append(spreadSheetId);
            sb.Append("/values/");
            sb.Append(title);
            sb.Append("?key=");
            sb.Append(apiKey);
            sb.Append("&valueRenderOption=UNFORMATTED_VALUE");

            string url = sb.ToString();

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                request.SendWebRequest();

                while (!request.isDone)
                {
                    await Task.Delay(10);
                }

                string jsonText = request.downloadHandler.text;

                if (request.responseCode == 200)
                {
                    var obj = Json.Deserialize(jsonText) as Dictionary <string, object>;

                    var values = obj["values"] as List <object>;

                    CsvData csv = new CsvData();
                    csv.SetFromListOfListObject(values);
                    return(csv);
                }
                else
                {
                    Debug.LogError($"Failed to GET {url}");
                    Debug.LogError($"Error Code: {request.responseCode}");
                }
            }

            return(null);
        }