如何使用php google api更改电子表格的列宽

时间:2018-04-05 20:49:40

标签: google-sheets-api

我正在使用经过修改的Google API V4指南示例here中提供的示例来调用UpdateDimensionProperties,将工作表中第一列的宽度更改为200像素,但是出现错误。有人有这个操作的实例吗?

public function setColumWidths($spreadsheetID, $sheetId) {        
    $dimensionRange = new Google_Service_Sheets_DimensionRange([
        'sheetId' => $sheetId,
        'dimension' => 'COLUMNS',
        'startIndex' => 1,
        'endIndex' => 1        
    ]);

    $dimensionProperties = new Google_Service_Sheets_DimensionProperties([
        'hiddenByFilter' => false,
        'hiddenByUser' => false,
        'pixelSize' => 200,
        'developerMetadata' => [
        ]                    
    ]);


    $requests = [
        // Change the width of the first column in the named sheetId to 200 pixels
        new Google_Service_Sheets_Request([
            'UpdateDimensionProperties' => [
                'range' => $dimensionRange,
                'properties' => $dimensionProperties,                       
                'fields' => 'pixelSize'
            ]
        ])
    ];          

    $request = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest($requests);
    $result = $this->sheetsService->spreadsheets->batchUpdate($spreadsheetID, $request);               
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我一定是在滥用Google_Service_Sheets_Request,因为没有它编码就可以了。虽然将这部分整理出来会很好,但这段代码可以让我继续前进:

public function setColumWidths($spreadsheetID, $sheetId) {

    $dimensionRange = new Google_Service_Sheets_DimensionRange([
        'sheetId' => $sheetId,
        'dimension' => 'COLUMNS',
        'startIndex' => 0,
        'endIndex' => 1        
    ]);

    $dimensionProperties = new Google_Service_Sheets_DimensionProperties([
        'pixelSize' => 200                   
    ]);

    $requestBody = [
        'requests' => [
            'updateDimensionProperties' => [
                'range' => $dimensionRange,
                'properties' => $dimensionProperties,
                'fields' => 'pixelSize'
            ]
        ]
    ];        

    $request = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest($requestBody);
    $result = $this->sheetsService->spreadsheets->batchUpdate($spreadsheetID, $request);        

}