Я пытаюсь центрировать выравнивание диапазона ячеек, но только первая ячейка диапазона обновляется с указанным форматом.
Вот мой код:
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"
}
}
]
}
Если я регистрирую значения - то есть print (startRowIndex, endRowIndex, startColumnIndex, endColumnIndex)
- они верны (например, 0 1 27 30
), но только первая ячейка обновляется до формата - не весь диапазон.
Что здесь происходит? Как применить указанный формат во всем диапазоне?
Вы хотите обновить "AB1: AD1" ({startRowIndex: 0, endRowIndex: 1, startColumnIndex: 27, endColumnIndex: 30}
). Если мое понимание правильное, как насчет этой модификации?
{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",
}
}
]
}
Если я не понимаю ваш вопрос, скажите мне. Я хотел бы изменить его.
Если вы хотите отразить формат для большого количества ячеек, вы можете использовать repeatCell
. Тело запроса выглядит следующим образом. В этом примере все ячейки в диапазоне изменяются.
{
"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"
}
}
]
}
import json
import gspread из импорта oauth2client.client SignedJwtAssertionCredentials
json_key = json.load(open ('creds.json')) # json учетные данные, которые вы загрузили ранее. = [' https://spreadsheets.google.com/feeds ']
credentials = SignedJwtAssertionCredentials (json_key ['client_email'], json_key ['private_key']. encode(), scope) # получать электронную почту и ключ от creds
file = gspread.authorize (учетные данные) # аутентифицировать с помощью Google sheet = file.open("MUO_Python_Sheet"). sheet1 # open sheet
If you are writing to an important sheet, you may wish to consider a safety cell. Store a value in a certain cell (I use "Dont delete this") and then read that cell first. If the contents have changed, then columns have been added or removed in your sheet, so dont proceed writing! Heres how that could be achieved:
if sheet.acell('B3') != 'SAFETY':
# something has changed in the sheet, DO NOT PROCEED
raise Exception("Oh My, I'm not ready for this.")
else: # продолжить с вашим письмом sheet.update_acell ('C2', 'Blue')