简体   繁体   English

如何使用 Python 在 Google 电子表格中拆分特定工作表的所有合并单元格?

[英]How do I split all merged cells for a specific sheet within a Google Spreadsheet using Python?

I'm working with Google sheet API and I have defined the following functions:我正在使用 Google sheet API,并且定义了以下函数:

def service():
    secret_id_gsuite_token = 'my_token' # AWS Secret with token
    gsheet = GSheets(secret_id_gsuite_token)
    return gsheet

To update a sheet with information present in a pandas dataframe:要使用 Pandas 数据框中存在的信息更新工作表:

def df_to_googlesheet(gsheet, df, gsheetId, sheet_name):   
    output = gsheet.spreadsheets.values().update(
    spreadsheetId = gsheetId,
    valueInputOption = 'RAW',
    range = sheet_name + '!A1',
    body = dict(
        majorDimension = 'ROWS',
        values = df.T.reset_index().T.values.tolist())
    ).execute()

To make a request to merge cells to form a "block":要请求合并单元格以形成“块”:

def merge_cells_request(sheeId, start_row, end_row, start_col, end_col):
    request = [{"mergeCells": { "range": { "sheetId": sheeId,
                                           "startRowIndex": start_row,
                                           "endRowIndex": end_row,
                                           "startColumnIndex": start_col,
                                           "endColumnIndex": end_col},
                                "mergeType": "MERGE_ALL"}}]
    return request

I also have a similar function to make requests to color cells backgrounds.我也有一个类似的功能来请求为单元格背景着色。 I put all my request in a requests list and apply them running:我将所有请求放在请求列表中并应用它们运行:

gsheet.batch_update(gsheetId, requests)

This batch_update function uses the batchUpdate() method of google sheets api.这个batch_update 函数使用了google sheet api 的batchUpdate()方法。

Finally this is the function to clear the sheet:最后,这是清除工作表的功能:

def clear_sheet(gsheet, spreadsheetId, sheeId):
    clear_request = [{"updateCells": {"range": {"sheetId": sheeId},
                                      "fields": "*"}}]

    gsheet.batch_update(spreadsheetId, clear_request)

The problem is that this last function does not clear the merged cells, I want to unmerge all merged cells in order to have the sheet totally clean again, but none of the options I found online seems to fit my code...问题是最后一个函数没有清除合并的单元格,我想取消合并所有合并的单元格以使工作表再次完全干净,但我在网上找到的选项似乎都不适合我的代码......

I believe your goal and situation as follows.我相信你的目标和情况如下。

  • From I want to unmerge all merged cells in order to have the sheet totally clean again , you want to unmerge cells in a sheet using Sheets API.I want to unmerge all merged cells in order to have the sheet totally clean again ,您想使用 Sheets API 取消合并工作表中的单元格。
  • You want to achieve this using googleapis for python.您想使用 googleapis for python 来实现这一点。
  • You have already been able to get and put values for Google Spreadsheet using Sheets API.您已经能够使用 Sheets API 获取和放置 Google 电子表格的值。

In this case, I would like to propose to use UnmergeCellsRequest of the batchUpdate method in Sheets API.在这种情况下,我想建议使用 Sheets API 中的 batchUpdate 方法的 UnmergeCellsRequest。

The request body is as follows.请求正文如下。

{
  "requests": [
    {
      "unmergeCells": {
        "range": {
          "sheetId": sheeId
        }
      }
    }
  ]
}
  • Although I'm not sure about your whole script, from your following script虽然我不确定您的整个脚本,但从您的以下脚本

     def clear_sheet(gsheet, spreadsheetId, sheeId): clear_request = [{"updateCells": {"range": {"sheetId": sheeId}, "fields": "*"}}] gsheet.batch_update(spreadsheetId, clear_request)
    • In this case, the request of gsheet.batch_update() might be as follows.在这种情况下, gsheet.batch_update()的请求可能如下所示。

       [{"unmergeCells": {"range": {"sheetId": sheeId}}}]

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 中的 API 重命名 Google Sheets 电子表格中的(工作)工作表? - How do I rename a (work)sheet in a Google Sheets spreadsheet using the API in Python? Python + 谷歌表 | 如何更新特定单元格 - Python + Google Sheet | How to Update specific cells 如何使用python在CSV文件中拆分合并的单元格 - How to split merged cells in a CSV file using python 如何使用 Python 拆分合并的 Excel 单元格? - How to split merged Excel cells with Python? 如何使用 Google 表格 API 在电子表格中创建新表格 - How to create a new sheet within a spreadsheet using Google Sheets API 如何使用 Python 更改 Google 表格的特定行? - How do you change a specific row of a Google Sheet using Python? 如何使用其API迭代解析Google电子表格中每个“子工作表”中的数据? - How do I iteratively parse data from each “sub-sheet” in a Google spreadsheet using their API? 如何使用openpyxl在python中读取合并的单元格? - How to read merged cells in python using openpyxl? 如何使用 python 在 google 表中获取 append 数据? - How do I append data in google sheet using python? 如何使用 Python 中的 Pygsheets 库合并 google sheet 中的单元格 - How to merge cells in google sheet using Pygsheets library in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM