将 Google 工作表 API(含 Python)中的列设置为 number-formatted
Setting column in Google Sheets API (with Python) to be number-formatted
我正在尝试使用 API(Sheets API v.4 和 Python 3.6.1 来格式化 Google Sheets 中的一列数字,特别是).下面是我的 non-functional 代码的一部分。我知道它正在执行,因为设置了列的背景颜色,但数字仍然显示为文本,而不是数字。
换句话说,我试图获得相当于单击列 header(A、B、C 或其他)然后选择格式 -> 数字 -> 数字菜单项图形用户界面。
def sheets_batch_update(SHEET_ID,data):
print ( ("Sheets: Batch update"))
service.spreadsheets().batchUpdate(spreadsheetId=SHEET_ID,body=data).execute() #,valueInputOption='RAW'
data={
"requests": [
{
"repeatCell": {
"range": {
"sheetId": all_sheets['Users'],
"startColumnIndex": 19,
"endColumnIndex": 20
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "NUMBER",
"pattern": "#,##0",
},
"backgroundColor": {
"red": 0.0,
"green": 0.4,
"blue": 0.4
},
}
},
"fields": "userEnteredFormat(numberFormat,backgroundColor)"
}
},
]
}
sheets_batch_update(SHEET_ID, data)
问题可能是您的数据当前存储为字符串,因此不受数字格式的影响。
"userEnteredValue": {
"stringValue": "1000"
},
"formattedValue": "1000",
"userEnteredFormat": {
"numberFormat": {
"type": "NUMBER",
"pattern": "#,##0"
}
},
当您通过 UI (Format > Number > ...) 设置数字格式时,它实际上同时做了两件事:
- 正在设置数字格式。
- 如果可能,将字符串值转换为数字值。
您的 API 调用仅执行 #1,因此当前设置有字符串值的任何单元格都将保留为字符串值,因此不会受到数字格式的影响。一种解决方案是遍历受影响的值并将 stringValue
移动到 numberValue
如果单元格包含数字。
为了进一步充实 Eric Koleda 的回答,我最终用两种方法解决了这两种问题,具体取决于我如何获取 Sheet:
的数据
首先,如果我将单元格附加到sheet,我使用了一个函数:
def set_cell_type(cell_contents):
current_cell_contents=str(cell_contents).replace(',', '')
float_cell=re.compile("^\d+\.\d+$")
int_cell=re.compile("^\d+$")
if int_cell.search(current_cell_contents):
data = {"userEnteredValue": {"numberValue": int(current_cell_contents)}}
elif float_cell.search(current_cell_contents):
data = {"userEnteredValue": {"numberValue": float(current_cell_contents)}}
else:
data = {"userEnteredValue": {"stringValue": str(cell_contents)}}
return data
正确格式化单元格。这是实际执行附加操作的调用:
rows = [{"values": [set_cell_type(cell) for cell in row]} for row in daily_data_output]
data = { "requests": [ { "appendCells": { "sheetId": all_sheets['Daily record'], "rows": rows, "fields": "*", } } ], }
sheets_batch_update(SHEET_ID,data)
第二个,如果我要替换整个sheet,我做了:
#convert the ints to ints and floats to floats
float_cell=re.compile("^\d+\.\d+$")
int_cell=re.compile("^\d+$")
row_list=error_message.split("\t")
i=0
while i < len(row_list):
current_cell=row_list[i].replace(',', '') #remove the commas from any numbers
if int_cell.search(current_cell):
row_list[i]=int(current_cell)
elif float_cell.search(current_cell):
row_list[i]=float(current_cell)
i+=1
error_output.append(row_list)
然后下面要实际保存error_output到sheet:
data = {'values': [row for row in error_output]}
sheets_update(SHEET_ID,data,'Errors!A1')
这两种技术,再加上我在最初的问题中已经想出的格式化调用,起到了作用。
我正在尝试使用 API(Sheets API v.4 和 Python 3.6.1 来格式化 Google Sheets 中的一列数字,特别是).下面是我的 non-functional 代码的一部分。我知道它正在执行,因为设置了列的背景颜色,但数字仍然显示为文本,而不是数字。
换句话说,我试图获得相当于单击列 header(A、B、C 或其他)然后选择格式 -> 数字 -> 数字菜单项图形用户界面。
def sheets_batch_update(SHEET_ID,data):
print ( ("Sheets: Batch update"))
service.spreadsheets().batchUpdate(spreadsheetId=SHEET_ID,body=data).execute() #,valueInputOption='RAW'
data={
"requests": [
{
"repeatCell": {
"range": {
"sheetId": all_sheets['Users'],
"startColumnIndex": 19,
"endColumnIndex": 20
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "NUMBER",
"pattern": "#,##0",
},
"backgroundColor": {
"red": 0.0,
"green": 0.4,
"blue": 0.4
},
}
},
"fields": "userEnteredFormat(numberFormat,backgroundColor)"
}
},
]
}
sheets_batch_update(SHEET_ID, data)
问题可能是您的数据当前存储为字符串,因此不受数字格式的影响。
"userEnteredValue": {
"stringValue": "1000"
},
"formattedValue": "1000",
"userEnteredFormat": {
"numberFormat": {
"type": "NUMBER",
"pattern": "#,##0"
}
},
当您通过 UI (Format > Number > ...) 设置数字格式时,它实际上同时做了两件事:
- 正在设置数字格式。
- 如果可能,将字符串值转换为数字值。
您的 API 调用仅执行 #1,因此当前设置有字符串值的任何单元格都将保留为字符串值,因此不会受到数字格式的影响。一种解决方案是遍历受影响的值并将 stringValue
移动到 numberValue
如果单元格包含数字。
为了进一步充实 Eric Koleda 的回答,我最终用两种方法解决了这两种问题,具体取决于我如何获取 Sheet:
的数据首先,如果我将单元格附加到sheet,我使用了一个函数:
def set_cell_type(cell_contents):
current_cell_contents=str(cell_contents).replace(',', '')
float_cell=re.compile("^\d+\.\d+$")
int_cell=re.compile("^\d+$")
if int_cell.search(current_cell_contents):
data = {"userEnteredValue": {"numberValue": int(current_cell_contents)}}
elif float_cell.search(current_cell_contents):
data = {"userEnteredValue": {"numberValue": float(current_cell_contents)}}
else:
data = {"userEnteredValue": {"stringValue": str(cell_contents)}}
return data
正确格式化单元格。这是实际执行附加操作的调用:
rows = [{"values": [set_cell_type(cell) for cell in row]} for row in daily_data_output]
data = { "requests": [ { "appendCells": { "sheetId": all_sheets['Daily record'], "rows": rows, "fields": "*", } } ], }
sheets_batch_update(SHEET_ID,data)
第二个,如果我要替换整个sheet,我做了:
#convert the ints to ints and floats to floats
float_cell=re.compile("^\d+\.\d+$")
int_cell=re.compile("^\d+$")
row_list=error_message.split("\t")
i=0
while i < len(row_list):
current_cell=row_list[i].replace(',', '') #remove the commas from any numbers
if int_cell.search(current_cell):
row_list[i]=int(current_cell)
elif float_cell.search(current_cell):
row_list[i]=float(current_cell)
i+=1
error_output.append(row_list)
然后下面要实际保存error_output到sheet:
data = {'values': [row for row in error_output]}
sheets_update(SHEET_ID,data,'Errors!A1')
这两种技术,再加上我在最初的问题中已经想出的格式化调用,起到了作用。