Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Created April 9, 2015 03:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save radiatoryang/93fd9c291bbc85e815a6 to your computer and use it in GitHub Desktop.
Save radiatoryang/93fd9c291bbc85e815a6 to your computer and use it in GitHub Desktop.
Unity C# code for grabbing a publicly published Google Docs spreadsheet
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// usage: place on a UI Text object to visualize spreadsheet data
// preparing a Google Doc: make sure you go to File >> Publish... "Share" does NOT work for this
// publicly editable: https://docs.google.com/spreadsheets/d/1QDaGFXxh3zbVTcQKXygKZ67_EFXjUlGbHbK21ICvv-k/edit?usp=sharing
// view as HTML: https://docs.google.com/spreadsheets/d/1QDaGFXxh3zbVTcQKXygKZ67_EFXjUlGbHbK21ICvv-k/pubhtml
// view as CSV: https://spreadsheets.google.com/pub?hl=en&hl=en&key=1QDaGFXxh3zbVTcQKXygKZ67_EFXjUlGbHbK21ICvv-k&output=csv
// CSV URL template: https://spreadsheets.google.com/pub?hl=en&hl=en&key= +INSERT DOC ID KEY HERE+ &output=csv
public class DemoSpreadsheet : MonoBehaviour {
public string spreadsheetKey = "1QDaGFXxh3zbVTcQKXygKZ67_EFXjUlGbHbK21ICvv-k";
// Start can be used as a coroutine
IEnumerator Start() {
// create a new "WWW" object that will fetch the web data
WWW webRequest = new WWW( "https://spreadsheets.google.com/pub?hl=en&hl=en&key=" + spreadsheetKey + "&output=csv" );
// wait until the web data has finished downloading
yield return webRequest;
// you could also see whether (WWW.isDone == true) to see if it has finished yet
// treat the web data like alphanumeric text (a string)
var csvData = webRequest.text;
// convert text into rows by splitting along line breaks
string[] rows = csvData.Split ( "\n" [0] );
// print rows
foreach ( var row in rows ) {
Debug.Log ( row );
GetComponent<Text>().text += row + "\n";
yield return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment