【问题标题】:Formatting cells background and bold in google sheet (php)在谷歌表(php)中格式化单元格背景和粗体
【发布时间】:2019-07-25 17:12:44
【问题描述】:

我正在尝试更改电子表格中几个单元格的背景颜色和粗体值。我知道我有一个有效的服务,因为我之前正在更新电子表格值并且它有效。

更改值有效,所以我猜它一定是我的配置。我没有收到任何错误,$result 变量保持为空。任何想法将不胜感激,谢谢。

// known to work from changing the values
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'MY SPREADSHEET ID';

// new code
$r = $g = $b = 0.5;
$a = 1;

$myRange = [
    //'sheetId' => 0, // can be omitted because I'm working on the first sheet
    'startRowIndex' => 5,
    'endRowIndex' => 10,
    //'startColumnIndex' => 0, // can be omitted because default is 0
    'endColumnIndex' => 5,
];
$format = [
    'backgroundColor' => [
        'red' => $r,
        'green' => $g,
        'blue' => $b,
        'alpha' => $a,
    ],
    'textFormat' => [
      'bold' => true
    ]
];


$requests = [
    new Google_Service_Sheets_Request([
        'repeatCell' => [
            'fields' => 'userEnteredFormat.backgroundColor, userEnteredFormat.textFormat.bold',
            'range' => $myRange,
            'cell' => [
                'userEnteredFormat' => $format,
            ],
        ],
    ])
];


$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
  'requests' => $requests
]);
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);

【问题讨论】:

  • I [don't] get any errors我没有注意到任何错误消息
  • 谢谢@greybeard,那条评论让我想到了正确的方向。请参阅下面的解决方案。
  • @Ja Nosch 感谢您的回复。我深表歉意,我的回答对解决您的问题没有用。不幸的是,从您的回答中,我无法理解您的最终脚本。那么您可以将最后一个添加到您的答案中吗?然后,从您的回复中,我可以知道我的回答不是必需的。所以我必须删除我的答案,因为我不想混淆其他用户。
  • @Tanaike,我在下面添加了完整的脚本。感谢您的提示,我从最初的问题中删除了错误的呼叫,以免其他人感到困惑。
  • @Ja Nosch 感谢您的回复。非常抱歉我的回答对解决您的问题没有帮助。

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


【解决方案1】:

解决方案:

我在这里犯了两个错误:

1) 我以为错误报告已开启,但事实并非如此。 @greybeard 让我寻找它,然后我收到以下错误(这可能对其他人有帮助):

Uncaught Google_Service_Exception: { "error": { "code": 400, "message": "Invalid requests[0].repeatCell: No grid with id: 1"

2) 正如您在上面的错误中看到的,我的网格 ID 被拒绝。这是因为我将sheetId 与工作表索引混淆了。谷歌表格实际上有一个“真实”的 id,您可以通过以下请求获得它

$sheetId = $service->spreadsheets->get($spreadsheetId);

这将返回一个数组,其中包含一个包含 sheetIds 的对象。对于索引为 0 的工作表,这将是

$sheetId = $sheetId->sheets[0]->properties->sheetId;

要直接访问某个工作表而不是整个电子表格,您可以添加“范围”属性

$sheetId = $service->spreadsheets->get($spreadsheetId, ['ranges' => 'NAME_OF_SHEET']);

所以完整的脚本是:

$client = getClient(); // this calls my custom function that does the authentication
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'MY SPREADSHEET ID';

// get sheetId of sheet with index 0
$sheetId = $service->spreadsheets->get($spreadsheetId);
$sheetId = $sheetId->sheets[0]->properties->sheetId;

// set colour to a medium gray
$r = $g = $b = 0.5;
$a = 1;

// define range
$myRange = [
    'sheetId' => $sheetId, // IMPORTANT: sheetId IS NOT the sheets index but its actual ID
    'startRowIndex' => 5,
    'endRowIndex' => 10,
    //'startColumnIndex' => 0, // can be omitted because default is 0
    'endColumnIndex' => 5,
];

// define the formatting, change background colour and bold text
$format = [
    'backgroundColor' => [
        'red' => $r,
        'green' => $g,
        'blue' => $b,
        'alpha' => $a,
    ],
    'textFormat' => [
      'bold' => true
    ]
];

// build request
$requests = [
    new Google_Service_Sheets_Request([
        'repeatCell' => [
            'fields' => 'userEnteredFormat.backgroundColor, userEnteredFormat.textFormat.bold',
            'range' => $myRange,
            'cell' => [
                'userEnteredFormat' => $format,
            ],
        ],
    ])
];

// add request to batchUpdate    
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
  'requests' => $requests
]);

// run batchUpdate
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);

【讨论】:

    猜你喜欢
    • 2012-12-24
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2015-04-20
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多