Home > Net >  How to read 2 dimensional array data using Google Sheets API?
How to read 2 dimensional array data using Google Sheets API?

Time:11-14

i'm new to Google Sheets API, currently i am develop a system to send data to google sheets. And while im trying to send the data, it shows error like: Invalid values [0][0]

This is the error

Below is my code line.

`

     $client = getClientAuth();

        $spreadsheet_ID = $this->spreadsheet->spreadsheetId; 
        
        $range = "Sheet1";

        $values = $this->sheetData;

        $this->service = new Sheets($client);


        $body = new ValueRange([
            'majorDimension' => 'ROWS',
            'values' => [array_map(function($nv) {
                return (is_null($nv)) ? "" : $nv;
              },$values)]
        ]);

        $params = ['valueInputOption' => 'RAW'];

        $insert = ['insertDataOption' => 'INSERT_ROWS'];

        //executing the request
        $data = $this->service->spreadsheets_values->append($spreadsheet_ID, $range,
        $body, $params, $insert);
    
        return $data;

`

CodePudding user response:

From your error message and your showing script, I thought that in your situation, $values might be a 2-dimensional array. I think that if $values is a 1-dimensional array, your script works. And also, please include 'insertDataOption' => 'INSERT_ROWS' in $params. So, in this case, how about the following modification?

From:

$body = new ValueRange([
    'majorDimension' => 'ROWS',
    'values' => [array_map(function($nv) {
        return (is_null($nv)) ? "" : $nv;
      },$values)]
]);

$params = ['valueInputOption' => 'RAW'];

$insert = ['insertDataOption' => 'INSERT_ROWS'];

//executing the request
$data = $this->service->spreadsheets_values->append($spreadsheet_ID, $range,
$body, $params, $insert);

To:

$body = new ValueRange([
    'majorDimension' => 'ROWS',
    'values' => array_map(function($row) {
      return array_map(function($col) {
        return (is_null($col)) ? "" : $col;
      }, $row);
    }, $values)
]);
$params = ['valueInputOption' => 'RAW', 'insertDataOption' => 'INSERT_ROWS'];
$data = $service->spreadsheets_values->append($spreadsheet_ID, $range, $body, $params);

or

$body = new Google_Service_Sheets_ValueRange([
    'majorDimension' => 'ROWS',
    'values' => array_map(function($row) {
      return array_map(function($col) {
        return (is_null($col)) ? "" : $col;
      }, $row);
    }, $values)
]);
$params = ['valueInputOption' => 'RAW', 'insertDataOption' => 'INSERT_ROWS'];
$data = $service->spreadsheets_values->append($spreadsheet_ID, $range, $body, $params);

Note:

  • When the arrow function is used, I think that the following modification can be used.

    • From

      'values' => array_map(function($row) {
        return array_map(function($col) {
          return (is_null($col)) ? "" : $col;
        }, $row);
      }, $values)
      
    • To

      'values' => array_map(fn($row) => array_map(fn($col) => (is_null($col)) ? "" : $col, $row), $values)
      
  • Related