我是新手,我创建了Google表格并使用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);
我得到这个结果:
效果很好,但是我想在第一行合并单元格和某些格式,我想要这样的结果:
寻找一些我尝试过的信息:
$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);
但是它不起作用,我该如何解决?
我需要一些帮助。
答案 0 :(得分:1)
如果我的理解是正确的,那么该修改如何?我认为您的以下脚本几乎是正确的。需要对其进行一些修改。请进行如下修改。
$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);
如果您要合并单元格并将水平对齐方式更改为与图像一样居中,那么下面的示例脚本怎么样?
示例脚本:// 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);
如果我误解了您的问题,而这不是您想要的结果,我深表歉意。