Search code examples
phpgoogle-sheetsgoogle-sheets-apigoogle-api-php-client

Locate specific text in Google Sheets using the API


I have a Google Sheet and I am able to extract the ID of the file using Google Drive API. I'd like to search the file for specific text using the API, specifically I want to extract the row and column of the text. I found information on developer metadata on the docs, but there is no php example and I am lost.


Solution

  • I manage to convert the file to plain text using this code:

    $response = $service->files->export($fileId, 'text/csv', array(
    'alt' => 'media'));
    

    and using this code to get the full content of the file

    $content = $response->getBody()->getContents();
    

    and then with a simple regex i was able to extract all the data i needed. Maybe this is not the best solution, but it is good workaround for me. This is my regex in case anyone is wondering

    $re = '/"(.*?) at (.*?)",(.*?' . $order['Phone'] . '),.*?,([0-9]+)/m';
    preg_match_all($re, $content, $matches, PREG_SET_ORDER, 0);