如何确定 Google 工作表 API 返回值的数据类型
How to determine the data type of values returned by Google Sheets API
我正在使用 Google 工作表 API 从具有未知值的数据集中获取值。我需要弄清楚每个 returned 值(number, boolean, string
或 datetime
)的数据类型。
API 将 return 号码设为 number
。我可以检查 string
中的 boolean
值,否则 return 类型为 string
的值。我需要一种可靠的方法来确定字符串是否应为 datetime
.
类型
Google Sheets 似乎知道每个单元格的数据类型,但我无法获取 API 到 return 单元格数据类型。有谁知道我是否可以这样做,这可能吗?或者我是否需要使用正则表达式检查每个字符串是否存在可能的 datetime
格式,并在字符串匹配时将其强制转换为日期?
您可以使用 Sheets API v4 的 sheets.spreadsheets.get 检索单元格格式。在您的情况下,您可以使用 sheets(data(rowData(values(userEnteredFormat/numberFormat,userEnteredValue)),startColumn,startRow))
作为字段。
例如,当您想要检索"A1"的单元格格式时,您可以使用如下端点。
端点:
GET https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###?ranges=sheet1!a1%3Aa1&fields=sheets(data(rowData(values(userEnteredFormat%2FnumberFormat%2CuserEnteredValue))%2CstartColumn%2CstartRow))
以下结果是为 number, boolean, string and datetime
检索到的回复。
结果 1:
在此示例中,值和格式分别为“123”和数字。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"numberValue": 123
},
"userEnteredFormat": {
"numberFormat": {
"type": "NUMBER",
"pattern": "#,##0.00"
}
}
}
]
}
]
}
]
}
]
}
结果 2:
在此示例中,值和格式分别为 "true" 和自动检测的布尔值。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"boolValue": true
}
}
]
}
]
}
]
}
]
}
结果 3:
在此示例中,值和格式分别为“123”和字符串。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"stringValue": "123"
},
"userEnteredFormat": {
"numberFormat": {
"type": "TEXT"
}
}
}
]
}
]
}
]
}
]
}
结果 4:
在此示例中,值和格式分别为“2017/10/10 1:23:45”和日期时间。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"numberValue": 43018.05815972222
},
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
"pattern": "yyyy/MM/dd"
}
}
}
]
}
]
}
]
}
]
}
numberValue
是序列号。
如果我误解了你的问题,我很抱歉。
我正在使用 Google 工作表 API 从具有未知值的数据集中获取值。我需要弄清楚每个 returned 值(number, boolean, string
或 datetime
)的数据类型。
API 将 return 号码设为 number
。我可以检查 string
中的 boolean
值,否则 return 类型为 string
的值。我需要一种可靠的方法来确定字符串是否应为 datetime
.
Google Sheets 似乎知道每个单元格的数据类型,但我无法获取 API 到 return 单元格数据类型。有谁知道我是否可以这样做,这可能吗?或者我是否需要使用正则表达式检查每个字符串是否存在可能的 datetime
格式,并在字符串匹配时将其强制转换为日期?
您可以使用 Sheets API v4 的 sheets.spreadsheets.get 检索单元格格式。在您的情况下,您可以使用 sheets(data(rowData(values(userEnteredFormat/numberFormat,userEnteredValue)),startColumn,startRow))
作为字段。
例如,当您想要检索"A1"的单元格格式时,您可以使用如下端点。
端点:
GET https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###?ranges=sheet1!a1%3Aa1&fields=sheets(data(rowData(values(userEnteredFormat%2FnumberFormat%2CuserEnteredValue))%2CstartColumn%2CstartRow))
以下结果是为 number, boolean, string and datetime
检索到的回复。
结果 1:
在此示例中,值和格式分别为“123”和数字。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"numberValue": 123
},
"userEnteredFormat": {
"numberFormat": {
"type": "NUMBER",
"pattern": "#,##0.00"
}
}
}
]
}
]
}
]
}
]
}
结果 2:
在此示例中,值和格式分别为 "true" 和自动检测的布尔值。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"boolValue": true
}
}
]
}
]
}
]
}
]
}
结果 3:
在此示例中,值和格式分别为“123”和字符串。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"stringValue": "123"
},
"userEnteredFormat": {
"numberFormat": {
"type": "TEXT"
}
}
}
]
}
]
}
]
}
]
}
结果 4:
在此示例中,值和格式分别为“2017/10/10 1:23:45”和日期时间。
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"numberValue": 43018.05815972222
},
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
"pattern": "yyyy/MM/dd"
}
}
}
]
}
]
}
]
}
]
}
numberValue
是序列号。
如果我误解了你的问题,我很抱歉。