Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-formulaarray-formulasgoogle-sheets-macros

How to increment number in function parameter as we drag the cells


I had a custom function (find below) that parse jason data to googlesheets. Using function I am retrieving values using following parameters.

=IMPORTJSON("https://mirko.com/api/v3/financials/income-statement/"&C4&"","financials/0/date")

I want the values in "financials/0/date" to increment like this 1,2,3,.....x. How could I possibly do it.

/**
* 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{
    // /rates/EUR
    var res = UrlFetchApp.fetch(url);
    var content = res.getContentText();
    var json = JSON.parse(content);

    var patharray = xpath.split("/");
    //Logger.log(patharray);

    for(var i=0;i<patharray.length;i++){
      json = json[patharray[i]];
    }

    //Logger.log(typeof(json));

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

}

Solution

  • Assuming you are using this formula in row 4, you can try

    =IMPORTJSON("https://mirko.com/api/v3/financials/income-statement/"&C4,"financials/"&row(C4)-4&"/date")
    

    and see if that helps?