如何使用谷歌电子表格API(PHP)合并谷歌表中的两个单元格?
答案 0 :(得分:0)
对于正在寻找在PHP中使用Google电子表格API进行合并的解决方案的人,您可以按照以下步骤进行操作,但是请注意,要运行此代码,您必须使用Google Sheet API PHP SDK。另外,您还必须使用use
关键字在类或php文件的顶部声明此代码中使用的这些类。
####### Set the Grid Range in sheet ########
$rangeinst = new Google_Service_Sheets_GridRange();
$rangeinst->setSheetId($setSheetId); //set your sheet id here
$rangeinst->setStartRowIndex(0); //start row
$rangeinst->setEndRowIndex(2); //end row
$rangeinst->setStartColumnIndex(0); //start column
$rangeinst->setEndColumnIndex(4); //end column
########## Set Grid range object in merging #########
$merge = new Google_Service_Sheets_MergeCellsRequest();
$merge->setMergeType('MERGE_COLUMNS'); //Can be MERGE_ROWS or MERGE_ALL for more you can check https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#GridRange
$merge->setRange($rangeinst);
//update the sheet with merge request object
$requestMergeBody = new Google_Service_Sheets_Request();
$requestMergeBody->setMergeCells($merge);
$mergerequestBatchBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$mergerequestBatchBody->setRequests($requestMergeBody);
$service->spreadsheets->batchUpdate($spreadsheetId, $mergerequestBatchBody); //$spreadsheetId is different than sheet id (a sheet index) so don't confuse with these two terms,
//spreadsheetId will be your whole spreadsheet unique Id that you can found in your share url of spreadsheet and will look something like this 1Mbd5MsTixm5SE0HHl0-yVqj_4ENwijSdNm5OAiG4pN0