Handling multiple rows returned by IMPORTJSON script on GoogleSheets

荒凉一梦 提交于 2021-02-11 07:41:01

问题


I am trying to populate a google sheet using an API. But the API has more than one row to be returned for a single query. Following is the JSON returned by API.

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

By using the following calls via 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")

I am able to get the following in GoogleSheets,

Whereas, the actual output according to JSON should be,

As you can see a complete row is being overwritten. How can this be fixed?


EDIT

Following is the link to sheet for viewing only.


回答1:


I believe your goal as follows.

  • You want to achieve the bottom image in your question on Google Spreadsheet.

Unfortunately, I couldn't find the method for directly retrieving the bottom image using ImportJson. So in this answer, I would like to propose a sample script for retrieving the values you expect using Google Apps Script. I thought that creating a sample script for directly achieving your goal might be simpler rather than modifying ImportJson.

Sample script:

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;
}
  • When you use this script, please put =SAMPLE(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2)) to a cell as the custom formula.

Result:

When above script is used, the following

Note:

  • In this sample script, when the structure of the JSON object is changed, it might not be able to be used. So please be careful this.

References:

  • Class UrlFetchApp
  • Custom Functions in Google Sheets


来源:https://stackoverflow.com/questions/64502611/handling-multiple-rows-returned-by-importjson-script-on-googlesheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!