skip to Main Content

This is the code :

var values = [[ ph, sd, sg, qz, str ]];
var Rangeisi = shtdb.getRangeList(['C3:C6','C9']);
if(id="id001"){Rangeisi.setValues(values);
   return }

And this is the result :

TypeError: Rangeisi.setValues is not a function

But when I use this code :

var values = [[ ph, sd, sg, qz, str ]];
var Rangeisi = shtdb.getRange(['A1:E1']);
if(id="id001"){Rangeisi.setValues(values);
  return }

It works!

My question is: how to select range (C3,C4,C5,C6,C9) to set values [ ph, sd, sg, qz, str ] ?

2

Answers


  1. There is no setValues() method of a RangeList only setValue() which set each cell in the range to the same value.

    The following example shows how to use a range list but it is limited to your specific example shown in your question. It would probably need to be modifed for different RangeList.

    Since the first range in the range list is C3:C6 i construct a 2D array of those value. And since the last range in the range list is on C9 I still construct a 2D array of that value to use the setValues() method of Range. I wrap both of those in an array to match the number of ranges in the range list.

    function test() {
      try {
        let ph = 1;
        let sd = 2;
        let sg = 3;
        let qz = 4;
        let str = 5;
        let spread = SpreadsheetApp.getActiveSpreadsheet();
        let shtdb = spread.getSheetByName("Sheet1");
        var values = [ [ [ph], [sd], [sg], [qz] ], [ [str] ] ]; 
        var Rangeisi = shtdb.getRangeList(['C3:C6','C9']); 
        let id = "id001";
        if(id="id001"){
          Rangeisi.getRanges().forEach( (range,index) => {
              range.setValues(values[index]);
            }
          );
        }
      }
      catch(err) {
        console.log(err);
      }
    }
    

    Reference:

    Login or Signup to reply.
  2. Or simply this:

    function test() {
        let ph = 1;
        let sd = 2;
        let sg = 3;
        let qz = 4;
        let str = 5;
        let ss = SpreadsheetApp.getActive();
        let sh = ss.getSheetByName("Sheet1");
        var values = [[ph], [sd], [sg], [qz], [str]];
        sh.getRange(1,1,values.length,values[0].length).setValues(values)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search