Search code examples
laravelgoogle-sheetsgoogle-sheets-api

Laravel to Google Sheets new lines not starting at beginning


I'm using this package to store data from a Laravel app into Google Sheets: https://github.com/kawax/laravel-google-sheets/

Everything works fine except for one strange issue. Here's how I add new lines to the sheet:

$sheet = Sheets::spreadsheet("sheet_name")->sheet("tab_name");
$sheet->append($this->data);

$this->data contains an associative array with the column names and values.

Most of the times the new lines are added as desired. But sometimes they start in column B with everything shifted over. And now today they are randomly starting in column V.

I can see that the last line in that sheet has values up to column U, so there must be a connection, even though this new line is added five lines lower.

I've not been able to specify a range for the append method. Is there a way to force Google Sheets to store the data starting from A?


Solution

  • If anybody stumbles across this, I finally found the answer. The issue seems to be that the cursor or range isn't reset between requests, so the input position can change depending on where the last request left off. The solution was to reset the range in such a way:

    $sheet->range('')->append($this->data);