问题描述
我是新手,我创建了一个Google表格,并使用 PHP API :
I'm newbie on this, I create a Google Sheets and put some content there using PHP API:
$id_file = "XXXXXXXXxxxxXXXXXXXXxxx"; //ID sheet
$range = "A2:E2";
$values = [["INGLÉS","ESPAÑOL","DEFINICIÓN","FUENTE","COMENTARIOS"]];
$range1 = "A1:E1";
$values1 = [["GLOSARIO"]];
$data = [];
$data1 = [];
$data[] = new Google_Service_Sheets_ValueRange([
'range' => $range,
'majorDimension' => 'ROWS',
'values' => $values
]);
$requestBody = new Google_Service_Sheets_BatchUpdateValuesRequest([
"valueInputOption" => "USER_ENTERED",
"data" => $data
]);
$response = $service->spreadsheets_values->batchUpdate($id_file, $requestBody);
$data1[] = new Google_Service_Sheets_ValueRange([
'range' => $range1,
'majorDimension' => 'ROWS',
'values' => $values1
]);
$requestBody1 = new Google_Service_Sheets_BatchUpdateValuesRequest([
"valueInputOption" => "USER_ENTERED",
"data" => $data1
]);
$response1 = $service->spreadsheets_values->batchUpdate($id_file, $requestBody1);
我得到这个结果:
效果很好,但我想在第一行合并单元格和某些格式,我想要这样的结果:
It works great but I would like to merge cells and some formatting on the first row, I would like a result like this:
寻找一些我尝试过的信息:
Looking some information I tried with this:
$rangel = new Google_Service_Sheets_GridRange();
$rangel->setStartRowIndex(0);
$rangel->setEndRowIndex(1);
$rangel->setStartColumnIndex(0);
$rangel->setEndColumnIndex(5);
$rangel->setSheetId(0);
$request = new Google_Service_Sheets_MergeCellsRequest();
$request->setMergeType('MERGE_COLUMNS');
$request->setRange($range1);
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdateRequest->setRequests($request);
$response = $service->spreadsheets->batchUpdate($id_file,$batchUpdateRequest);
但是它不起作用,我该如何解决?
But it doesn't work, How can I fix it?
我需要一些帮助.
推荐答案
- 您要使用Sheets API和php合并Spreadsheet的"A1:E1"单元格.
如果我的理解是正确的,那么该修改如何?我认为您的以下脚本几乎是正确的.需要对其进行一些修改.请对其进行如下修改.
If my understanding is correct, how about this modification? I think that your below script is almost correct. It is required to be modified a little. Please modify it as follows.
$rangel = new Google_Service_Sheets_GridRange();
$rangel->setStartRowIndex(0);
$rangel->setEndRowIndex(1);
$rangel->setStartColumnIndex(0);
$rangel->setEndColumnIndex(5);
$rangel->setSheetId(0);
$request = new Google_Service_Sheets_MergeCellsRequest();
$request->setMergeType('MERGE_ROWS'); // Modified
$request->setRange($rangel); // Modified
$body = new Google_Service_Sheets_Request(); // Added
$body->setMergeCells($request); // Added
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdateRequest->setRequests($body); // Modified
$response = $service->spreadsheets->batchUpdate($id_file,$batchUpdateRequest);
其他示例脚本:
如果要合并单元格并将水平对齐方式更改为与图像一样居中,那么下面的示例脚本怎么样?
Additional sample script:
If you want to merge the cells and change the horizontal alignment to center like your image, how about the following sample script?
// Range
$rangel = new Google_Service_Sheets_GridRange();
$rangel->setStartRowIndex(0);
$rangel->setEndRowIndex(1);
$rangel->setStartColumnIndex(0);
$rangel->setEndColumnIndex(5);
$rangel->setSheetId(0);
// Merge rows of "A1:E1".
$request1 = new Google_Service_Sheets_MergeCellsRequest();
$request1->setMergeType('MERGE_ROWS');
$request1->setRange($rangel);
$body1 = new Google_Service_Sheets_Request();
$body1->setMergeCells($request1);
// Change horizontalAlignment to "CENTER".
$cellFormat = new Google_Service_Sheets_CellFormat();
$cellFormat->setHorizontalAlignment('CENTER');
$cellData = new Google_Service_Sheets_CellData();
$cellData->setUserEnteredFormat($cellFormat);
$rowData = new Google_Service_Sheets_RowData();
$rowData->setValues([$cellData]);
$rows[] = $rowData;
$request2 = new Google_Service_Sheets_UpdateCellsRequest();
$request2->setRows($rows);
$request2->setFields('userEnteredFormat.horizontalAlignment');
$request2->setRange($rangel);
$body2 = new Google_Service_Sheets_Request();
$body2->setUpdateCells($request2);
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdateRequest->setRequests([$body1, $body2]);
$response = $service->spreadsheets->batchUpdate($id_file, $batchUpdateRequest);
注意:
- 此修改后的脚本和示例脚本假定已经可以使用Sheets API.
- MergeCellsRequest
- UpdateCellsRequest
- PHP Quickstart
如果我误解了您的问题,而这不是您想要的结果,我深表歉意.
If I misunderstood your question and this was not the result you want, I apologize.
这篇关于如何使用Google Sheets API和PHP合并单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!