Skip to content

Instantly share code, notes, and snippets.

@nothke
Created December 25, 2019 13:32
Show Gist options
  • Select an option

  • Save nothke/8980d6273fec681971107fe533a9d539 to your computer and use it in GitHub Desktop.

Select an option

Save nothke/8980d6273fec681971107fe533a9d539 to your computer and use it in GitHub Desktop.
using System.Net;
public static class GoogleSheetsDownloader
{
/// <summary>
///
/// </summary>
/// <param name="url">Google Sheets link (set to view 'Anyone with the link'), without stuff after slash: /edit.. or /view..
/// For example https://docs.google.com/spreadsheets/d/12345U57wZNjsOtOB3ULxXofn45axbHiA-qX37kF2sOM </param>
/// <param name="format">Data format, for example "csv" or "tsv"</param>
/// <returns></returns>
public static string Download(string url, string format)
{
const string FORMAT_PREFIX = "/export?gid=0&format=";
string suffix = FORMAT_PREFIX + format;
string str = GetResponseText(url + suffix);
return str;
}
static string GetResponseText(string address)
{
var request = (HttpWebRequest)WebRequest.Create(address);
using (var response = (HttpWebResponse)request.GetResponse())
{
var encoding = System.Text.Encoding.UTF8;// System.Text.Encoding.GetEncoding(response.CharacterSet);
using (var responseStream = response.GetResponseStream())
using (var reader = new System.IO.StreamReader(responseStream, encoding))
return reader.ReadToEnd();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment