【问题标题】:Format specific cell to date with python in google sheet API V4在 google sheet API V4 中使用 python 格式化特定单元格到日期
【发布时间】:2021-04-07 17:35:24
【问题描述】:

我尝试在谷歌电子表格中将一些单元格格式化为日期格式。

单元格上的数据如下所示:09/11/2021
我试着让它看起来像这样:11/2021
或者至少用日期格式格式化单元格。

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

我尝试了什么:

  • updateCells 和 repeatCell 均无效。
  • 将值添加到单元格之后,在 AND 之前进行格式化。
  • 通过更改 rown 或 coln 值(在下面的代码中)来更改单元格
  • 其他日期格式,如dd-mm-yy 或mm yyyy
  • 还阅读了大量谷歌文档并观看了他们的视频...

updateCells 正文

        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'
                }
                }
            ]
        }

重复细胞体

         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"
                    }
                }
            ]
        }

欢迎任何提示和技巧或想法!

【问题讨论】:

  • 在batchUpdate()中使用json.dumps()之前是否尝试过使用body?

标签: python-3.x google-sheets formatting google-sheets-api


【解决方案1】:

我认为在您的请求正文中,您的问题的原因可能是由于'startColumnIndex': coln, 和'endColumnIndex': coln 的值相同。那么,例如,如何修改您的请求正文如下?

对于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'
            }
        }
    ]
}

对于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"
            }
        }
    ]
}

注意:

  • 在上面修改的请求体中,例如rown和coln的值分别为1和1时,修改了单元格“A1”的数字格式。
  • 在这个修改后的脚本中,它假设单元格中09/11/2021 的值是日期对象。请注意这一点。

参考:

【讨论】:

  • 感谢您的回答@Tanaike!我试图用 -1 更改起始值,但没有任何改变。我还尝试按照谷歌文档中的建议删除行的起始索引,但仍然没有看到任何变化。我打印了 coln 和 rown 的值,它们看起来不错
  • @Bruuuh 感谢您的回复。我带来的不便表示歉意。首先,在我的回答中,它假设09/11/2021 的单元格值是日期对象。这个怎么样?如果您对此无法理解,您能否提供用于复制当前问题的示例电子表格?借此,我想确认一下。因为当我测试上述修改时,我可以确认脚本有效并且可以修改数字格式。对于这种情况,我深表歉意。
  • 很好,你是对的!日期的格式不好,我正在发送字符串。我直接用datetime.strptime(myDate, "%d/%m/%Y").strftime("%d/%m/%y")之类的东西替换了它,这是有效的!非常感谢@Tanaike :)
  • @Bruuuh 感谢您回复并添加更多信息。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多