我试图从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;
}但这似乎行不通。任何我可以尝试的建议都将不胜感激。谢谢
发布于 2012-08-02 14:01:47
一个解决方案是
if (parsedResponse.items[i].summary == undefined) {
Do something here;
}或
if (parsedResponse.items[i].summary == null) {
Do something here;
}发布于 2012-08-02 13:15:16
这应该是可行的:
if (typeof(parsedResponse.items[i].summary) === "undefined") {发布于 2017-01-02 19:51:54
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;
}https://stackoverflow.com/questions/11771384
复制相似问题