Python 数据框到 Google Sheet:冻结行并围绕一组列绘制垂直线

Python dataframe to Google Sheet: Freeze rows and sketch vertical lines around a sets of columns

我有一个数据框 df_final(这是一个时间序列数据),如所附照片所示。我可以使用下面的代码将此数据框写入 Google Sheet。

问题:

  1. 在 Google Sheet 中,我想在整个数据框周围绘制垂直线

  2. 接下来我还要在C,D,E,F列和G,H,I,J两列画竖线,如图。

  3. 最后,我想冻结第 7 行(即 .. 在数据帧的 header 处)

清除 Google Sheet:

service.spreadsheets().values().clear(
    spreadsheetId=spreadsheet_id,
    range='A1:AZ2600',
    body={}
).execute()

将df_final写入Google Sheet:

cell_range_insert= 'B7'
values = df_final.to_json()
body = {'values': values}
response_date= service.spreadsheets().values().append(
    spreadsheetId=spreadsheet_id,
    valueInputOption='RAW',
    range=cell_range_insert,
    body=dict(
        majorDimension= 'ROWS',
        values=df_final.T.reset_index().T.values.tolist()
    )
).execute()

在您的情况下,下面的示例脚本怎么样?在这个脚本中,我修改了 .

示例脚本:

spreadsheet_id = "###" # Please set Spreadsheet ID.
sheet_id = 0 # Please set the sheet ID.

# 1. Clear sheet. In this case, the values and the borders are removed.
service.spreadsheets().batchUpdate(
    spreadsheetId=spreadsheet_id, body={"requests": [{
        "repeatCell": {"range": {"sheetId": sheet_id}, "fields": "userEnteredValue,userEnteredFormat.borders"}
    }, {
        "updateSheetProperties": {"properties": {"sheetId": sheet_id}, "fields": "gridProperties.frozenRowCount"}
    }
    ]}).execute()

# 2. Put the values from the dataframe to Spreadsheet.
cell_range_insert = 'B7'
# values = df_final.to_json() # It seems that this is not used.
# body = {'values': values} # It seems that this is not used.
v = df_final.T.reset_index().T.values.tolist()
response_date = service.spreadsheets().values().append(
    spreadsheetId=spreadsheet_id,
    valueInputOption='RAW',
    range=cell_range_insert,
    body=dict(
        majorDimension='ROWS',
        values=v
    )
).execute()

# 3. Set the borders.
temp = -1
n = []
for index, row in df_final.iloc[:, 7:8].iterrows():
    s = ''.join(row.astype(str).tolist())
    if temp != s:
        n.append(index)
        temp = s
offset = 7
requests = [{
    "repeatCell": {
        "cell": {"userEnteredFormat": {"borders": {"top": {"style": "SOLID_THICK"}}}},
        "range": {
            "sheetId": sheet_id,
            "startRowIndex": e + offset,
            "endRowIndex": e + 1 + offset,
            "startColumnIndex": 1,
            "endColumnIndex": 10
        },
        "fields": "userEnteredFormat.borders"
    }
} for e in n]

end = len(v) + offset - 1
add_requests1 = [{
    "repeatCell": {
        "cell": {"userEnteredFormat": {"borders": {"left": {"style": "SOLID_THICK"}}}},
        "range": {
            "sheetId": sheet_id,
            "startRowIndex": 6,
            "endRowIndex": end,
            "startColumnIndex": a,
            "endColumnIndex": b
        },
        "fields": "userEnteredFormat.borders.left"
    }
} for [a, b] in [[1, 2], [2, 3], [6, 7], [10, 11]]]
add_requests2 = [{
    "repeatCell": {
        "cell": {"userEnteredFormat": {"borders": {"top": {"style": "SOLID_THICK"}}}},
        "range": {
            "sheetId": sheet_id,
            "startRowIndex": a,
            "endRowIndex": b,
            "startColumnIndex": 1,
            "endColumnIndex": 10
        },
        "fields": "userEnteredFormat.borders.top"
    }
} for [a, b] in [[6, 7], [end, end + 1]]]
add_requests3 = [{
    "updateSheetProperties": {
        "properties": {"gridProperties": {"frozenRowCount": offset}, "sheetId": sheet_id},
        "fields": "gridProperties.frozenRowCount"
    }
}]
requests.extend(add_requests1)
requests.extend(add_requests2)
requests.extend(add_requests3)
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests}).execute()
  • 当此脚本为 运行 时,边框位于“B”、“C”、“G”和“K”列的左侧。并且,边框被放置在 7 行的行的顶部和行的末尾。水平边框的中间使用 .
  • 另外,前 7 行被冻结。

参考文献: