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())
添加回答
舉報