0

I am very new to excel but I have been coding for a couple of years. Now I have created a function which returns an array of objects.

function sheetnames() {
  var out = new Array()
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for (var i=0 ; i<sheets.length ; i++){
    let totalScore = sheets[i].getRange("C2").getValue();

    if(i == 0 || i == 1){
      continue;
    }
    console.log(sheets[i].getName(), totalScore);
    out.push({name: sheets[i].getName(), score: totalScore});  
  } 
  return out.sort((a)=> a.totalScore);
}

I want to call this function and write Name in A1 and score in B1 and then next row Name in A2 and score in B2 and so on and so on. I'm calling my function in excel using =sheetnames() which i got to work before when I only returned an array with strings.

Thanks!

1 Answer 1

1

I solved it by updating the excelsheet straigt from the code instead of calling the function from the acctual excel.

function sheetnames() {
  resetStandingsSheet();
  var out = new Array()
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for (var i=0 ; i<sheets.length ; i++){
    let totalScore = sheets[i].getRange("C2").getValue();

    if(i == 0 || i == 1){
      continue;
    }
    console.log(sheets[i].getName(), totalScore);
    out.push({name: sheets[i].getName(), score: totalScore});  
  } 
  var scoresAndNames = out.sort((a)=> a.totalScore);
  var standingSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
  
  standingSheet.appendRow(["Name", "Score"]);
  for(var i = 0; i < scoresAndNames.length; i++){
    standingSheet.appendRow([scoresAndNames[i].name, scoresAndNames[i].score + 'p']);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.