import re


def gsheet_csv_url(url):
    """
    Returns the url for the Google Sheet csv export.

    Parameters
    ----------
    url : str
        The editor url string as found when viewing the sheet in a browser.
    """
    def get_sheet():
        for i, x in enumerate(s):
            if x == 'gid':
                return s[i+1]

        raise ValueError('Sheet ID not found in url {}'.format(url))

    s = re.split('/|#|=|&', url)

    key = s[5]
    sheet = get_sheet()

    return 'https://docs.google.com/spreadsheets/d/{}/export?gid={}&format=csv'.format(key, sheet)