Bokai
Bokai

Reputation: 117

setValue() of an array only pasting one of the values in the array

I have a sheet where I am taking the last number at the end of the values in the barcode column, which represents the total number of items in an order. I put those values in an array. Then I tried setting the values of the array to the column "Number of Units". However, it is putting the first value of the array to all the cells in the column. How can I get it to put each value in the array into its own cell under the "Number of Units" column?

Here is a look at the issue in the Sheet: enter image description here

Here is what the code is supposed to do: (I just put the values in manually)enter image description here

The numbers after the last hyphen in the barcode column is the number of units.

Here is my code that splits the barcode values and adds the number to an array:

function numberOfUnits() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //Get specific sheet
  var sheet = ss.getSheetByName('Sheet3');

  // get teh data on the sheet
  var values = sheet.getDataRange().getValues();
  var units = []
  for (var i = 1 ; i < values.length; i++) {
   var barcode = values[i][1];
   var unit = barcode.split('-');
    units.push(unit[unit.length-1]);

  }
  return units;
}

This seems to work just fine, but when I call this function from within another function, that is when I get the error.

Here is the other function:

function rate() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //Get specific sheet
  var sheet = ss.getSheetByName('Sheet3');

  // get the data on the sheet
  var values = sheet.getDataRange().getValues();
  // sets the units as an array of all the units values
  var units = numberOfUnits();
  // sets the unit value to every cell under the "number of units" column
  sheet.getRange(2, values[0].length, values.length, 1).setValue(units);

}

Upvotes: 0

Views: 3859

Answers (1)

Jack Brown
Jack Brown

Reputation: 5892

This line:

sheet.getRange(2, values[0].length, values.length, 1).setValue(units);

Should be

sheet.getRange(2, values[0].length, values.length, 1).setValues(units);

Note the difference setValues vs setValue, documentation.

Upvotes: 2

Related Questions