无法使用 Python 在 Gsheet 中发布的选定数据框列周围绘制垂直线和水平线

Unable to sketch vertical and horizontal lines around selected dataframe columns published in Gsheet using Python

根据关于 的讨论,我已经能够使用下面的代码(谢谢 @Tanaike!)并在我发布到 [=] 的数据框 df_final 周围绘制垂直和水平边界38=] Sheet 在单元格 B7 使用 python.

请补充我遇到的 2 个问题:

  1. 从附图中可以看出,线条并没有一直延伸到AE列,而是停在了Z(见照片中的红线)。我试图编辑代码并使 endColumnIndex 高于 26 但它没有帮助。

  2. 如何使 sheet 的内容居中对齐?我已经自动调整它们,如下面的代码所示。

def Publish_Gsheet(df_final, service, spreadsheet_id, sheet_id):

### Clear the Google Sheet:
service.spreadsheets().batchUpdate(
    spreadsheetId=spreadsheet_id, body={"requests": [{
        "repeatCell": {
            "range": {
                "sheetId": sheet_id
            },
            "fields": "userEnteredValue,userEnteredFormat.borders"
        }
    }
    ]}).execute()
##########################


###  Write df_final to Google Sheet:
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()
###################################

### Autofit Columns and Rows width:
request_body = {
    'requests': [
        {
            'autoResizeDimensions': {
                'dimensions': {
                    'sheetId': sheet_id,
                    'dimension': 'COLUMNS',
                    'startIndex': 0,
                    'endIndex': 26
                }
            }
        },
        {
            'autoResizeDimensions': {
                'dimensions': {
                    'sheetId': sheet_id,
                    'dimension': 'ROWS',
                    'startIndex': 0,
                    'endIndex': 1000
                }
            }
        }
    ]
}


##### Sketching horizontal rows after each Week:
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": 26
        },
        "fields": "userEnteredFormat.borders"
    }
} for e in n]

##### Sketching vertical lines in between predefined columns:
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], [15, 16], [17, 18], [21,22], [22,23], [23,24], [27, 28]]]
add_requests2 = [{
    "repeatCell": {
        "cell": {"userEnteredFormat": {"borders": {"top": {"style": "SOLID_THICK"}}}},
        "range": {
            "sheetId": sheet_id,
            "startRowIndex": a,
            "endRowIndex": b,
            "startColumnIndex": 1,
            "endColumnIndex": 26
        },
        "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()

不幸的是,虽然我不确定我是否能正确理解你的问题,但从你提供的问题中,我理解你的目标如下。

  1. 关于As can be seen in the attached photo, the lines dont extend all the way to the column AE, but rather stop at Z (see red lines in photo). I tried to edit the code and make endColumnIndex higher than 26 but it doesnt help.,在本例中,您只想修改垂直线。不需要修改水平线。您想将行放在“AE”列的右侧。

  2. 关于How do I make the contents of the sheet to be center alligned? I already auto fit them as shown in code below.,您想为sheet的所有单元格设置此项。

  3. 关于Yes I need both horizontal and vertical lines which are missing (red lines). For center alignment: I would like that for the entire sheet.,当我看到你的图片时,红线只是水平线。从您的图像中,您可能希望将垂直线放在“AA”和“AB”列之间。并且,您可能希望将水平线从“B”列左侧放置到“AE”列右侧。而且,您可能希望在整个 sheet.

    中反映居中对齐

如果我对你的2个目标的理解是正确的,下面的修改怎么样?

修改后的脚本:

在这种情况下,请修改### Autofit Columns and Rows width:行下方的显示脚本,如下所示。

### Autofit Columns and Rows width:
request_body = {
    "requests": [
        {
            "autoResizeDimensions": {
                "dimensions": {
                    "sheetId": sheet_id,
                    "dimension": "COLUMNS",
                    "startIndex": 0,
                    "endIndex": 31,
                }
            }
        },
        {
            "autoResizeDimensions": {
                "dimensions": {
                    "sheetId": sheet_id,
                    "dimension": "ROWS",
                    "startIndex": 0,
                    "endIndex": 1000,
                }
            }
        },
    ]
}

##### Sketching horizontal rows after each Week:
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": 31,
            },
            "fields": "userEnteredFormat.borders",
        }
    }
    for e in n
]

##### Sketching vertical lines in between predefined columns:
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],
        [15, 16],
        [17, 18],
        [21, 22],
        [22, 23],
        [23, 24],
        [27, 28],
        [31, 32],
    ]
]
add_requests2 = [
    {
        "repeatCell": {
            "cell": {
                "userEnteredFormat": {"borders": {"top": {"style": "SOLID_THICK"}}}
            },
            "range": {
                "sheetId": sheet_id,
                "startRowIndex": a,
                "endRowIndex": b,
                "startColumnIndex": 1,
                "endColumnIndex": 31,
            },
            "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",
        }
    }
]

add_requests4 = [
    {
        "repeatCell": {
            "cell": {"userEnteredFormat": {"horizontalAlignment": "CENTER"}},
            "fields": "userEnteredFormat.horizontalAlignment",
            "range": {"sheetId": sheet_id},
        }
    }
]

requests.extend(add_requests1)
requests.extend(add_requests2)
requests.extend(add_requests3)
requests.extend(add_requests4)
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests}).execute()
  • 我认为在你的目标中,endColumnIndex 应该是 31
  • 为了设置单元格值的居中对齐,我添加了add_requests4