我已经离线测试了我的javascript代码,它运行正常。但是,当我在谷歌脚本中尝试相同的代码时,它与离线的工作方式不同。代码是这样的:
function myfunction(){
Data = [
{
"prefix": [
{
"prefix": "rr",
"uri": "http://www.w3.org/ns/r2rml#"
},
{
"prefix": "rdf",
"uri": "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
},
{
"prefix": "xsd",
"uri": "http://www.w3.org/2001/XMLSchema#"
},
{
"prefix": "rdfs",
"uri": "http://www.w3.org/2000/01/rdf-schema#"
},
{
"prefix": "domain",
"uri": "http://example.com/"
}
],
"rr:subjectMap": [
{
"rr:class": "Person"
},
{
"rr:template": "notemplate"
}
],
"predicatesObjectMapsArray": [
{
"rr:predicateObjectMap": [
{
"rr:predicate": "firstName"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 1
}
]
}
]
},
{
"rr:predicateObjectMap": [
{
"rr:predicate": "lastName"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 2
}
]
}
]
},
{
"rr:predicateObjectMap": [
{
"rr:predicate": "id"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 3
}
]
}
]
},
{
"rr:predicateObjectMap": [
{
"rr:predicate": "department"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 4
}
]
}
]
},
{
"rr:predicateObjectMap": [
{
"rr:predicate": "phoneNumber"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 5
}
]
}
]
}
],
"rr:logicalTable": [
{
"rr:logicalTable": "Person"
}
]
},
{
"prefix": [
{
"prefix": "rr",
"uri": "http://www.w3.org/ns/r2rml#"
},
{
"prefix": "rdf",
"uri": "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
},
{
"prefix": "xsd",
"uri": "http://www.w3.org/2001/XMLSchema#"
},
{
"prefix": "rdfs",
"uri": "http://www.w3.org/2000/01/rdf-schema#"
},
{
"prefix": "domain",
"uri": "http://example.com/"
}
],
"rr:subjectMap": [
{
"rr:class": "Company"
},
{
"rr:template": "notemplate"
}
],
"predicatesObjectMapsArray": [
{
"rr:predicateObjectMap": [
{
"rr:predicate": "id"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 1
}
]
}
]
},
{
"rr:predicateObjectMap": [
{
"rr:predicate": "employer"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 2
}
]
}
]
},
{
"rr:predicateObjectMap": [
{
"rr:predicate": "employerid"
},
{
"rr:objectMap": [
{
"rr:datatype": "xsd:string",
"rr:template": "none",
"rr:column": 3
}
]
}
]
}
],
"rr:logicalTable": [
{
"rr:logicalTable": "Company"
}
]
}]
for (var i=0; i<Data.length; i++) {
for (var triple in Data[i]) {
Logger.log(triple);
}
}
Logger.log结果为000000000 ...
我怀疑,谷歌脚本可能(在数据中的价值)工作方式不同。或者它一般是否正常工作?我应该在不同的地方寻找错误吗?
答案 0 :(得分:0)
以下示例在GAS中按预期工作:
function myfunction(Data) {
var sum = 0;
for (var value in Data) {
sum += Data[value];
}
return sum;
}
function test_myfunction() {
var Data = [1, 2, 3];
var result = myfunction(Data);
Logger.log(6 === result); // True
}
您可以发布一个包含特定案例详情的示例吗?这将有助于我们为您提供帮助。
<强>更新强>
我无法重现这个问题。以下是我使用您的代码获得的输出: