1

I have created a function that is supposed to loop through an array of objects and return the first value of each object.

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
  var sheetData = sheet.getDataRange().getValues();
  
  var data = [];
  
  for (var i = 0; i < sheetData.length; i++){
      var obj = {};
      obj = sheetData[i][0];
      return sheetData[i][0];
  }
      data.push(obj);
}

It's only returning the first item in the first row/column. Any clues on what I'm missing?

4
  • You only execute the loop once and the you break from it by returning the value. Commented Apr 7, 2017 at 0:00
  • if its to be used as a custom function, none of the answers will work. Commented Apr 7, 2017 at 0:21
  • @ZigMandel what do you mean by that? Commented Apr 7, 2017 at 6:41
  • first confirm you plan to use as a custom function (called from a cell formula) Commented Apr 7, 2017 at 11:16

4 Answers 4

2

You could use Object.keys together with Array#map to get just the first key value from each object.

data = sheetData.map(v => v[Object.keys(v)[0]]);


Working example:

var arr = [{foo: 'bar', bar: 'foo'},{foo: 'war', bar: 'foo'},{foo: 'mar', bar: 'foo'}],
    res = arr.map(v => v[Object.keys(v)[0]]);
    
    console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

0

How about this solution. Hope it helps!

var sheetData = [{name : "Mike", id: 10},{name : "Laura", id: 23},{name : "carl", id: 25},{name : "Lori", id: 23}];
    var arr = []
    for(var i in sheetData){
        var someObject = sheetData[i];
        arr.push(someObject[Object.keys(someObject)[0]]);
    }
    console.log(arr);

Comments

0

You have to move the return statement outside the loop.

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name),
  sheetData = sheet.getDataRange().getValues(),
  data = [];
  
  for (var i = 0; i < sheetData.length; i++){
      var obj = {};
      obj = sheetData[i][0];
      data.push(obj);
  }
   return data; 
}

Comments

-1

I'm not sure what your intent is, but probably it should be something like this?

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
  var sheetData = sheet.getDataRange().getValues();

  var data = [];

  for (var i = 0; i < sheetData.length; i++){
      var obj = {};
      obj = sheetData[i][0];
      data.push(obj);
  }

  return data;
}

UPDATE

As @grogx noted below, creation of a temporary object appears unnecessary in this context and the sample above could be optimized to

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
  var sheetData = sheet.getDataRange().getValues();

  var data = [];

  for (var i = 0; i < sheetData.length; i++){
      data.push(sheetData[i][0]);
  }

  return data;
}

Which can further be shortened to

function getSheetSectionData(name){
    return SpreadsheetApp.getActiveSpreadsheet()
        .getSheetByName(name)
        .getDataRange()
        .getValues()
        .map((e) => e[0]);
}

However, we do not really know, what the original intent of the OP was. It may be the case, that that temporary object was indeed required for some sort of intermediate transformation, which was striped out from the MCVE.

1 Comment

Why create a new object "obj"?? there's no need of doing that. You already have objects coming back!!

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.