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
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 ).
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).
The spreadsheet ID can be discovered from the spreadsheet URL; the sheet ID can be obtained from the spreadsheet.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With