Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check sheet exist or not in spreatsheet Google spreadsheet API PHP

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

like image 369
Rigal Avatar asked Jul 06 '17 05:07

Rigal


People also ask

How do I check if a value exists in another sheet in Google Sheets?

The formula =arrayformula(iferror(match(A2:A, AnotherSheet! A2:A, 0))) checks each value in A2:A of the present sheet for being in A2:A of AnotherSheet. If it's there, it returns the position in AnotherSheet, otherwise the output is empty (the error #N/A is suppressed by iferror ).

How do I check if a value exists in Google Sheets?

We can use the COUNTIF function to count the number of times a value appears in a range. If COUNTIF returns greater than 0, that means that value exists. By attaching “>0” to the end of the COUNTIF Function, we test if the function returns >0. If so, the formula returns TRUE (the value exists).

How do I find the sheet ID in Google Sheets API?

The spreadsheet ID can be discovered from the spreadsheet URL; the sheet ID can be obtained from the spreadsheet.


1 Answers

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
   } 
like image 87
Rigal Avatar answered Oct 18 '22 23:10

Rigal