This is an API that matches the version 4 of Google's sheets API for the most part. So if you'd like documentation on how to use this API, you have to consult their API instead, and use the C# bindings. There is also a smaller high level layer on top, which is useful for people who just need to access the data from a 2D array.
An abstract SheetsSerializer class exists so that you can use any JSON serializer you'd like.
using UnityEngine;
public class JSONSerializer : SheetsSerializer
{
public override T DeserializeObject<T>(string data)
{
return JsonUtility.FromJson<T>(data);
}
public override string SerializeObject(object data)
{
return JsonUtility.ToJson(data);
}
}using Newtonsoft.Json;
public class JSONSerializer : SheetsSerializer
{
public override T DeserializeObject<T>(string data)
{
return JsonConvert.DeserializeObject<T>(data);
}
public override string SerializeObject(object data)
{
return JsonConvert.SerializeObject(data);
}
}The requirements to using the Google Sheets API, is to have the spreadsheetId and an access token. The spreadsheetId can be retrieved from a url. You will also need to provide the SheetsSerializer.Serializer property with your own serializer object.
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}
The API token can be created from the Google API Console, for more info on this, visit this article by google
public async void Start()
{
SheetsSerializer.Serializer = new SheetsSerializer(); //TODO: Create your own custom serializer/deserializer
string spreadsheetId = ""; //TODO: Get your own spreadsheetId
string key = ""; //TODO: Get your own key token
Authorization authorization = await Authorization.Authorize(key);
Spreadsheet spreadsheet = await Spreadsheet.Get(spreadsheetId, authorization);
Debug.Log("URL: " + spreadsheet.URL);
Debug.Log("Title: " + spreadsheet.Title);
Sheet sheet = spreadsheet.Sheets[0];
Debug.Log("Rows: " + sheet.Rows);
Debug.Log("Columns: " + sheet.Columns);
Cell[,] data = sheet.Data;
for (int x = 0; x < sheet.Columns; x++)
{
for (int y = 0; y < sheet.Rows; y++)
{
Debug.Log(data[x, y].Value);
}
}
}If you'd like to use the low level API, you can use the GetRaw() method instead of Get(). The raw method alternative will give out a SpreadsheetRaw object, which is identical to the Google API reference.
If you want to work with both the low level and high level, you can create a spreadsheet from the low level spreadsheet by passing it into the constructor. The same can be done for converting a raw sheet to a high level sheet. This can not be done the other way around, and its by design.
SheetsSerializer.Serializer = new SheetsSerializer();
string key = ""; //TODO: Get a key
Authorization authorization = await Authorization.Authorize(key);
SpreadsheetRaw raw = await SpreadsheetRaw.Get(spreadsheetId, token, includeGridData);
Spreadsheet spreadsheet = await Spreadsheet.Get(spreadsheetId, token, serializer);
//create a new spreadsheet from the raw data
Spreadsheet spreadsheetConverted = new Spreadsheet(raw);Two ways to authorize are implemented. Using OAuth client ID and client secret, or using a provided key from the Google Developer Console.
string key = "";
Authorizer authorizer = await Authorizer.Authorizer(key);string clientId = "";
string clientSecret = "";
Authorizer authorizer = await Authorizer.Authorize(clientId, clientSecret);NOTE: Modifying calls require OAuth authorization
new SheetSerializer()?
Because it is an abstract class, and you have to make your own implementation of this class.