简体   繁体   English

gspread 设置列类型/格式

[英]gspread set column type/format

I'm using gpread to write data into google sheet.我正在使用 gpread 将数据写入谷歌表格。 The data is written correctly, but I need to set a whole specific column to be of type "Number" so that it can be ordered by default without having to change the column format by hand each time.数据写入正确,但我需要将整个特定列设置为“数字”类型,以便默认情况下可以对其进行排序,而无需每次手动更改列格式。 This column has only numbers (besides the header).此列只有数字(标题除外)。

This is the code where I update the sheet and try to set the column type.这是我更新工作表并尝试设置列类型的代码。 The data is written correctly, but I haven't been able to correctly change the format for the column R (it treats them as strings, so it orders them like 101,50,9 instead of 9,50,101):数据写入正确,但我无法正确更改列 R 的格式(它将它们视为字符串,因此将它们排序为 101,50,9 而不是 9,50,101):

sheet.clear()

sheet.update([lol.columns.values.tolist()] + lol.values.tolist())
sheet.format("R", { "numberFormat": { "type": "NUMBER" }})

How can I change the column R format to Number?如何将列 R 格式更改为数字?

Instead of this:而不是这个:

sheet.format("R", { "numberFormat": { "type": "NUMBER" }})

Try this:尝试这个:

sheet.format("R", { "numberFormat": { "type": "NUMBER","pattern": "#,##0" }})

What I recommend when you don't know how to format something using a client library is to get its current format using the Google API.当您不知道如何使用客户端库格式化某些内容时,我建议您使用 Google API 获取其当前格式。

Eg You have a cell with the correct format of the numbers as you desire.例如,您有一个单元格,其中包含您想要的正确格式的数字。

Then using this API you fill it out as follows:然后使用此API填写如下:

spreadsheetId = Your spreadsheet ID
ranges = Your Cell reference goes here
fields = *

Execute it and then the results you'll see:执行它,然后你会看到结果:

      "data": [
        {
          "startRow": x,
          "startColumn": x,
          "rowData": [
            {
              "values": [
                {
                  "userEnteredValue": {
                    "numberValue": xxxxxxxxx
                  },
                  "effectiveValue": {
                    "numberValue": 950101
                  },
                  "formattedValue": "950,101",
                  "userEnteredFormat": {
                    "numberFormat": {
                      "type": "NUMBER",
                      "pattern": "#,##0"
                    }

And this is what you'll use:这就是您将使用的:

                    "numberFormat": {
                      "type": "NUMBER",
                      "pattern": "#,##0"

- Number format tokens - 数字格式标记

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

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