简体   繁体   English

有没有办法使用 googlesheets api 和 gspread 格式化多个工作表

[英]Is there a way to format multiple worksheets using the googlesheets api and gspread

I'm currently writing a python program which scrapes data from a website and then writes that info into a google spreadsheet.我目前正在编写一个 python 程序,它从网站上抓取数据,然后将该信息写入谷歌电子表格。 Based on the data contained in each row the data is separated into different worksheets inside of the main spreadsheet.根据每行中包含的数据,数据被分成主电子表格内的不同工作表。 I've been sending multiple requests using gspread's batch_update() function but it only formats sheet1 and doesn't format the subsequent pages.我一直在使用 gspread 的 batch_update() 函数发送多个请求,但它只格式化 sheet1 而没有格式化后续页面。 What can I do to make all of my sheets be formatted the same.我该怎么做才能使所有工作表的格式相同。

batch_update() calls the spreadsheets.batchUpdate() method through the googlesheets api which should affect the whole spreadsheet instead of the first worksheet which I don't understand batch_update() 通过 googlesheets api 调用电子表格.batchUpdate() 方法,这应该影响整个电子表格而不是我不理解的第一个工作表

creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
vt = client.open(sheetName)

formatRequests = []
formatRequests.append({
        "repeatCell" : {
            "range" : {
                "startColumnIndex" : 0,
                "endColumnIndex" : 1
            },
            "cell" : {
                "userEnteredFormat" : {
                    "numberFormat" : {
                        "type": "DATE",
                        "pattern" : "mmm dd, yyyy, hh:mm am/pm"
                    }
                }
            },
            "fields" : "userEnteredFormat.numberFormat"
        }
    })

#... A bunch of other formatting appends

body = {
        'requests' : formatRequests
    }
vt.batch_update(body)

This only formats the first page in the spreadsheet这仅格式化电子表格中的第一页

  • You want to set the format for all sheets in the Spreadsheet.您想为电子表格中的所有工作表设置格式。
  • You want to achieve this using Sheets API with gspread.您想使用带有 gspread 的 Sheets API 来实现这一点。
  • You have already been able to get and put values using Sheets API.您已经能够使用 Sheets API 获取和放置值。

If my understanding is correct, how about this modification?如果我的理解是正确的,这个修改怎么样?

Modification point:修改点:

  • In this modification, at first, all sheets are retrieved from the Spreadsheet.在此修改中,首先从电子表格中检索所有工作表。 Then the request body is created using the retrieved sheet IDs.然后使用检索到的工作表 ID 创建请求正文。

Modified script:修改后的脚本:

Please modify your script as follows.请按如下方式修改您的脚本。

From: 从:
 formatRequests = [] formatRequests.append({ "repeatCell" : { "range" : { "startColumnIndex" : 0, "endColumnIndex" : 1 }, "cell" : { "userEnteredFormat" : { "numberFormat" : { "type": "DATE", "pattern" : "mmm dd, yyyy, hh:mm am/pm" } } }, "fields" : "userEnteredFormat.numberFormat" } })
To: 到:
 formatRequests = [] worksheet_list = vt.worksheets() # Added for sheet in worksheet_list: # Added formatRequests.append({ "repeatCell": { "range": { "startColumnIndex": 0, "endColumnIndex": 1, "sheetId": sheet.id # Added }, "cell": { "userEnteredFormat": { "numberFormat": { "type": "DATE", "pattern": "mmm dd, yyyy, hh:mm am/pm" } } }, "fields": "userEnteredFormat.numberFormat" } })

Reference:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM