Skip to content

Instantly share code, notes, and snippets.

@martinpi
Created March 19, 2025 09:10
Show Gist options
  • Save martinpi/b0d513c4268b1f4814a1964ffab14706 to your computer and use it in GitHub Desktop.
Save martinpi/b0d513c4268b1f4814a1964ffab14706 to your computer and use it in GitHub Desktop.
Download Localisation data from Google
#if TOOLS
using System;
using System.IO;
using System.Text;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
public static class GoogleDataLoader {
private static string[] _Scopes = { SheetsService.Scope.Spreadsheets }; // Change this if you're accessing Drive or Docs
public static GoogleCredential _CredentialFile = GoogleCredential.FromFile("./google.json").CreateScoped(SheetsService.Scope.Spreadsheets);
public static GoogleCredential FromJSON(string json) => GoogleCredential.FromJson(json).CreateScoped(SheetsService.Scope.Spreadsheets);
public static SheetsService Connect(string applicationName, GoogleCredential credential) {
return new SheetsService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = applicationName
});
}
public static ValueRange ReadValueRange(SheetsService sheetService, string sheetId, string sheetName = "1 - Garden") {
SpreadsheetsResource.ValuesResource.GetRequest request = sheetService!.Spreadsheets.Values.Get(sheetId, sheetName);
ValueRange response = request.Execute();
return response;
}
}
public static class GDown {
private static void StoreCSVContents(Google.Apis.Sheets.v4.Data.ValueRange data, string destination) {
string csv = "";
var cols = data.Values[0].Count;
foreach (var v in data.Values) {
csv += "\"" + String.Join("\",\"", v) + "\"" + System.Environment.NewLine;
}
using (FileStream fs = File.Create(destination)) {
Byte[] info = new UTF8Encoding(true).GetBytes(csv);
fs.Write(info, 0, info.Length);
Console.WriteLine("Writing " + info.Length + " bytes to " + destination);
}
}
public static void ImportGDocs(GoogleCredential credential) {
string applicationName = "..."; // replace with correct data
string spreadsheetId = "..."; // replace with correct data
string page = "All";
var sheet = GoogleDataLoader.Connect(applicationName, credential);
if (sheet != null) {
var data = GoogleDataLoader.ReadValueRange(sheet, spreadsheetId, page);
var length = data.Values.Count;
StoreCSVContents(data, "./texts.csv");
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment