47

I am testing the new Google Spreadsheets as there is a new feature I really need: the 200 sheets limit has been lifted (more info here: https://support.google.com/drive/answer/3541068).

However, I can't publish a spreadsheet to CSV like you can in the old version. I go to 'File>Publish to the web' and there is no more options to publish 'all sheets' or certain sheets and you can't specify cell ranges to publish to CSV etc.

This limitation is not mentioned in the published 'Unsupported Features' documentation found at: https://support.google.com/drive/answer/3543688

Is there some other way this gets enabled or has it in fact been left out of the new version?

My use case is: we retrieve Bigquery results into the spreadsheets, we publish the sheets as a CSV automatically using the "publish automatically on update" feature which then produces the CSV URL which gets placed into charting tools that read the CSV URL to generate the visuals.

Does anyone know how to do this?

7
  • Anyone have the same issues? Commented Feb 4, 2014 at 10:21
  • 1
    I see its been fixed. Publishing successfully now. Commented Mar 10, 2014 at 19:19
  • 2
    The limitation is still there
    – Michael
    Commented Mar 13, 2014 at 21:20
  • 1
    I need the same feature, too. I wish Google would bring it back. Put some kind of traffic limit if they need to. But it's a great feature if you need to let many people edit data, but still want to feed it into a script or other program.
    – Alex
    Commented Apr 11, 2014 at 2:28
  • 2
    @steven.levey How did you fix this? I'm still not seeing any way to publish either a certain sheet or specific range? Any help would be greatly appreciated
    – DarkUFO
    Commented Apr 15, 2014 at 17:28

10 Answers 10

61

The new Google spreadsheets use a different URL (just copy your <KEY>):

  • New sheet : https://docs.google.com/spreadsheets/d/<KEY>/pubhtml
  • CSV file : https://docs.google.com/spreadsheets/d/<KEY>/export?gid=<GUID>&format=csv

The GUID of your spreadsheet relates to the tab number.

/!\ You have to share your document using the Anyone with the link setting.

6
  • 2
    That link doesn't work for me. The gid changes automatically from 0 to some longer number. The page jumps to a 'page not found' page, even though the key is definitely correct. This is all very confusing! Commented May 12, 2014 at 23:22
  • Hi, is there a way to select which columns or make queries as before ?
    – neavilag
    Commented Jun 7, 2014 at 6:27
  • 4
    @Paul McMurdie : Try to remove "gid=0" : docs.google.com/spreadsheets/d/<KEY>/export?format=csv
    – s4tori
    Commented Jun 16, 2014 at 14:02
  • 1
    @s4tori I ended up just switching to the JSON rest-API for google spreadsheets because it is officially supported. Looks like csv is not long-term supported anymore in the updated Google Spreadsheets spec. Commented Jun 16, 2014 at 17:39
  • 2
    gid is the desired workbook id. Spreadsheets may have multiple workbooks. The first one usually has gid of 0 but if you add more they'll have random numbers.
    – aaandre
    Commented Sep 16, 2015 at 20:27
24

Here is the solution, just write it like this:

https://docs.google.com/spreadsheets/d/<KEY>/export?format=csv&id=<KEY>

I know it's weird to write the KEY twice, but it works perfectly. A teammate from work discovered this by opening the excel file in Google Docs, then File -> Download as -> Comma separated values. Then, in the downloads section of the browser appears a link to the CSV file, like this: https://docs.google.com/spreadsheets/d/<KEY>/export?format=csv&id=<KEY>&gid=<SOME NUMBER> But it doesn't work in this format, what my friend did was remove "&gid=<SOME NUMBER>" and it worked! Hope it helps everyone.

6
  • 2
    This is working for me. To download a specific sheet in the spreadsheet include the &gid=<NUMBER>, where <NUMBER> can be obtained by visiting the sheet in in Google docs. The correct number will be in the address bar as #gid=<NUMBER> at the end of the URL.
    – Bryan Rink
    Commented May 28, 2014 at 20:33
  • 1
    Also, make sure you've gone to File->Share and enabled "Anyone who has the link can view"
    – Bryan Rink
    Commented May 29, 2014 at 15:39
  • This worked for me. I tested it with both a new sheet and old style sheet. Interestingly when I tried to export?format=csv with the old sheet it produced an XLSX file, but with the new format it gave CSV
    – stoves
    Commented Oct 7, 2014 at 21:16
  • June 2015 and working. Just have to make sure you do what @BryanRink shows.
    – dpren
    Commented Jun 7, 2015 at 4:07
  • 1
    Unfortunately this example now returns an html, not the csv from gsheet.
    – diegodsp
    Commented Jul 15, 2020 at 12:06
7

If you enable "Anyone with the link sharing" for spreadsheet, here is a simple method to get range of cells or columns (or whatever your feel like) export in format of HTML, CSV, XML, JSON via the query:

https://docs.google.com/spreadsheet/tq?key=YOUR-KEY&gid=1&tq=select%20A,%20B&tqx=reqId:1;out:html;%20responseHandler:webQuery

Downside to this is that your doc is still availble in full via the public link, but if you want to export/import data to say Excel this is a perfect way.

1
  • gid=1 is for old sheets version. In new versions, it looks like gid=2036742317 (IDK if this is random numbers or some sort of hashes). Commented Dec 31, 2014 at 10:37
6

It's not going to help everyone, but I've made a PHP script to read the HTML into an array.

I've added converting back to a CSV at the end. Hopefully this will help some people who have access to PHP.

$html_link  = "https://docs.google.com/spreadsheets/d/XXXXXXXXXX/pubhtml";
$local_html = "sheets.html";

$file_contents = file_get_contents($html_link);
file_put_contents($local_html,$file_contents);

$dom        = new DOMDocument();  
$html       = @$dom->loadHTMLFile($local_html);  //Added a @ to hide warnings - you might remove this when testing
$dom->preserveWhiteSpace = false;   


$tables     = $dom->getElementsByTagName('table');   
$rows       = $tables->item(0)->getElementsByTagName('tr'); 
$cols       = $rows->item(0)->getElementsByTagName('td');  //You'll need to edit the (0) to reflect the row that your headers are in.

$row_headers = array();
foreach ($cols as $i => $node) {
    if($i > 0 ) $row_headers[] = $node->textContent;
}  

foreach ($rows as $i => $row){   
    if($i == 0 ) continue;
    $cols = $row->getElementsByTagName('td');   
    $row = array();
    foreach ($cols as $j => $node) {
        $row[$row_headers[$j]] = $node->textContent;
    }   
    $table[] = $row;
} 

//Convert to csv
$csv = "";
foreach($table as $row_index => $row_details){
    $comma      = false;
    foreach($row_details as $value){
        $value_quotes = str_replace('"', '""', $value);
        $csv .= ($comma ? "," : "") . ( strpos($value,",")===false ? $value_quotes : '"'.$value_quotes.'"'  );
        $comma = true;
    }
    $csv .= "\r\n";
}

//Save to a file and/or output 
file_put_contents("result.csv",$csv);
print $csv;
3
  • 1
    Thanks Jamie G for the effort! I'm hoping someone from @google-apps-script team will update this issue for the new Google Sheets so we dont have to go to these lengths to publish the spreadsheets. Commented Feb 15, 2014 at 6:20
  • Note that due to inevitable Google HTML changes, I made a couple of edits today (13/03/2014) - lines 13 and 14 have tweaks.
    – Jamie G
    Commented Mar 13, 2014 at 11:44
  • 1
    If the spreadsheet has customized column width, there may be additional <div> tags within the nodes, so one may want to use textContent instead of nodeValue Commented Apr 4, 2014 at 2:22
4

Here is another temporary, non-PHP workaround:

  1. Go to an existing NEW google sheet
  2. Go to "File -> New -> Spreadsheet"
  3. Under "File -> Publish to the web..." now has the option to publish a csv version

I believe this is actually creating an old Google sheet but for my purposes (importing google sheet data from clients or myself into R for statistical analysis) it works until they hopefully update this feature.

I posted this in a Google Groups forum also, please find it here:

https://productforums.google.com/forum/#!topic/docs/An-nZtjaupU

1
  • Thank you! This is the best solution I found and its perfect to use in Python, R, ...
    – diegodsp
    Commented Jul 15, 2020 at 12:09
3

The correct URL for downloading a Google spreadsheet as CSV is:

https://docs.google.com/spreadsheets/export?id=<ID>&exportFormat=csv
1
  • this works for me as well as about five other formats listed on various stack overflow posts. there are so many confusing ways to get the same data. mind boggling. Commented Jun 6, 2018 at 1:45
2

The current answers do not work anylonger. The following has worked for me:

1
  • Make sure you perform step 1 and click "Publish to the web", otherwise it will not work
    – feiyingx
    Commented Sep 17, 2014 at 18:02
1

That new feature appears to have disappeared. I don't see any option to publish a csv/tsv version. I can download tsv/csv with the export, but that's not available to other people with merely the link (it redirects them to a google docs sign-in form).

1

I found a fix! So I discovered that old spreadsheets before this change were still allowing only publishing certain sheets. So I made a copy of an old spreadsheet, cleared the data out, copy and pasted my current info into it and now I'm happily publishing just a single sheet of my large spreadsheet. Yay

1

I was able to implement a query to the result, see this table

https://docs.google.com/spreadsheets/d/1LhGp12rwqosRHl-_N_N8eTjTwfFsHHIBHUFMMyhLaaY/gviz/tq?tq=select+A,B,I,J,K+where+B%3E=4.5&pli=1

the spreadsheet fetches data from earthquake, but I just want to select MAG 4.5+ earthquakes so it makes the query and the columns, just a problem:

I cannot parse the result, I tried to decode as json but was not able to parse it. I would like to be able to show this as HTML or CSV or how to parse this ? for example to be able to plot it on a Google Map.

2
  • Great example. The response is not JSON. It's JavaScript. For instance here's a snippet from a test query I just did: {"v":new Date(2015,3,1),"f":"4/1/2015"}.
    – ingydotnet
    Commented Apr 4, 2015 at 13:35
  • Thanks, but I exactly don´t know how to use it, I would like to plot that data on a map... for example
    – neavilag
    Commented Apr 24, 2015 at 2:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.