Greeting SO! As you may have already guessed by looking at my profile, I am pretty new to this community so I'll do my best to explain with as much details and as clearly as I can. :)
P.S.:If you see anything that I could do to improve my code, feel free to suggest it! (I am somewhat of a beginner)
P.S2.: Keep in mind english is my second language so I might do spelling/grammatical mistakes.
Alright, here it is:
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secret.json');
$client->addScope(Google_Service_Drive::DRIVE);
$client->setIncludeGrantedScopes(true);
$service = new Google_Service_Sheets($client);
The code above is my initialization
while(($row = $mssql->fetch()) != false){
$nbRows++;
$data['MatchType'][] = $row['MatchType'];
$data['AccountNo'][] = $row['AccountNo'];
$data['AccountName'][] = $row['AccountName'];
}
So basically what the block of code above does is fill an array named with the header of a column with all the values from one columns of the database. It'll do so with all the columns of the database. So you'll end up with something like:
data (
MatchType (
[0] => Success
[1] => Failed
[2] => Pending
...
)
AccountNo (
[0] => 12345
[1] => 23456
[2] => 34567
...
)
AccountName (
[0] => Family Dental
[1] => Chez Germain et ses amis!
[2] => 123 travellers's guide
...
)
After that I just send all this data to my function addData. ($nbRows and $columnArray[$columnValue] are simply used to specify the range in the spreadsheet in which I want to write later on)
$columnValue = 0;
$columnArray = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
foreach ($data as $key => $element) {
addData($data[$key], $service, $spreadsheetId, $nbRows, $columnArray[$columnValue]);
$columnValue++;
}
Now we meet my function that calls the BatchUpdate method of the API and adds the columns to the spreadsheet.
function addData($receivedData, $service, $spreadsheetId, $nbRows, $columnRank) {
$data = new Google_Service_Sheets_ValueRange();
$data->setMajorDimension("COLUMNS");
$data->setRange("sample!" . $columnRank . "1:" . $columnRank . $nbRows);
$data->setValues(["values" => $receivedData]);
$requestBody = new Google_Service_Sheets_BatchUpdateValuesRequest();
$requestBody->setValueInputOption("RAW");
$requestBody->setData($data);
$response = $service->spreadsheets_values->batchUpdate($spreadsheetId,$requestBody);
}
Now to my question
When addData comes to adding my third (and only the third) column which is AccountName, I come across this error:
Google_Service_Exception: { "error": { "code": 400, "message": "'valueInputOption' is required but not specified", "errors": [ { "message": "'valueInputOption' is required but not specified", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } } in C:\wamp\www\vendor\google\apiclient\src\Google\Http\REST.php on line 118
This error happens even tho I have verified multiple times that I do indeed pass the appropriate valueInputOption the method. I have also verified the data I passed and I proved it was working by using the API explorer on the google site to verify it added the data to the spreadsheet (which it did). I also tried casting the data as a string and removing special characters to be sure but it didn't worked. I am feeling lost because I can successfully add my first two columns to the spreadsheet but for some reasons, when it comes to the third which has similar data, it gives me this error. I need your Help SO!
EDIT: Added AccountName exemples.