Google App 脚本 - 映射时出错 - 类型错误:无法读取未定义的属性“地图”

在社区的指导下,我最近在这里了解了给定谷歌表格的 BatchUpdating 背景颜色。


我试图将其应用于我的实际工作表,但遇到了错误代码。TypeError: Cannot read property 'map' of undefined


我的测试表中没有问题的代码在这里:


var TestArray = Sheets.Spreadsheets.get("1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg", {

   ranges:"TestBackgroundSheet!A1:AD39", fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"

 });

 

 var backgroundColors = TestArray["sheets"][0]["data"][0]["rowData"]

                      .map(row => row["values"]

                      .map(value => value["effectiveFormat"]["backgroundColor"])); 

然后我将这段代码复制并粘贴到我的实际项目中。为了排除故障,我什至将我的测试项目中的确切工作表复制到实际项目中。代码列在这里:


var TestArray = Sheets.Spreadsheets.get("1pcIKNUFmkk0d-UGg1sXl5xbsJC2WhocIHpM3et-CMgo", {

   ranges:"TestBackgroundSheet!A1:AD39", fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"

 });

 

 var backgroundColors = TestArray["sheets"][0]["data"][0]["rowData"]

                      .map(row => row["values"]

                      .map(value => value["effectiveFormat"]["backgroundColor"]));

如您所见,除了 SheetID 不同之外,代码完全相同。尽管如此,我仍然收到TypeError: Cannot read property 'map' of undefined错误。


错误代码引用第 172 行,它是.map(value => value["effectiveFormat"]["backgroundColor"]));


拉风的咖菲猫
浏览 95回答 1
1回答

扬帆大鱼

回答:并非所有元素都有数据 - 因此当该行没有数据时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())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript