简体   繁体   中英

looping setRows in Google-apps-script for Google forms

In the context of Google forms, using Google apps script, I am trying to create a form of grid-multiple choice type. I am using following code to loop the rows - the aim is to have roll number of students on the rows and their grades in columns: Code:

function myFunction() {
  var form = FormApp.create('Peer_Grading_');
  var name = form.addTextItem();
  name.setTitle('Your Name');
  var rnum=form.addTextItem();
  rnum.setTitle('Your Roll Number');
  var item = form.addGridItem()

  item.setTitle('Grade Matrix')
  for(var i=1; i<=30; i=i+1){

    if(i<10){
      item.setRows(['16633000'+i])

    }
    else{
      item.setRows(['1663300'+i])

    }
  };
  item.setColumns(['AA', 'AB', 'BB', 'BC', 'CC', 'CD']);
}

But I am getting this screenshot as output: screenshot of the form.

Only the last roll number is coming as an output of for loop.

It looks like you are overwriting the row in the loop. Try using an array then push the data in the loop:

function myFunctionForm() {
  var form = FormApp.create('Peer_Grading_');
  var name = form.addTextItem();
  name.setTitle('Your Name');
  var rnum=form.addTextItem();
  rnum.setTitle('Your Roll Number');
  var item = form.addGridItem()

  var rowArr = []

  item.setTitle('Grade Matrix')
  for(var i=1; i<=30; i=i+1){

    if(i<10){
  rowArr.push('16633000'+i)

    }
    else{
    rowArr.push(['1663300'+i])

    }
  };
   item.setColumns(['AA', 'AB', 'BB', 'BC', 'CC', 'CD']);
  item.setRows(rowArr);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM