Home > Software engineering >  SetValues() of a named range column based on values from another column
SetValues() of a named range column based on values from another column

Time:07-09

I am grabbing two arrays of values from 2 separate single-column named ranges.

The following code gets the error of: Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues.

I understand that setValues() requires a 2 dimensional array. How can I change values in otherRangeVals and change it to a 2 dimensional array?

function setValuesInARange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getRangeByName("columnOne");
  var otherRange = ss.getRangeByName("columnTwo");
  var startingRow = ss.getRangeByName("headerRow").getRow();
  var vals = range.getValues();
  var otherRangeVals = otherRange.getValues();
  for (var i = startingRow   1; i < vals.length; i  ) {
    var currentVal = vals[i];
    Logger.log(currentVal);
    if (currentVal != "") {
      otherRangeVals[i] = "new value";
    }
  }
  otherRange.setValues(otherRangeVals);
}

CodePudding user response:

Try it this way:

function setValuesInARange() {
  var ss = SpreadsheetApp.getActive();
  var c1rg = ss.getRangeByName("columnOne");
  var c2rg = ss.getRangeByName("columnTwo");
  var hr = ss.getRangeByName("headerRow").getRow();
  var c1vs = c1rg.getValues();
  var c2vs = c2rg.getValues();
  for (var i = hr   1; i < c1vs.length; i  ) {
    if (c1vs[i][0] != "") {
      c2vs[i][0] = "new value";
    }
  }
  c2rg.setValues(c2vs);
}
  • Related