简体   繁体   English

从 Google 电子表格单元格解析 Json

[英]Parse Json from Google spreadsheet cell

In google spreadsheet cell i have this text:在谷歌电子表格单元格中,我有这样的文字:

{"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"]};

Its JSON from Facebook API getting from Supermetrics.它来自 Facebook API 的 JSON 来自 Supermetrics。

Now i want to parse this cell, but this code doesnt work :-/现在我想解析这个单元格,但这段代码不起作用:-/

I am using this function in spreadsheet "=parseTargeting(A1)"我在电子表格“=parseTargeting(A1)”中使用这个函数

and this custom function in Script editor.以及脚本编辑器中的这个自定义函数。

 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"]+ "\n";  


    }

    var returnVek = "Vek:"+minAge + " - " + maxAge+" \n";
    var returnInterests = "Zájmy:"+interests;


    var returnString = returnVek + returnInterests;



  return returnString;

}

This function return always Undefined.此函数始终返回未定义。 If i add this code如果我添加此代码

" 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"]};
"

to function - then its work.发挥作用 - 那么它的工作。 But i need this function to get value from google spreadsheet cell dynamically.但是我需要这个函数来动态地从谷歌电子表格单元格中获取价值。

I dont get it :-/ Can you help how to parse JSON from Google Spreadsheet Cell?我不明白:-/ 你能帮助如何从谷歌电子表格单元解析 JSON 吗?

How about this sample script?这个示例脚本怎么样? For your json object, there is ;对于您的 json 对象,有; at the end of object.在对象的末尾。 By this, it cannot be parsed.通过这种方式,它无法解析。 So ;所以; is removed and parsed it.被删除并解析它。

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 :注意:

  • In this script, it supposes that your json object is in a cell "A1".在此脚本中,它假设您的 json 对象位于单元格“A1”中。
  • When you use this script, please copy and paste it to the script editor on Spreadsheet with the json object.使用此脚本时,请将其复制并粘贴到带有 json 对象的电子表格上的脚本编辑器中。

If I misunderstand your question, please tell me.如果我误解了你的问题,请告诉我。 I would like to modify it.我想修改它。

Edit :编辑:

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"]+ "\n";  
    }
    var returnVek = "Vek:"+minAge + " - " + maxAge+" \n";
    var returnInterests = "Zájmy:"+interests;
    Logger.log(returnInterests);
    var returnString = returnVek + returnInterests;
  } else {
    var returnString = "No data";
  }
  return returnString; // Added
}

Note :注意:

  • For your sample sheet, the values of "A4" and "A7" occur an error at var interestsBasics = content["flexible_spec"][0]["interests"][0]["name"];对于您的样本表,“A4”和“A7”的值在var interestsBasics = content["flexible_spec"][0]["interests"][0]["name"]; . . Because there is no property of flexible_spec in the value.因为值中没有flexible_spec属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM