5

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.

WWWubzy
  • 65
  • 6
  • You may refer with this [thread](https://stackoverflow.com/questions/37785216/google-sheets-api-v4-and-valueinputoption) which stated that to make all needed data to be represented as string, you need to `set valueInputOption = USER_ENTERED;` and prepend it with apostrophe sign `'`. Here's another [SO post](https://stackoverflow.com/questions/38025841/trying-to-append-a-row-to-a-google-spreadsheet-in-php) which might help. – abielita May 30 '17 at 14:28
  • @abielita First of all, thanks alot for your comment! Unfotunately though I had already done extensive research before posting this question and looked at both of those threads :s – WWWubzy May 30 '17 at 17:40
  • @WWWubzy did you find a workaround? – Alexey Kosov Oct 04 '17 at 15:51
  • @Alexey Kosov Unfortunately I did not. We ended scraping the idea anyway because of google's api usage limitations. Sorry for not being of much help. – WWWubzy Oct 04 '17 at 16:00

0 Answers0