8

How to check if sheet exits in a spreadsheet (check sheet by name) or not using Google Sheet API. I have used Google Sheet API v4.

If a sheet is not existed then create a new sheet.

Thanks

Bhupesh
  • 883
  • 7
  • 16
Rigal
  • 611
  • 2
  • 6
  • 25

1 Answers1

7

Finally I found Solution.There is no method available to check sheet exist or not in spreadsheet.

I have used spreadsheet object to get all information of spreadsheet. and make custom function to check specific sheet name exist or not and it's working fine.

    function myArrayContainsWord(array $myArray, $word) {
    foreach ($myArray as $element) {
        if ($element->title == $word) {
            return true;
        }
    }
    return false;
  }

   $service = new Google_Service_Sheets($client);
   $spreadsheetId = 'your spredsheetid';
   $sheetInfo = $service->spreadsheets->get($spreadsheetId);
   $allsheet_info = $sheetInfo['sheets'];
   $idCats = array_column($allsheet_info, 'properties');

   if (myArrayContainsWord($idCats, "sheetname")) {
      //echo found
   } 
Rigal
  • 611
  • 2
  • 6
  • 25