Skip to main content
 首页 » 编程设计

python之更新整个工作表的单行格式

2024年12月31日9www_RR

我只想应用来自 JSON 条目的格式。我做的第一件事是在我的电子表格上为所有列的第二行制作我想要的格式。然后我使用 .get 请求(从 A2 到 AO3)检索它们。

request = google_api.service.spreadsheets().get( 
    spreadsheetId=ss_id, 
    ranges="Tab1!A2:AO3", 
    includeGridData=True).execute() 

我接下来要做的是收集每列的每种格式并将它们记录在字典中。

my_dictionary_of_formats = {} 
row_values = row_1['sheets'][0]['data'][0]['rowData'][0]['values'] 
for column in range(0, len(row_values)): 
    my_dictionary_of_formats[column] = row_values[column]['effectiveFormat'] 

现在我有一个包含我所有专栏的所有有效格式的字典。我现在无法将该格式应用于每列中的所有行。我尝试了一个 batchUpdate 请求:

cell_data = { 
  "effectiveFormat": my_dictionary_of_formats[0]} 
 
row_data = { 
  "values": [ 
      cell_data 
  ] 
} 
 
update_cell = { 
  "rows": [ 
       row_data 
  ], 
  "fields": "*", 
  "range":  
    { 
      "sheetId": input_master.tab_id, 
      "startRowIndex": 2, 
      "startColumnIndex": 0, 
      "endColumnsIndex": 1 
    } 
} 
 
request_body = { 
  "requests": [ 
      {"updateCells": update_cell}], 
  "includeSpreadsheetInResponse": True, 
  "responseIncludeGridData": True} 
 
service.spreadsheets().batchUpdate(spreadsheetId=my_id, body=request_body).execute() 

这消灭了一切,我不确定为什么。我不认为我理解 fields='* 属性。

长话短说 我想将格式应用于单个列中的所有行。就像我在第二行第一列上使用“Paint Format”工具并将其一直拖到最后一行一样。

-----更新

您好,感谢评论,这是我的解决方案:

###collect all formats from second row 
import json 
row_2 = goolge_api.service.spreadsheets().get( 
    spreadsheetId=spreadsheet_id, 
    ranges="tab1!A2:AO2", 
    includeGridData=True).execute() 
my_dictionary = {} 
row_values = row_2['sheets'][0]['data'][0]['rowData'][0]['values'] 
for column in range(0,len(row_values)): 
    my_dictionary[column] = row_values[column] 
json.dumps(my_dictionary,open('config/format.json','w')) 
 
###Part 2, apply formats 
requests = [] 
my_dict = json.load(open('config/format.json')) 
for column in my_dict: 
    requests.append( 
 
    { 
      "repeatCell": { 
        "range": { 
            "sheetId": tab_id, 
            "startRowIndex": str(1), 
            "startColumnIndex":str(column), 
            "endColumnIndex":str(int(column)+1) 
    }, 
       "cell": { 
          "userEnteredFormat": my_dict[column] 
    }, 
        'fields': "userEnteredFormat({})".format(",".join(my_dict[column].keys())) 
        } 
    }) 
 
body = {"requests": requests} 
google_api.service.spreadsheets().batchUpdate(spreadsheetId=s.spreadsheet_id,body=body).execute() 

请您参考如下方法:

当您将 fields 作为请求的一部分包含时,您向 API 端点表明它应该使用在您上传的资源中找到的信息覆盖目标范围内的指定字段。 fields="*" 相应地被解释为“此请求指定给定范围的全部数据和元数据。从该范围中删除任何以前的数据和元数据,并使用提供的内容。”

因此,您的 updateCells 请求中未指定的任何内容都将从请求中提供的 range 中删除(例如值、公式、数据验证等)。

您可以在 guide to batchUpdate 中了解更多信息

对于 updateCell request , fields 参数描述如下:

The fields of CellData that should be updated. At least one field must be specified. The root is the CellData; 'row.values.' should not be specified. A single "*" can be used as short-hand for listing every field.

如果再查看CellData的资源描述,您会观察到以下字段:

  • “用户输入值”
  • “有效值(value)”
  • “格式化值”
  • “用户输入格式”
  • “有效格式”
  • “超链接”
  • “注意”
  • “textFormatRuns”
  • “数据验证”
  • “数据透视表”

因此,您的请求的正确字段规范可能是 fields="effectiveFormat",因为这是您在 row_data 属性中提供的唯一字段。

也考虑使用 repeatCell request如果您只是指定一种格式。