Ela*_*son 6 google-sheets google-apps-script google-sheets-formula
我有一张表,其中 Z 列中的每一行都有一个通过TAGS从 Twitter 恢复的 JSON 字符串。
Z 列中的 JSON 字符串都具有类似的结构:
{
"hashtags": [
{
"text": "Negev_Summit",
"indices": [
172,
185
]
}
],
"symbols": [],
"user_mentions": [
{
"screen_name": "JY_LeDrian",
"name": "Jean-Yves Le Drian",
"id": 1055021191,
"id_str": "1055021191",
"indices": [
69,
80
]
}
],
"urls": [],
"media": [
{
"id": 1513588335893258200,
"id_str": "1513588335893258240",
"indices": [
271,
294
],
"media_url": "http://pbs.twimg.com/media/FQFYknkXoAAxgYd.jpg",
"media_url_https": "https://pbs.twimg.com/media/FQFYknkXoAAxgYd.jpg",
"url": "https://twitter.com/yairlapid/status/1513588345468825605",
"display_url": "pic.twitter.com/dA4cBepIh2",
"expanded_url": "https://twitter.com/yairlapid/status/1513588345468825605/photo/1",
"type": "photo",
"sizes": {
"medium": {
"w": 1024,
"h": 576,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"large": {
"w": 1024,
"h": 576,
"resize": "fit"
},
"small": {
"w": 680,
"h": 383,
"resize": "fit"
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要为 Z 列中的每个 JSON 字符串提取特定值,并将它们放入 AA、AB 和 AC 列(主题标签、用户提及和 URL)中。
我已经设法用一个非常肮脏的多个 REGEXREPLACE 公式来实现这一点,但似乎没有办法更有效地实现这一点,这似乎不合逻辑:
=IFERROR("@"&JOIN(" @",SPLIT(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXEXTRACT(INDIRECT("Y"&ROW()),".*user_mentions\"":\[(.*)\],\""urls.*"),"(,\""indices\"":\[\d+,\d+\])",""),"(,\""id_str\"":\""\d+\"")",""),"(,\""id\"":\d+)",""),"(\{\""screen_name\"":\"")",""),"\"",\""name\"":\""(.){1,50}\""\}",""),",")),"")
Run Code Online (Sandbox Code Playgroud)
理想情况下,我正在寻找一个可以解析 JSON 字符串并从 JSON 的每个部分提取 1 个或多个值的脚本。例如:
对于主题标签(AA 列):
=PARSEJSON(Z1, "hashtags")
Run Code Online (Sandbox Code Playgroud)
结果:
#hashtag1 #hashtag2
Run Code Online (Sandbox Code Playgroud)
对于用户提及(AB 列):
=PARSEJSON(Z1, "user_mentions/screen_name")
Run Code Online (Sandbox Code Playgroud)
结果:
@username1 @username2
Run Code Online (Sandbox Code Playgroud)
如果有任何帮助让我朝着正确的方向前进,我将不胜感激。
如果您的主要目的是仅获取中的值,screen_name我会修改我的脚本并使用=IMPORTJSON(url, "user_mentions/screen_name")
/**
* Imports JSON data to your spreadsheet Ex: IMPORTJSON("http://myapisite.com","city/population")
* @param url URL of your JSON data as string
* @param xpath simplified xpath as string
* @customfunction
*/
function IMPORTJSON(url,xpath){
try{
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
var patharray = xpath.split("/");
for(var i=0;i<patharray.length;i++){
json = json[patharray[i]];
}
if(typeof(json) === "undefined"){
return "Node Not Available";
} else if(typeof(json) === "object"){
var tempArr = [];
for(var obj in json){
tempArr.push([obj,json[obj]]);
}
return tempArr;
} else if(typeof(json) !== "object") {
return json;
}
}
catch(err){
return "Error getting data";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9130 次 |
| 最近记录: |