HUGO

getCSV output nothing

I want to get value of a cell given row and column number, so I use the code below, but it output nothing.

{ $url := "https://docs.google.com/spreadsheets/d/1N8480w1hPyilUS4qK1rnSY6RiE-OX-2hy1glQCTRh_w/export?format=csv&gid=1124904396" }}
    {{ $sep := "," }}
    {{ $csv := getCSV $sep $url }}
    {{ $r := 12 }}
    {{ $c := 2 }}
    <i>
      {{ range $i,$csv }}
        {{ index ( index $csv $r ) $c }}   
      {{ end }}
    </I>

Someone can help me?

This is not a Hugo issue.

The CSV is private: You either need to make it public or use authentication.

I tried to put csv file in the local static folder, and I got the same result.

    {{ $url := "csv/test.csv" }}
{{ $sep := "," }}
{{ $csv := getCSV $sep $url }}
{{ $r := 12 }}
{{ $c := 2 }}
<i>
  {{ range $i,$csv }}
    {{ index ( index $csv $r ) $c }}   
  {{ end }}
</i>

Try putting it in the data folder.

Also see the example for CSV.

I think this line alone without the range should output row 13, column 3:

{{ index ( index $csv $r ) $c }}

It starts with 0, so always add a -1 to the variables where you set the location of the cell.
Then remove the range part, because if you range inside of it the dot (.) is one row, so you could access the column by using {{ index . $c }}. But: you would access that column in EVERY row, so take away the range line and the end line and try again.

Other than that :wink: put a sample online.

Thanks a lot! I removed the {{range}} part, and it output the right result with local csv file now.

but when I use the google spreadsheet csv url, console remind me the infomation below.

ERROR 2021/03/30 21:05:28 Failed to get CSV resource “https://docs.google.com/spreadsheets/d/1N8480w1hPyilUS4qK1rnSY6RiE-OX-2hy1glQCTRh_w/export?format=csv”: failed to parse CSV file https://docs.google.com/spreadsheets/d/1N8480w1hPyilUS4qK1rnSY6RiE-OX-2hy1glQCTRh_w/export?format=csv: parse error on line 3, column 11: bare " in non-quoted-field
If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site config:
ignoreErrors = [“error-remote-getcsv”]
ERROR 2021/03/30 21:05:28 render of “page” failed: execute of template failed: template: _default/single.html:9:6: executing “main” at <index (index $csv 1) 1>: error calling index: index of untyped nil
ERROR 2021/03/30 21:05:28 render of “page” failed: execute of template failed: template: _default/single.html:9:6: executing “main” at <index (index $csv 1) 1>: error calling index: index of untyped nil
ERROR 2021/03/30 21:05:28 render of “page” failed: execute of template failed: template: _default/single.html:9:6: executing “main” at <index (index $csv 1) 1>: error calling index: index of untyped nil
ERROR 2021/03/30 21:05:28 render of “page” failed: execute of template failed: template: _default/single.html:9:6: executing “main” at <index (index $csv 1) 1>: error calling index: index of untyped nil
Built in 32175 ms
Error: Error building site: failed to render pages: render of “page” failed: execute of template failed: template: _default/single.html:9:6: executing “main” at <index (index $csv 1) 1>: error calling index: index of untyped nil

Is this caused by authentication issues ?

This is because you did not publish your spreadsheet properly. You need to set it to public because getCSV does not add any authentication to the file.

Yeah, I found that I forgot to publish my spreadsheet lol. Thanks for ur help! ^^

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.