4

我正在尝试从 RESTful 服务填充电子表格,有时没有这样的属性,所以我的电子表格中出现“未定义”,脚本停止并给出错误。我不知道如何处理它,或者跳过“未定义”部分。

这是json中的示例查询

{

"rel": "self",
"url": "https://www.sciencebase.gov/catalog/items?max=2&s=Search&q=project+
2009+WLCI&format=json&fields=title%2Csummary%2Cspatial%2Cfacets",
"total": 65,
"nextlink": {
    "rel": "next",
    "url": "https://www.sciencebase.gov/catalog/items?max=2&s=
Search&q=project+2009+WLCI&format=json&fields=title%2Csummary%2
Cspatial%2Cfacets&offset=2"
},
"items": [
    {
        "link": {
            "rel": "self",
            "url": "https://www.sciencebase.gov/catalog/item/
             4f4e4ac3e4b07f02db67875f"
        },
        "id": "4f4e4ac3e4b07f02db67875f",
        "title": "Decision-Making and Evaluation - Social and Economic 
        Evaluation Supporting Adaptive Management for WLCI",
        "summary": "Provide information on the social and economic environment 
        for the WLCI area and provide context for the biological and physical 
        aspects of this project.",
        "spatial": {
            "representationalPoint": [
                -108.585,
                42.141
            ]
        },
        "facets": [
            {
                "startDate": "2007-10-01 00:00:00",
                "projectStatus": "Active",
                "facetName": "Project",
                "_class": "ProjectFacet",
                "active": true,
                "className": "gov.sciencebase.catalog.item.facet.ProjectFacet",
                "endDate": null,
                "projectType": "Science",
                "_embeddedClassName": "gov.sciencebase.catalog.item.facet.
                ProjectFacet"
            }
        ]
    },
    {
        "link": {
            "rel": "self",
            "url": "https://www.sciencebase.gov/catalog/item
            /4f4e4ac0e4b07f02db676d57"
        },
        "id": "4f4e4ac0e4b07f02db676d57",
        "title": "Data and Information Management Products for the Wyoming 
        Landscape Conservation Initiative"
    }
]

}

由于第二个项目只有一个标题,之后没有更多内容,如果我尝试使用循环获取项目的摘要或方面等,我会得到“未定义”。我已经想到并尝试使用 if 语句,例如

if (parsedResponse.items[i].summary === "undefined") {
    Do something here;
}

但这似乎不起作用。我可以尝试的任何建议表示赞赏。谢谢

4

3 回答 3

11

一个解决方案是

if (parsedResponse.items[i].summary == undefined) {
    Do something here;
}

或者

if (parsedResponse.items[i].summary == null) {
    Do something here;
}
于 2012-08-02T06:01:46.597 回答
1

这应该有效:

if (typeof(parsedResponse.items[i].summary) === "undefined") {
于 2012-08-02T05:15:16.113 回答
0
  function getLastNonBlankColCrow(sheet,columnName, maxi) {
  var lastNonBlankColCrow = 0;
  var rangeded = sheet.getRange(columnName+1+":"+columnName+maxi).getValues();
  for (var i=0;i<=maxi; i++)
    {
        if (rangeded[i] == undefined)
         {
         }
      else
     {
       if ( rangeded[i][0]!="" ) 
        { 
        lastNonBlankColCrow = i+1;
         }
      }
   }
   return lastNonBlankColCrow;
  }
于 2017-01-02T11:51:54.220 回答