Google Sheets API PHP-附加具有行边框和行高的新行

时间:2019-09-17 08:12:54

标签: php google-sheets google-api google-sheets-api

如何以边框和行高之类的样式向工作表添加新行?

这是我的代码:

    $body = new Google_Service_Sheets_ValueRange([
        'values' => $this->values
    ]);

    $params = [
        'valueInputOption' => 'USER_ENTERED',
        // 'insertDataOption' => "INSERT_ROWS"
    ];

    $insert = [
        'insertDataOption' => "INSERT_ROWS"
    ];
    $result = $this->service->spreadsheets_values->append(
        $this->spreadID,
        $this->sheet,
        $body,
        $params,
        $insert
    );

或者有什么方法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

您必须使用batchUpdate方法更新工作表样式。 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate https://developers.google.com/sheets/api/samples/formatting

我更改背景颜色的示例:

$this->_service()->spreadsheets->batchUpdate($this->_spreadsheetId, new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest( [
    'requests' => [
        new \Google_Service_Sheets_Request([
            'repeatCell' => [
                'range'          => [
                    'sheetId' => $this->_sheetId,
                    'startRowIndex' => $newRowIndex,
                    'endRowIndex' => $footerIndex,
                    'startColumnIndex' => 0,
                    'endColumnIndex' => 7
                ],
                "cell"  => [
                    "userEnteredFormat" => [
                        "backgroundColor" => $backgroundColor
                    ]
                ],
                "fields" => "UserEnteredFormat(backgroundColor)"
            ]
        ]),
    ]
]));