问题
I'm trying to change a cell format to horizontal alignment for the text. Can't get it to work. Here is my code so far.
data={
"requests":[
{
"horizontalAlignment": {
'range': {
"sheetId": sheetId,
"startRowIndex": 0,
"endRowIndex": 10,
"startColumnIndex": 0,
"endColumnIndex": 1
},
'Center'
}
}
]
}
result = g.service.spreadsheets().batchUpdate(spreadsheetId=key, body=data).execute()
not sure how to specify the range in there with the horizontal alignment.
回答1:
How about this modification?
Modified script :
data={
"requests":
[
{
"updateCells":
{
"rows":
[
{
"values":
[
{
"userEnteredFormat":
{
"horizontalAlignment": "CENTER"
}
}
]
}
],
"range":
{
"sheetId": sheetId,
"startRowIndex": 0,
"endRowIndex": 10,
"startColumnIndex": 0,
"endColumnIndex": 1
},
"fields": "userEnteredFormat"
}
}
]
}
result = g.service.spreadsheets().batchUpdate(spreadsheetId=key, body=data).execute()
Reference :
- REST Resource: spreadsheets
If I misunderstand your question, please tell me. I would like to modify my answer.
回答2:
Use repeatCell to update at once.
{
repeatCell: {
cell: {
userEnteredFormat: {
horizontalAlignment: "CENTER",
verticalAlignment: "MIDDLE",
}
},
range: {
sheetId: 0,
startRowIndex: 0,
endRowIndex: 10,
startColumnIndex: 0,
endColumnIndex: 1,
},
fields: "userEnteredFormat"
}
}
来源:https://stackoverflow.com/questions/50874219/google-sheets-horizontal-alignment