1

Here is the code I have so far:

var items = data.getRange(1,14,1,lc).getValues()[0];
var names = cdata.getRange(52, 1, 28,1).getValues()[0];

When I log 'items' I get the desired information populated in a single array. When I try to get the 'names' in a column using a similar method I can only get the first cell in a single array or all of the desired cells in an array of arrays.

example of items

[item1,item2,item3,...]

example of names

[name1] or [[name1],[name2],[name3],[...]]

Why am I not able to use the same method to accurately retrieve the names in a column as I did in the row? How do I fix this and get the desired result? How do I get names to return the data in the cells as I did with items?

Dan.db
  • 79
  • 6
  • Does this answer your question? [What does the range method getValues() return and setValues() accept?](https://stackoverflow.com/questions/63720612/what-does-the-range-method-getvalues-return-and-setvalues-accept) – Marios Nov 19 '20 at 20:26
  • getRange(row,column,#rows,#columns) and getValues() can get just the values in the range or [row][column] but I'm not 100% sure about getValues. Tried doing [0][0] instead of just [0] and I still got the one name. – Dan.db Nov 19 '20 at 20:38
  • didn't realize it was a link sorry. It did give me some more insight and answers part of my question. Still need to figure out a way to flatten the array then since .flat isn't a thing in google scripts. – Dan.db Nov 19 '20 at 21:09
  • 1
    `getValues().flat()` – Marios Nov 19 '20 at 21:32
  • 1
    That worked thanks! Didn't think it would work because it didn't show up in the list when typed. – Dan.db Nov 19 '20 at 21:59

2 Answers2

1

I see you have 1 column for names combined with []. That will return a [] result from a a structure like [ [][][] ] (not [ [] ]). If your items have multiple rows, you'll get the entire first row [,,,], as you suggest in items.

var items = data.getRange(1,14,1,lc).getValues()[0];
var names = cdata.getRange(52, 1, 28,1).getValues()[0];

That's more a diagnosis than an answer. Basically, you're getting what you should get from that, but I think you want it transposed. Various ways to do that.

But will this work?

// flatten(flatten()) works for 2-level nesting, and so on
function flatten(arrayOfArrays){
  return [].concat.apply([], arrayOfArrays);
}

reference: https://gist.github.com/MauricioMoraes/225afcc9dd72acf1511f

Jason Torpy
  • 124
  • 14
  • I wasn't able to get this to work when I wrote it as follows: var names = cdata.getRange(52, 1, 28,1).getValues(); // flatten(flatten()) works for 2-level nesting, and so on function flatten(names){ return [].concat.apply([], names); Logger.log(flatten); } – Dan.db Nov 19 '20 at 21:54
  • 1
    I was able to use var names = cdata.getRange(52, 1, 28,1).getValues().flat(); to get the desired result. – Dan.db Nov 19 '20 at 22:00
1

You will have to use this instead:

var names = cdata.getRange(52, 1, 28,1).getValues().flat();
ale13
  • 5,679
  • 3
  • 10
  • 25