Help with displaying first 10 entries of CSV where Range is already filtered [SOLVED]

{{ range $i, $r := getCSV .Site.Params.sep .Site.Params.userreviews }}
            {{ if eq (index $r 3) $.Params.company }}<li>{{ index $r 9 }}</li>
{{end}}{{end}}

The code above looks up a Google Sheet, and checks to see whether column 3 matches the company name. If it does, it displays the contents of column 9.

{{ range $i, $r := getCSV .Site.Params.sep .Site.Params.userreviews | first 10 }}
            {{ if eq (index $r 3) $.Params.company }}<li>{{ index $r 9 }}</li>
{{end}}{{end}}

This will do the same except display the first 10 entries.

Quite clearly, this is filtering the first 10 before I check if column 3 equals the company name, so is not what I want. I want the first 10 entries where the company equals column 3.

Can anyone point me in the right direction on this please? I’m stumped.

Well, I have got mostly there using scratch, but not sure that it the most efficient way.

As my data is in Google Sheets, I am looking at that side of things to make it more efficient. I’ll post my solution when I have it sorted fully, but if anyone has any suggestions, I am still open to them.

Edit: So this has worked out quite well.

As a little background, this is for user review functionality on my website. Reviews are submitted via Typeform, which is then copied automatically to Google Sheets. The CSV is then published to the web via Google Sheets, which I then set as a site variable in Hugo and iterate over the data.

One of the issues (which I have solved) is that it adds the new review to the bottom of the sheet, so when I tried to use scratch it loads them in the wrong order (i.e. oldest at the top). Calculating the bottom 20 reviews was also a little messy in Scratch compared to my current solution.

I solved this by creating a new worksheet, and using the SORT function to automatically put this at the top. Example:

=SORT(userreviews!A2:ZZ9998,17, FALSE)

This makes iterating over the data so much more simple, and I can then use a very simple Scratch to display it (test example below):

            {{ $.Scratch.Set "companyreviewcounter" 0 }}
            {{ range $i, $r := getCSV .Site.Params.sep .Site.Params.userreviews  }}
                {{ if eq (index $r 3) $.Params.company}}
                {{ $.Scratch.Add "companyreviewcounter" 1 }}
                    {{ if le ($.Scratch.Get "companyreviewcounter") 20  }}
                    <li>{{ index $r 9 }}</li>
                    {{end}}
                {{end}}
            {{end}}
1 Like