有没有一种方法可以使用适用于Python的gspread API自动调整Google表格中的列? [英] Is there a way to autofit a column in Google Sheets using gspread API for Python?

查看:93
本文介绍了有没有一种方法可以使用适用于Python的gspread API自动调整Google表格中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以使用针对Pyton的gspread API在Google表格中处理行?自动调整效果很好,但手动更改尺寸也是可以的.

Is there a way to manipulate row with in Google Sheets using gspread API for Pyton? Autofit will be great but manually change size is ok too.

推荐答案

  • 您要使用Sheets API将列宽调整为自动调整".
  • 您想使用gspread实现这一目标.
  • 您已经能够使用gspread放置和获取Spreadsheet的值.
  • 如果我的理解是正确的,那么该示例脚本如何?请认为这只是几个答案之一.

    If my understanding is correct, how about this sample script? Please think of this as just one of several answers.

    在此示例脚本中,我对gspread的 batch_update()使用了"autoResizeDimensions".这将使用Sheets API的电子表格sheets.batchUpdate.

    In this sample script, I used "autoResizeDimensions" with batch_update() of gspread. This uses spreadsheets.batchUpdate of Sheets API.

    spreadsheetName = "###"  # Please set Spreadsheet name.
    sheetName = "###"  # Please set sheet name.
    
    ss = client.open(spreadsheetName)
    sheetId = ss.worksheet(sheetName)._properties['sheetId']
    body = {
        "requests": [
            {
                "autoResizeDimensions": {
                    "dimensions": {
                        "sheetId": sheetId,
                        "dimension": "COLUMNS",
                        "startIndex": 0,  # Please set the column index.
                        "endIndex": 2  # Please set the column index.
                    }
                }
            }
        ]
    }
    res = ss.batch_update(body)
    

    注意:

    • 在上面的示例脚本中, startIndex endIndex 分别使用 0 2 .这意味着 sheetName 的列"A"和"B"的宽度会自动调整大小.
    • 如果要使用电子表格ID,请将 ss = client.open(spreadsheetName)修改为 ss = client.open_by_key(spreadsheetId).
    • Note:

      • In the above sample script, startIndex and endIndex uses 0 and 2, respectively. This means that the width of column "A" and "B" of sheetName are automatically resized.
      • If you want to use Spreadsheet ID, please modify ss = client.open(spreadsheetName) to ss = client.open_by_key(spreadsheetId).
      • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

        If I misunderstood your question and this was not the direction you want, I apologize.

        这篇关于有没有一种方法可以使用适用于Python的gspread API自动调整Google表格中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆