1 回答

TA贡献1799条经验 获得超9个赞
回答:
并非所有元素都有数据 - 因此当该行没有数据时row["values"]您无法运行。.map(value => value["effectiveFormat"]["backgroundColor"])
更多信息:
您已使用 API 从 API 获取数据
Sheets.Spreadsheets.get("1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg")
并使用您的字段掩码对其进行相应过滤。您作为响应获得的数据将是工作表中的所有内容- 即使是没有背景颜色数据的单元格。因此,您不能像这样映射每一行,因为您将尝试引用effectiveFormat根本不存在的元素。
使固定:
您可以使用三元运算符来解决这个问题;如果该元素value["effectiveFormat"]不存在,您可以简单地返回null:
var rowData = TestArray["sheets"][0]["data"][0]["rowData"]
.map(row => row.getValues()).toString()
var backgroundColors = JSON.parse("[" + rowData + "]")
.map(value => {
let v = value["effectiveFormat"]
return v ? v["backgroundColor"] : null
})
注意: API 还会在响应中返回 JSON 对象中的函数,您可以在 Apps 脚本中使用这些函数。这会派上用场,因为row["values"]直接引用可以返回对象而不是数据:
console.log(TestArray["sheets"][0]["data"][0]["rowData"]
.map(row => row["values"]))
产量:
[
{
setPivotTable: [Function],
getDataSourceTable: [Function],
getDataValidation: [Function],
getEffectiveValue: [Function],
setNote: [Function],
setFormattedValue: [Function],
getTextFormatRuns: [Function],
setUserEnteredFormat: [Function],
toString: [Function],
getFormattedValue: [Function],
setEffectiveFormat: [Function],
effectiveFormat: [Object],
setDataSourceFormula: [Function],
getPivotTable: [Function],
setUserEnteredValue: [Function],
setDataValidation: [Function],
setDataSourceTable: [Function],
getUserEnteredFormat: [Function],
setEffectiveValue: [Function],
getEffectiveFormat: [Function],
getHyperlink: [Function],
getNote: [Function],
setHyperlink: [Function],
getUserEnteredValue: [Function],
setTextFormatRuns: [Function],
getDataSourceFormula: [Function]
},
...
]
在将数据放回第二个映射函数之前,第一个调用通过此调用toString()。.map(row => row.getValues())
添加回答
举报