Arno van Oordt
Arno van Oordt

Reputation: 3520

Importing generated csv from Laravel into Google Sheets

I'm trying to import an automatically generated csv into Google Sheets using this as cell value: =importData("https://example.com/reports/some-report")

When I try importData on a static file on the server everything works as expected (like =importData("https://example.com/storage/some-static-report.csv") )

..but when I generate the file on the fly () I get a #N/A in the cell; "Resource at url not found." error.

This is the Laravel code to generate the csv:

public function downloadReportsAsCSV(Request $request) {
    $list = [];

    $list[] = ['timestamp', 'id', 'value'];

    // ... fill the list with data here

    $callback = function() use ($list) {
            $out = fopen('php://output', 'w+');

            // Write CSV lines
            foreach ($list as $line) {
                fputcsv($out, $line);
            }

            fclose($out);
        };

    $name = 'somereport.csv';
    $headers = [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => 'attachment; filename='. $name,
        ];
    return response()->stream($callback, 200, $headers);
}

The route to this method is public so authentication is not a problem in this case.

When I call https://example.com/reports/some-report in a browser it downloads the file (as somereport.csv), but somehow Google Sheeds can't handle it the way I expect it to.

Any ideas on how to make this work?

Upvotes: 1

Views: 334

Answers (1)

Arno van Oordt
Arno van Oordt

Reputation: 3520

It seems to be working after all, it's just that Google Sheets apparently needed quit some time before updating the field (at least a couple of minutes).

If anyone has any idea how to trigger Google Sheets to update the data immediately I'd sure like to know.

Upvotes: 1

Related Questions