4
$\begingroup$

I'm able to import data from Google Sheets using e.g.

Import["https://docs.google.com/spreadsheets/d/1F3uGfNJH-hch3NfnvZvqzg8vqmMLUkMF2dgAsHPP66s/gviz/tq?tqx=out:html&tq&gid=0", "Data"]

However, the blank cells are removed, leaving a ragged array. Is there any way of preserving the blank cells in the imported data? (When importing as an Excel sheet, it preserves the blank entries)

$\endgroup$
4
$\begingroup$

Change the URL to request the data in CSV format from Google Sheets (instead of HTML, which is perhaps not the best format for structured tabular data):

Import["https://docs.google.com/spreadsheets/d/1F3uGfNJH-hch3NfnvZvqzg8vqmMLUkMF2dgAsHPP66s/gviz/tq?tqx=out:csv&tq&gid=0", "CSV"]
$\endgroup$
  • $\begingroup$ Thanks, this works for my toy example above but for some reason doesn't work for the full spreadsheet. (outputs "{{"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">"}, \ {"<html>"}, {"<head>"}, {"<META http-equiv=\"Content-Type\" \ content=\"text/html; charset=UTF-8\">"}, {"<title>xxxxxx</title>"}, \ {"</head>"}, {"<body>"}, {"<table border=\"1\" cellpadding=\"2\" \ cellspacing=\"0\">"}, {"<tr style=\"font-weight: bold; \ background-color: #aaa;\">"}, \ {"<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td><\ /td><td></td><td></td><td></td><td></td><td></td>...") $\endgroup$ – Greg Colbourn Dec 19 '19 at 20:22
  • $\begingroup$ @GregColbourn Did you instruct Google Sheets to output CSV? Notice that I am not using the same URL you posted. I changed html to csv in it. $\endgroup$ – Szabolcs Dec 19 '19 at 21:40
  • $\begingroup$ Sorry, missed that bit, yes it works now! $\endgroup$ – Greg Colbourn Dec 19 '19 at 23:23
3
$\begingroup$

In version 10.1 it appears "FullData" will do this, but it also produces some extra fluff that will need to be trimmed.

dat = Import[
   "https://docs.google.com/spreadsheets/d/1F3uGfNJH-\
hch3NfnvZvqzg8vqmMLUkMF2dgAsHPP66s/gviz/tq?tqx=out:html&tq&gid=0", "FullData"];

dat[[2, 2 ;;, ;; 7]] // Grid // TeXForm

$\begin{array}{ccccccc} 341367901 & \text{} & 0 & 0 & 0 & \text{} & 0 \\ 341367902 & \text{} & 0 & 0 & 0 & \text{} & 0 \\ 341367903 & \text{} & 0 & 0 & 0 & \text{} & 0 \\ 341367904 & \text{} & 0 & 0 & 0 & \text{} & 0 \\ 341367905 & \text{} & 0 & 0 & 0 & \text{} & 0 \\ 341367906 & \text{} & 0 & 0 & 0 & \text{} & 0 \\ 341367907 & \text{} & 0 & 0 & 0 & \text{} & 0 \\ 341367908 & 6.3 & 6.3 & 0 & 0 & \text{} & 0 \\ 341367909 & 5.3 & 11.6 & 0 & 0 & \text{} & 0 \\ 341367910 & 5 & 16.6 & 0 & 0 & \text{} & 0 \\ 341367911 & 5.8 & 22.4 & 0 & 0 & \text{} & 0 \\ 341367912 & 5.4 & 27.8 & 0 & 0 & \text{} & 0 \\ 341367913 & 5.4 & 33.2 & 0 & 0 & 278 & 278 \\ 341367914 & 5.3 & 38.5 & 0 & 0 & \text{} & 278 \\ 341367915 & 6.2 & 44.7 & 0 & 0 & \text{} & 278 \\ 341367916 & 1 & 45.7 & 0 & 0 & \text{} & 278 \\ 341367917 & 4 & 49.7 & 0 & 0 & \text{} & 278 \\ 341367918 & \text{} & 49.7 & 0 & 0 & \text{} & 278 \\ 341367919 & 3.6 & 53.3 & 0 & 0 & 219 & 497 \\ 341367920 & \text{} & 53.3 & 0 & 0 & \text{} & 497 \\ 341367921 & \text{} & 53.3 & 0 & 0 & \text{} & 497 \\ 341367922 & 3.9 & 57.2 & 0 & 0 & \text{} & 497 \\ 341367923 & 7 & 64.2 & 0 & 0 & \text{} & 497 \\ 341367924 & 5.9 & 70.1 & 0 & 0 & \text{} & 497 \\ 341367925 & 5.6 & 75.7 & 0 & 0 & \text{} & 497 \\ 341367926 & 5.9 & 81.6 & 0 & 0 & 260 & 757 \\ 341367927 & \text{} & 81.6 & 0 & 0 & \text{} & 757 \\ \end{array}$

$\endgroup$
  • $\begingroup$ This works for both my toy example in the OP and my full spreadsheet, thanks! $\endgroup$ – Greg Colbourn Dec 19 '19 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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