0

I'm having some difficulty with the following formula in Google Scripts. What I would like to achieve, is:

1- get Number from A1

2- from Number, have multiple returns

3- set those returns in the same row (A) as original Number, in neighboring cells (A2, A3)

What it looks like now:

function GETALLTWO(bid) {
  if (input.map) {     
    return input.map(GETALLTWO)
  } else {
    return input;
  }
  var api_str = "client_id=randomstringofnumbers&client_secret=anotherrandomstringofnumbers";
  var url= "https://api.untappd.com/v4/beer/info/" + bid + "?" + api_str;
  var response = UrlFetchApp.fetch(url);
  if (response.getResponseCode() == 200) {
    var respObj = JSON.parse(response.getContentText());
    var beer = respObj.response.beer;
    if (beer.bid == bid) {
      return beer.beer_name;
      return beer.rating_score;
      return beer.stats.total_user_count;
      return beer.rating_count;
      return beer.stats.monthly_count;
      return beer.created_at;
    }
  }
}

From Code.gs, I can get the first Return to work, but am I combining the different functions correctly? And how would I set the location to return?

Hope someone can help, would love to learn more about scripting.

// Edit for future reference:

function getAll(bid) {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Overview')
  var row=sh.getActiveCell().getRow();
  var bid=sh.getRange(row,3).getValue();
  var api_str =     "client_id=randomstring&client_secret=randomstring";
  var url= "https://api.untappd.com/v4/beer/info/" + bid + "?" +     api_str;
  var response = UrlFetchApp.fetch(url);
  if (response.getResponseCode() == 200) {
    var respObj = JSON.parse(response.getContentText());
    var beer = respObj.response.beer;
    if (beer.bid == bid) {
      var rObj= {name:beer.beer_name,rating:beer.rating_score,totalcount:beer.stats.total_user_count,ratingcount:beer.rating_count,monthlycount:beer.stats.monthly_count,createddate:beer.created_at};
      values=[[rObj.name,rObj.ratingcount,rObj.rating,rObj.totalcount,rObj.monthlycount,rObj.createddate]];
      return values;
    }
  }
}
1
  • Same row or column? A2,A3 is same column. A1,B1 is same row Commented Feb 21, 2020 at 18:31

1 Answer 1

1

With this function you just need to put your cursor on the row that you want to get the bid from. It will put the data back in that row starting at column D

function getAllTwo() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Your Sheet Name')
  var row=sh.getActiveCell().getRow();
  var bid=sh.getRange(row,3).getValue();
  var api_str = "client_id=randomstringofnumbers&client_secret=anotherrandomstringofnumbers";
  var url= "https://api.untappd.com/v4/beer/info/" + bid + "?" + api_str;
  var response = UrlFetchApp.fetch(url);
  if (response.getResponseCode() == 200) {
    var respObj = JSON.parse(response.getContentText());
    var beer = respObj.response.beer;
    if (beer.bid == bid) {
      var rObj= {name:beer.beer_name,rating:beer.rating_score,totalcount:beer.stats.total_user_count,ratingcount:beer.rating_count,monthlycount:beer.stats.monthly_count,createdate:beer.created_at};
      values=[[rObj.name,rObj.ratingcount,rObj.rating,rObj.totalcount,rObj.monthlycount,rObj.createddate]];
      sh.getRange(row,4,values.length,values[0].length).setValues(values);
    }
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, I left the input.map out of the script. I thought it was necessary for an array. Now I'm wondering where this leaves the results; the function GETALLTWO(bid) takes the number from a cell. I would like the function to display the Return in the cells in the same row. How would I go about this? Now I only get a blank cell.
Can you share an image of your spreadsheet? Not the spreadsheet just an image.
What's the sheet name? How about if we just go get the data from the sheet on the row where your cursor is when you run the function then you can return it back to that same row.
I just went ahead and assumed it would be okay to do it that way. Just take a look at my answer. If you don't agree with my method then suggest one of your own and I'll do it your way.
Thank you so much Cooper, you've been a great help; I'm slowly understanding a bit more now!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.