First time trying to automate some tasks with PHP and gSheet with API. Sorry in advance if my question is fool, I'm really lost here as a newbie.
My php page gets an array converted from a csv file like the example:
0,,0,R0VXJ9I7,2,*%*
1,,0,R0VXJ9I7,3,*%*
,,,1,,*%*
My goal is to append data in a gsheet file as shown above. Each "," means a different column, each ",%" indicates a new row. I got all done but there is one problem: in the Google sheet file the next lane appended column starts where the previous lane finished, like for example:
0 | |0 |R0VXJ9I7 |2
1 | |0 |R0VXJ9I7 |3
| | |1|
What am I doing wrong? I would appreciate any help or advice
require '../../data/vendor/autoload.php';
// the string comes here as plain text
$oddRow = explode(',*%*',$vl02);
//only for debug @.@
$i = 0;
foreach($oddRow as $preRow){
$client = new \Google_Client();
$client->setApplicationName('Google Sheets API');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$path = '../../data/credentials.json';
$client->setAuthConfig($path);
$service = new \Google_Service_Sheets($client);
$spreadsheetId = '#####';//protected for safety
$spreadsheet = $service->spreadsheets->get($spreadsheetId);
$newRow = explode(',', $preRow);
echo $newRow[$i]; //debug
echo $i; //debug
$i++; //debug
$rows = [$newRow];
$valueRange = new \Google_Service_Sheets_ValueRange();
$valueRange->setValues($rows);
$range = '####';//protected for safety
$options = ['valueInputOption' => 'USER_ENTERED'];
$service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $options);
}