Api 处理GoogleSheets上IMPORTJSON脚本返回的多行

Api 处理GoogleSheets上IMPORTJSON脚本返回的多行,api,google-apps-script,google-sheets,Api,Google Apps Script,Google Sheets,我正在尝试使用API填充google工作表。但是,对于单个查询,API要返回多行。下面是API返回的JSON # https://api.dictionaryapi.dev/api/v2/entries/en/ABANDON [ { "word": "abandon", "phonetics": [ { "text": &

我正在尝试使用API填充google工作表。但是,对于单个查询,API要返回多行。下面是API返回的JSON

# https://api.dictionaryapi.dev/api/v2/entries/en/ABANDON
[
    {
        "word": "abandon",
        "phonetics": [
            {
                "text": "/əˈbændən/",
                "audio": "https://lex-audio.useremarkable.com/mp3/abandon_us_1.mp3"
            }
        ],
        "meanings": [
            {
                "partOfSpeech": "transitive verb",
                "definitions": [
                    {
                        "definition": "Cease to support or look after (someone); desert.",
                        "example": "her natural mother had abandoned her at an early age",
                        "synonyms": [
                            "desert",
                            "leave",
                            "leave high and dry",
                            "turn one's back on",
                            "cast aside",
                            "break with",
                            "break up with"
                        ]
                    },
                    {
                        "definition": "Give up completely (a course of action, a practice, or a way of thinking)",
                        "example": "he had clearly abandoned all pretense of trying to succeed",
                        "synonyms": [
                            "renounce",
                            "relinquish",
                            "dispense with",
                            "forswear",
                            "disclaim",
                            "disown",
                            "disavow",
                            "discard",
                            "wash one's hands of"
                        ]
                    },
                    {
                        "definition": "Allow oneself to indulge in (a desire or impulse)",
                        "example": "they abandoned themselves to despair",
                        "synonyms": [
                            "indulge in",
                            "give way to",
                            "give oneself up to",
                            "yield to",
                            "lose oneself in",
                            "lose oneself to"
                        ]
                    }
                ]
            },
            {
                "partOfSpeech": "noun",
                "definitions": [
                    {
                        "definition": "Complete lack of inhibition or restraint.",
                        "example": "she sings and sways with total abandon",
                        "synonyms": [
                            "uninhibitedness",
                            "recklessness",
                            "lack of restraint",
                            "lack of inhibition",
                            "unruliness",
                            "wildness",
                            "impulsiveness",
                            "impetuosity",
                            "immoderation",
                            "wantonness"
                        ]
                    }
                ]
            }
        ]
    }
]
通过IMPORTJSON使用以下调用

=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/phonetics/text", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/partOfSpeech", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/definitions/definition", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/definitions/synonyms", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/definitions/example", "noHeaders")
我可以在谷歌的网页上找到以下信息:

鉴于,根据JSON的实际输出应为

正如您所看到的,一个完整的行正在被覆盖。如何解决这个问题


编辑


以下是链接,仅供查看。

我相信您的目标如下

  • 你想在谷歌电子表格上获得问题的底部图像
不幸的是,我找不到使用ImportJson直接检索底部图像的方法。因此,在这个答案中,我想提出一个示例脚本,用于使用GoogleApps脚本检索您期望的值。我认为创建一个直接实现目标的示例脚本可能比修改ImportJson更简单

示例脚本:
  • 使用此脚本时,请将
    =SAMPLE(CONCATENATE(“https://api.dictionaryapi.dev/api/v2/entries/en/“&$A2”)
    作为自定义公式添加到单元格
结果: 使用上述脚本时,将执行以下操作

注:
  • 在这个示例脚本中,当JSON对象的结构更改时,它可能无法使用。所以请小心这个
参考资料:

给我留下印象的人不多。祝你一切顺利,谢谢你。@Mercurial谢谢你的回复。我很高兴你的问题解决了。
function SAMPLE(url) {
  var res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  if (res.getResponseCode() != 200) return res.getContentText();

  var obj = JSON.parse(res.getContentText());
  var values = obj[0].meanings.reduce((ar, {partOfSpeech, definitions}, i) => {
    definitions.forEach(({definition, example, synonyms}, j) => {
      var v = [definition, Array.isArray(synonyms) ? synonyms.join(",") : synonyms, example];
      var phonetics = obj[0].phonetics[i];
      ar.push(j == 0 ? [(phonetics ? phonetics.text : ""), partOfSpeech, ...v] : ["", "", ...v]);
    });
    return ar;
  }, []);
  return values;
}