Reputation: 4439
I'm trying to center align a range of cells, but only the first cell in the range is updated with the specified format.
Here is my code:
align = 'CENTER'
data={
"requests": [
{
"updateCells": {
"rows": [
{
"values": [
{
"userEnteredFormat": {
"horizontalAlignment": align,
"textFormat": {
"fontFamily": fontFamily,
"fontSize": fontSize
}
}
}
]
}
],
"range": {
"sheetId": sheetId,
"startRowIndex": startRowIndex,
"endRowIndex": endRowIndex,
"startColumnIndex": startColumnIndex,
"endColumnIndex": endColumnIndex
},
"fields": "userEnteredFormat"
}
}
]
}
If I log the values - i.e. print (startRowIndex, endRowIndex, startColumnIndex, endColumnIndex)
- they are correct (e.g. 0 1 27 30
), yet only the first cell is updated to the format - not the whole range.
What's going on here? How can I apply the specified format to the whole range?
Upvotes: 1
Views: 910
Reputation: 201378
You want to update "AB1:AD1" ({startRowIndex: 0, endRowIndex: 1, startColumnIndex: 27, endColumnIndex: 30}
). If my understanding is correct, how about this modification?
{values: [{userEnteredFormat: ###}, {userEnteredFormat: ###}, {userEnteredFormat: ###}]}
.{
"requests":
[
{
"updateCells":
{
"rows":
[
{
"values":
[
{
"userEnteredFormat":
{
"horizontalAlignment": align , #'CENTER','LEFT','RIGHT',
"textFormat":
{
"fontFamily": fontFamily,
"fontSize": fontSize
}
}
},
{
"userEnteredFormat":
{
"horizontalAlignment": align , #'CENTER','LEFT','RIGHT',
"textFormat":
{
"fontFamily": fontFamily,
"fontSize": fontSize
}
}
},
{
"userEnteredFormat":
{
"horizontalAlignment": align , #'CENTER','LEFT','RIGHT',
"textFormat":
{
"fontFamily": fontFamily,
"fontSize": fontSize
}
}
}
]
}
],
"range":
{
"sheetId": sheetId,
"startRowIndex": startRowIndex,
"endRowIndex": endRowIndex,
"startColumnIndex": startColumnIndex,
"endColumnIndex": endColumnIndex
},
"fields": "userEnteredFormat",
}
}
]
}
If I misunderstand your question, please tell me. I would like to modify it.
When you want to reflect the format for a lot of cells, you can use repeatCell
. The request body is as follows. In this sample, all cells in the range are modified .
{
"requests":
[
{
"repeatCell":
{
"cell":
{
"userEnteredFormat":
{
"horizontalAlignment": align , #'CENTER','LEFT','RIGHT',
"textFormat":
{
"fontFamily": fontFamily,
"fontSize": fontSize
}
}
},
"range":
{
"sheetId": sheetId,
"startRowIndex": startRowIndex,
"endRowIndex": endRowIndex,
"startColumnIndex": startColumnIndex,
"endColumnIndex": endColumnIndex
},
"fields": "userEnteredFormat"
}
}
]
}
Upvotes: 3