gpt4 book ai didi

python - 使用 Google API v4 添加带有数据的 Google 表格

转载 作者:太空狗 更新时间:2023-10-30 01:12:44 26 4
gpt4 key购买 nike

我正在使用 Python。我需要使用 Google API v4 在电子表格中添加工作表。我可以使用带有电子表格 ID 和 addSheet 请求的 batchUpdate 创建工作表(它返回 shetId 并创建空工作表)。但是如何在其中添加数据呢?

data = {'requests': [
{
'addSheet':{
'properties':{'title': 'New sheet'}
}
}
]}

res = service.spreadsheets().batchUpdate(spreadsheetId=s_id, body=data).execute()
SHEET_ID = res['replies'][0]['addSheet']['properties']['sheetId']

最佳答案

您可以添加此代码以在 Google 表格中写入数据。在文档中 - Reading & Writing Cell Values

Spreadsheets can have multiple sheets, with each sheet having any number of rows or columns. A cell is a location at the intersection of a particular row and column, and may contain a data value. The Google Sheets API provides the spreadsheets.values collection to enable the simple reading and writing of values.

写入单个范围

To write data to a single range, use a spreadsheets.values.update request:

values = [
[
# Cell values ...
],
# Additional rows ...
]
body = {
'values': values
}
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id, range=range_name,
valueInputOption=value_input_option, body=body).execute()

The body of the update request must be a ValueRange object, though the only required field is values. If range is specified, it must match the range in the URL. In the ValueRange, you can optionally specify its majorDimension. By default, ROWS is used. If COLUMNS is specified, each inner array is written to a column instead of a row.

写入多个范围

If you want to write multiple discontinuous ranges, you can use a spreadsheets.values.batchUpdate request:

values = [
[
# Cell values
],
# Additional rows
]
data = [
{
'range': range_name,
'values': values
},
# Additional ranges to update ...
]
body = {
'valueInputOption': value_input_option,
'data': data
}
result = service.spreadsheets().values().batchUpdate(
spreadsheetId=spreadsheet_id, body=body).execute()

The body of the batchUpdate request must be a BatchUpdateValuesRequest object, which contains a ValueInputOption and a list of ValueRange objects (one for each written range). Each ValueRange object specifies its own range, majorDimension, and the data to input.

希望这对您有所帮助。

关于python - 使用 Google API v4 添加带有数据的 Google 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40571487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com