简体   繁体   English

使用谷歌表 API V4 中的 python 将特定单元格格式化为最新

[英]Format specific cell to date with python in google sheet API V4

I try to format some cells to a date format in a google spreadsheet.我尝试将一些单元格格式化为谷歌电子表格中的日期格式。

The data on the cell looks like this: 09/11/2021单元格上的数据如下所示: 09/11/2021
And I try to make it looks like this: 11/2021我试着让它看起来像这样: 11/2021
Or at least format the cell with a date format.或者至少用日期格式格式化单元格。

I use batchUpdate() to update cells:我使用batchUpdate()更新单元格:
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()

What I tried:我尝试了什么:

  • Both updateCells and repeatCell but none worked. updateCellsrepeatCell都不起作用。
  • Formatting before AND after adding the value to the cell.在将值添加到单元格之后,在 AND 之前进行格式化。
  • Change cells by changing rown or coln values (in the code below)通过更改rowncoln值来更改单元格(在下面的代码中)
  • Other date formats like dd-mm-yy or mm yyyy其他日期格式,如dd-mm-yymm yyyy
  • Also read a lot of google documentation and watched their videos...还阅读了很多谷歌文档并观看了他们的视频......

updateCells body更新细胞体

        body = {
            "requests": [
                {
                'updateCells': {
                    'range': {
                        'sheetId': sheet_id,
                        'startRowIndex': rown-1,
                        'endRowIndex': rown,
                        'startColumnIndex': coln,
                        'endColumnIndex': coln
                    },
                    'rows': {'values': [{'userEnteredFormat': {'numberFormat': {'type': "DATE", 'pattern': "mm/yyyy"}}}]},
                    'fields': 'userEnteredFormat.numberFormat'
                }
                }
            ]
        }

repeatCell body重复细胞体

         body = {
            "requests": [
                {
                    "repeatCell": {
                        "range": {
                            "sheetId": sheet_id,
                            "startRowIndex": rown-1,
                            "endRowIndex": rown,
                            "startColumnIndex": coln,
                            "endColumnIndex": coln
                        },
                        "cell": {
                            "userEnteredFormat": {
                                "numberFormat": {
                                    "type": "DATE",
                                    "pattern": "mm/yyyy"
                                }
                            }
                        },
                        "fields": "userEnteredFormat.numberFormat"
                    }
                }
            ]
        }

Any tips and tricks or ideas are welcome !欢迎任何提示和技巧或想法!

I thought that in your request body, the reason of your issue might be due to that the values of 'startColumnIndex': coln, and 'endColumnIndex': coln are the same.我认为在您的请求正文中,您的问题的原因可能是由于'startColumnIndex': coln,'endColumnIndex': coln的值相同。 So, for example, how about modifying your request bodies as follows?那么,例如,如何修改您的请求正文如下?

For updateCells body :对于updateCells body

body = {
    "requests": [
        {
            'updateCells': {
                'range': {
                    'sheetId': sheet_id,
                    'startRowIndex': rown-1,
                    'endRowIndex': rown,
                    'startColumnIndex': coln-1, # Modified
                    'endColumnIndex': coln
                },
                'rows': {'values': [{'userEnteredFormat': {'numberFormat': {'type': "DATE", 'pattern': "mm/yyyy"}}}]},
                'fields': 'userEnteredFormat.numberFormat'
            }
        }
    ]
}

For repeatCell body :对于repeatCell body

body = {
    "requests": [
        {
            "repeatCell": {
                "range": {
                    "sheetId": sheet_id,
                    "startRowIndex": rown-1,
                    "endRowIndex": rown,
                    "startColumnIndex": coln-1, # Modified
                    "endColumnIndex": coln
                },
                "cell": {
                    "userEnteredFormat": {
                        "numberFormat": {
                            "type": "DATE",
                            "pattern": "mm/yyyy"
                        }
                    }
                },
                "fields": "userEnteredFormat.numberFormat"
            }
        }
    ]
}

Note:笔记:

  • In above modified request body, for example, when the values of rown and coln are 1 and 1 , respectively, the number format of cell "A1" is modified.在上述修改后的请求体中,例如,当rowncoln的值分别为11时,修改单元格“A1”的数字格式。
  • In this modified script, it supposes that the value of 09/11/2021 in the cell is the date object.在这个修改后的脚本中,假设单元格中09/11/2021的值是日期 object。 Please be careful this.请注意这一点。

Reference:参考:

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

相关问题 使用 Python (API v4) 从 Google 表格中读取单元格格式 - Read cell format from Google sheet using Python (API v4) 如何通过Python使用SHEET API v4删除Google电子表格中的行? - How to delete rows in google spreadsheet using SHEET API v4 by Python? 使用Google API V4按值查找单元格(Google表格) - Find cell by value with google api v4 (google sheets) 谷歌表格 API v4 方法:电子表格.values.append 只更新一个单元格 - Google Sheets API v4 Method: spreadsheets.values.append only updates one cell 如果我想在本地下载一个Google表格到CSV,我需要为Google API v4定义“redirect_uris”吗? - If I want to download a Google sheet to CSV locally, do I need "redirect_uris" defined for Google API v4? 将变量传递给 batchGet() 请求 - 谷歌分析报告 API v4 / Python / 循环 - Passing a variable to a batchGet() request - Google Analytics Reporting API v4 / Python / loop 如何从Google Analytics Reporting API v4 Python获取前50个会话 - How to get Top 50 Sessions from Google Analytics Reporting API v4 Python Google Analytics API v4 如何仅过滤目标 > 0 - Google Analytics API v4 how to filter only for goals > 0 Google Sheets API v4不保存mergeCells - Google Sheets API v4 doesn't save mergeCells 谷歌表格 API v4 删除未清除行 - Google Sheets API v4 Delete not Clear Row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM