从Google电子表格单元格中解析Json
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Google电子表格单元格中解析Json相关的知识,希望对你有一定的参考价值。
在谷歌电子表格单元格中,我有这样的文字:
{"age_max":65,"age_min":18,"flexible_spec":[{"interests":[{"id":"6002867432822","name":"Beauty"},{"id":"6002991733794","name":"Beauty & Care"},{"id":"6003177110133","name":"Natural Beauty"},{"id":"6003211042524","name":"Health and Beauty Care"},{"id":"6003393295343","name":"Health And Beauty"},{"id":"6003460329503","name":"Beautiful Skin"},{"id":"6004111438209","name":"Facial care"}]}],"genders":[2],"geo_locations":{"countries":["SK"],"location_types":["home","recent"]},"locales":[2,33],"targeting_optimization":"none","publisher_platforms":["facebook"],"facebook_positions":["feed","right_hand_column","instant_article"],"device_platforms":["mobile","desktop"]};
来自Facebook API的JSON来自Supermetrics。
现在我想解析这个单元格,但这段代码不起作用: - /
我在电子表格“= parseTargeting(A1)”中使用此功能
和脚本编辑器中的这个自定义函数
function parseTargeting(jsonData) {
var flexible_spec = jsonData["flexible_spec"];
var maxAge = jsonData["age_max"];
var minAge = jsonData["age_min"];
var interestsBasics = jsonData["flexible_spec"][0]["interests"][0]["name"];
var interestsBasicsCelkem = jsonData["flexible_spec"][0]["interests"].length-1;
var interests = "";
var output = [];
for(var i = 0; i<=interestsBasicsCelkem; i++){
interests += jsonData["flexible_spec"][0]["interests"][i]["name"]+ "
";
}
var returnVek = "Vek:"+minAge + " - " + maxAge+"
";
var returnInterests = "Zájmy:"+interests;
var returnString = returnVek + returnInterests;
return returnString;
}
此函数始终返回Undefined。如果我添加此代码
" var jsonData = {"age_max":65,"age_min":18,"flexible_spec":[{"interests":[{"id":"6002867432822","name":"Beauty"},{"id":"6002991733794","name":"Beauty & Care"},{"id":"6003177110133","name":"Natural Beauty"},{"id":"6003211042524","name":"Health and Beauty Care"},{"id":"6003393295343","name":"Health And Beauty"},{"id":"6003460329503","name":"Beautiful Skin"},{"id":"6004111438209","name":"Facial care"}]}],"genders":[2],"geo_locations":{"countries":["SK"],"location_types":["home","recent"]},"locales":[2,33],"targeting_optimization":"none","publisher_platforms":["facebook"],"facebook_positions":["feed","right_hand_column","instant_article"],"device_platforms":["mobile","desktop"]};
"
功能 - 然后它的工作。但我需要此功能来动态地从谷歌电子表格单元格中获取价值。
我不明白: - /你能帮助解析如何从Google Spreadsheet Cell解析JSON吗?
这个示例脚本怎么样?对于你的json对象,在对象的末尾有;
。这样,就无法解析。所以;
被删除并解析它。
Sample script :
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var obj = JSON.parse(ss.getRange("A1").getValue().replace(";", ""));
var res = parseTargeting(obj);
Logger.log(res)
}
Note :
- 在此脚本中,它假设您的json对象位于单元格“A1”中。
- 使用此脚本时,请使用json对象将其复制并粘贴到Spreadsheet上的脚本编辑器中。
如果我误解了你的问题,请告诉我。我想修改它。
编辑:
function parseTargeting(range) {
var content = JSON.parse(range.replace(";", "")); // Modified
if( range != "undefined" ){
var flexible_spec = content["flexible_spec"];
var maxAge = content["age_max"];
var minAge = content["age_min"];
var interestsBasics = content["flexible_spec"][0]["interests"][0]["name"];
Logger.log(interestsBasics);
var interestsBasicsCelkem = content["flexible_spec"][0]["interests"].length-1;
var interests = "";
var output = [];
for(var i = 0; i<=interestsBasicsCelkem; i++){
interests += content["flexible_spec"][0]["interests"][i]["name"]+ "
";
}
var returnVek = "Vek:"+minAge + " - " + maxAge+"
";
var returnInterests = "Zájmy:"+interests;
Logger.log(returnInterests);
var returnString = returnVek + returnInterests;
} else {
var returnString = "No data";
}
return returnString; // Added
}
Note :
- 对于样本表,“A4”和“A7”的值在
var interestsBasics = content["flexible_spec"][0]["interests"][0]["name"];
发生错误。因为价值中没有flexible_spec
的财产。
以上是关于从Google电子表格单元格中解析Json的主要内容,如果未能解决你的问题,请参考以下文章
尝试使用 API/json/google 表格从 10 个单元格中获取特定信息
是否可以使用 Google 电子表格 API 在单元格中添加评论?
在电子表格的相邻单元格中为 Google 表单的每个答案添加时间戳