有没有办法使用 googlesheets api 和 gspread 格式化多个工作表
Is there a way to format multiple worksheets using the googlesheets api and gspread
我目前正在编写一个 python 程序,该程序从网站上抓取数据,然后将该信息写入 google 电子表格。根据每一行中包含的数据,数据被分成主电子表格内的不同工作表。我一直在使用 gspread 的 batch_update() 函数发送多个请求,但它只格式化 sheet1 并且不格式化后续页面。我该怎么做才能使我所有的工作表格式相同。
batch_update() 通过 googlesheets api 调用 spreadsheets.batchUpdate() 方法,这应该影响整个电子表格而不是我不影响的第一个工作表不懂
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
vt = client.open(sheetName)
formatRequests = []
formatRequests.append({
"repeatCell" : {
"range" : {
"startColumnIndex" : 0,
"endColumnIndex" : 1
},
"cell" : {
"userEnteredFormat" : {
"numberFormat" : {
"type": "DATE",
"pattern" : "mmm dd, yyyy, hh:mm am/pm"
}
}
},
"fields" : "userEnteredFormat.numberFormat"
}
})
#... A bunch of other formatting appends
body = {
'requests' : formatRequests
}
vt.batch_update(body)
这只会格式化电子表格中的第一页
- 您想为 Spreadsheet 中的所有 sheet 设置格式。
- 您想使用带 gspread 的表格 API 实现此目的。
- 您已经能够使用表格获取和放置值 API。
如果我的理解是正确的,这个修改怎么样?
修改点:
- 在此修改中,首先,从 Spreadsheet 中检索所有 sheet。然后使用检索到的 sheet ID 创建请求正文。
修改后的脚本:
请按如下方式修改您的脚本。
从:
formatRequests = []
formatRequests.append({
"repeatCell" : {
"range" : {
"startColumnIndex" : 0,
"endColumnIndex" : 1
},
"cell" : {
"userEnteredFormat" : {
"numberFormat" : {
"type": "DATE",
"pattern" : "mmm dd, yyyy, hh:mm am/pm"
}
}
},
"fields" : "userEnteredFormat.numberFormat"
}
})
到:
formatRequests = []
worksheet_list = vt.worksheets() # Added
for sheet in worksheet_list: # Added
formatRequests.append({
"repeatCell": {
"range": {
"startColumnIndex": 0,
"endColumnIndex": 1,
"sheetId": sheet.id # Added
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
"pattern": "mmm dd, yyyy, hh:mm am/pm"
}
}
},
"fields": "userEnteredFormat.numberFormat"
}
})
参考:
如果我误解了你的问题,这不是你想要的方向,我很抱歉。
我目前正在编写一个 python 程序,该程序从网站上抓取数据,然后将该信息写入 google 电子表格。根据每一行中包含的数据,数据被分成主电子表格内的不同工作表。我一直在使用 gspread 的 batch_update() 函数发送多个请求,但它只格式化 sheet1 并且不格式化后续页面。我该怎么做才能使我所有的工作表格式相同。
batch_update() 通过 googlesheets api 调用 spreadsheets.batchUpdate() 方法,这应该影响整个电子表格而不是我不影响的第一个工作表不懂
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
vt = client.open(sheetName)
formatRequests = []
formatRequests.append({
"repeatCell" : {
"range" : {
"startColumnIndex" : 0,
"endColumnIndex" : 1
},
"cell" : {
"userEnteredFormat" : {
"numberFormat" : {
"type": "DATE",
"pattern" : "mmm dd, yyyy, hh:mm am/pm"
}
}
},
"fields" : "userEnteredFormat.numberFormat"
}
})
#... A bunch of other formatting appends
body = {
'requests' : formatRequests
}
vt.batch_update(body)
这只会格式化电子表格中的第一页
- 您想为 Spreadsheet 中的所有 sheet 设置格式。
- 您想使用带 gspread 的表格 API 实现此目的。
- 您已经能够使用表格获取和放置值 API。
如果我的理解是正确的,这个修改怎么样?
修改点:
- 在此修改中,首先,从 Spreadsheet 中检索所有 sheet。然后使用检索到的 sheet ID 创建请求正文。
修改后的脚本:
请按如下方式修改您的脚本。
从:formatRequests = []
formatRequests.append({
"repeatCell" : {
"range" : {
"startColumnIndex" : 0,
"endColumnIndex" : 1
},
"cell" : {
"userEnteredFormat" : {
"numberFormat" : {
"type": "DATE",
"pattern" : "mmm dd, yyyy, hh:mm am/pm"
}
}
},
"fields" : "userEnteredFormat.numberFormat"
}
})
到:
formatRequests = []
worksheet_list = vt.worksheets() # Added
for sheet in worksheet_list: # Added
formatRequests.append({
"repeatCell": {
"range": {
"startColumnIndex": 0,
"endColumnIndex": 1,
"sheetId": sheet.id # Added
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
"pattern": "mmm dd, yyyy, hh:mm am/pm"
}
}
},
"fields": "userEnteredFormat.numberFormat"
}
})
参考:
如果我误解了你的问题,这不是你想要的方向,我很抱歉。