![](/img/trans.png)
[英]Is there an issue on python 3.8 with gsheet batch_update?
[英]How to insert row and change cell styles using batch_update in gspread?
我正在使用gspread
和gspread_formatting
在 Google 中更新我的工作表。 我的一些代碼庫已經改寫為使用batch_update
,因為我在另一個答案中找到了一些代碼示例,我可以將其用作參考。 但是,我似乎無法轉換其他兩個操作。 這是我的代碼:
import gspread
from gspread_formatting import *
from oauth2client.service_account import ServiceAccountCredentials
def operate():
scope = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive'
]
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'creds.json',
scope
)
gc = gspread.authorize(credentials)
sh = gc.open('My spreadsheet')
ws = sh.sheet1
sheet_id = ws._properties['sheetId']
# Setting the column sizes
body = {
'requests': [
{
'updateDimensionProperties': {
'range': {
'sheetId': sheet_id,
'dimension': 'COLUMNS',
'startIndex': 0,
'endIndex': 10
},
'properties': {
'pixelSize': 150
},
'fields': 'pixelSize'
}
},
{
'updateDimensionProperties': {
'range': {
'sheetId': sheet_id,
'dimension': 'COLUMNS',
'startIndex': 4,
'endIndex': 6
},
'properties': {
'pixelSize': 250
},
'fields': 'pixelSize'
}
}
]
}
res = sh.batch_update(body)
# Request 1
ws.insert_row(['One', 'Two', 'Three'], 1)
# Request 2
format_cell_range(
ws, 'A1:Z7',
cellFormat(
wrapStrategy='WRAP',
verticalAlignment='MIDDLE',
backgroundColor=color(0.886, 0.945, 0.988),
textFormat=textFormat(
foregroundColor=color(0, 0.129, 0.443),
fontFamily='Roboto',
bold=True
)
)
)
所以,我想要做的是以某種方式將請求 1 和請求 2 添加到bulk_update
方法中。 是否可以使用bulk_update
插入一行並更改格式? 如果是,我該怎么做? 謝謝你的幫助。
['One', 'Two', 'Three']
的值放入插入的行。 您想使用batch_update()
方法將以下單元格格式設置為“A1:Z7”范圍。
wrapStrategy='WRAP', verticalAlignment='MIDDLE', backgroundColor=gsf.color(0.886, 0.945, 0.988), textFormat=gsf.textFormat( foregroundColor=gsf.color(0, 0.129, 0.443), fontFamily='Roboto', bold=True )
您想使用 gspread 和 python 來實現這一點。
您的目標是將以下腳本轉換為batch_update()
# Request 1 ws.insert_row(['One', 'Two', 'Three'], 1) # Request 2 format_cell_range( ws, 'A1:Z7', cellFormat( wrapStrategy='WRAP', verticalAlignment='MIDDLE', backgroundColor=color(0.886, 0.945, 0.988), textFormat=textFormat( foregroundColor=color(0, 0.129, 0.443), fontFamily='Roboto', bold=True ) ) )
您已經能夠使用 Sheets API 獲取和放置電子表格的值。
如果我的理解是正確的,這個答案怎么樣? 請將此視為幾種可能的答案之一。
在這種情況下, batch_update()
的請求體的batch_update()
如下。
insertDimension
將新行插入到第一行。['One', 'Two', 'Three']
的值放入updateCells
插入的行中。repeatCell
將單元格格式設置為“A1:Z7”的repeatCell
。spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "###" # Please set the sheet name.
sh = gc.open_by_key(spreadsheetId)
ws = sh.worksheet(sheetName)
sheetId = ws._properties['sheetId']
requests = {
"requests": [
{
"insertDimension": {
"range": {
"sheetId": sheetId,
"startIndex": 0,
"dimension": "ROWS",
"endIndex": 1
}
}
},
{
"updateCells": {
"range": {
"sheetId": sheetId,
"startRowIndex": 0,
"endRowIndex": 1
},
"rows": [
{
"values": [
{
"userEnteredValue": {
"stringValue": "One"
}
},
{
"userEnteredValue": {
"stringValue": "Two"
}
},
{
"userEnteredValue": {
"stringValue": "Three"
}
}
]
}
],
"fields": "userEnteredValue"
}
},
{
"repeatCell": {
"range": {
"sheetId": sheetId,
"startRowIndex": 0,
"endRowIndex": 7,
"startColumnIndex": 0,
"endColumnIndex": 26
},
"cell": {
"userEnteredFormat": {
"wrapStrategy": "WRAP",
"verticalAlignment": "MIDDLE",
"backgroundColor": {
"red": 0.886,
"green": 0.945,
"blue": 0.988
},
"textFormat": {
"foregroundColor": {
"red": 0,
"green": 0.129,
"blue": 0.443
},
"fontFamily": "Roboto",
"bold": True
}
}
},
"fields": "userEnteredFormat"
}
}
]
}
res = sh.batch_update(requests)
bulk_update
是batch_update
。如果我誤解了您的問題並且這不是您想要的結果,我深表歉意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.