Get cell format information from Google Sheets using PHP

别来无恙 提交于 2020-01-25 07:06:50

问题


I'm reading values from Google Sheets using google-api-php-client and google-api-php-client-services libraries this way:

   $service = new \Google_Service_Sheets($this->calendarService->getClient());
        $spreadsheetId = 'abhslJcKXjIqS8bAJ1lojekXuOk0WOSrdtVtJ2C512jQ';

        $range = 'Sheet Name';

        $response = $service->spreadsheets_values->get($spreadsheetId, $range);

        $values = $response->getValues(); // returns all cells as array

How can I get information on format of each cell? (eg. number, date, email etc.).


回答1:


None of the responses you can get from the Spradsheets.batchUpdate [1] includes information about the cells format. Neither the Spreadsheets.values.get request [2] includes this information in its response (ValueRange object [3]). You're able to change the cells format using UpdateCells request [4], but you can't retrieve it using Sheets API.

Using Apps Script there's a function which retrieves the number/date format [5], you could do a workaround with it.

[1] https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/response#Response

[2] https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get

[3] https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values#ValueRange

[4] https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#UpdateCellsRequest

[5] https://developers.google.com/apps-script/reference/spreadsheet/range.html#getNumberFormat()




回答2:


You can get cell formatting also get textFormat (bold,strikethrough etc) Beside, you can search the object there are more specs related to each cell.

Cell Documentation of google api

To be able to get this you should use Google_Service_Sheets -> spreadsheets method by including gridData like below :

$sheets = new \Google_Service_Sheets($client); //callling sheet api with authorized client 
$spreadsheet = $sheets->spreadsheets->get($spreadsheetId,['includeGridData' => true]);

And i assume spreadsheet has 1 sheet to show you correct place of formatting :

$sheet = $spreadsheet->getSheets();
$format = $sheet[0]['data'][0]['rowData']['values'][0]['effectiveFormat']['numberFormat]; //this is one of example from that object

Hope it helps



来源:https://stackoverflow.com/questions/58843013/get-cell-format-information-from-google-sheets-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!