I think I have identified the underlying problem, but not the solution.
The problem seems to be that the output cell is on A2, so that I can include formulas on row 1 to reformat the header on the downloaded csv table. If I do not populate Row 1, then the script works ok in both downloading and sorting to csv table. However, when I include the formulas on Row 1, that is when I get the error.