Reading Google Sheets
Published 4 months ago
Getting Started
In this tutorial I am going to look at reading data from Google Sheets.
I will be using the demo sheet Google provided within their original C# tutorial. I also recommend that you complete this tutorial first, we will be using parts from it and you will need the credentials.json generated.

Setting up

We're going to need the Google APIs as well as the Credentials file from the project created in the tutorial above. Create a folder called Plugins in the Assets folder in Unity. Then add a sub folder called GoogleAPI.
This next part was the most trickery, getting and copying the correct libraries from the tutorial.
Locate the Packages folder and copy over the latest versions of each .dll;
  • Google.Apis.Auth.dll (Google.Apis.1.41.1)
  • Google.Apis.Core.dll (Google.Apis.Auth.1.41.1)
  • Google.Apis.dll (Google.Apis.Core.1.41.1)
  • Google.Apis.Sheets.v4.dll (Google.Apis.Sheets.v4.
  • Newtonsoft.Json.dll (Newtonsoft.Json.10.0.2)
You may have to repeat this a few times until you get the correct version. The versions I used are noted above but may have changed.
Copy the credentials.json file to the Assets folder of your unity project.

Creating a Basic Script

Again I will be using the original tutorial to get this up and running before we start to make any changes.
Create yourself a new MonoBehaviour script and add it to an empty game object in the scene. All we are going to do is add the code we need and run the project to get the data from a Google Sheet.
Using Googles tutorial code as a bases complete the following;
Add the following using statements;
using Google.Apis.Auth.OAuth2; using Google.Apis.Sheets.v4; using Google.Apis.Sheets.v4.Data; using Google.Apis.Services; using Google.Apis.Util.Store; using System; using System.IO; using System.Threading;
Before void Start add the following
static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly }; static string ApplicationName = "Google Sheets API .NET Quickstart";
Copy everything from Main to the body of Start. The change the following;
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
...change to...
using (var stream = new FileStream("Assets/credentials.json", FileMode.Open, FileAccess.Read))
Change the final part of the code (after the list generation) to;
Debug.LogFormat("Rows: {0}", values.Count); if (values != null && values.Count > 0) { foreach (var row in values) { Debug.LogFormat("{0}, {1}", row[0], row[4]); } } else { Debug.Log("No data found."); }


Once you have everything setup and you are ready to test the project out click on Play. As this is your first time running the project you will need to authorise your app to access the data. This is the same as you did for the tutorial.
Once it runs you should see a list of individuals in the Console
Note: This was tested with Google Sheets API v4 and Unity 2019.4.0f1 (but should work on other versions of unity)

Going forward

In the next article I will discuss creating ScriptableObjects automatically from a Google Sheet
Martin Dobie
Data Analyist / Developer - Programmer