Marcelo Meza
Marcelo Meza

Reputation: 73

Populate multiple choice grid from sheets

thanks to all, in advance, for any support you might be able to provide! I'm trying to populate a Multiple Choice Grid in Google Forms from Google Sheets. I've been able to populate dropdowns, but not grids. Here is the code I'm using for a grid:

 var form = FormApp.openById("MyId");
    var GridList = form.getItemById("MyItem").asGridItem();
    var ss = SpreadsheetApp.openById("MyGoogleSheet");
    var names = ss.getSheetByName("MySheet");
    var sheetValues = names.setRows(['Row1', 'Row2', 'Row3'])
    var sheetValues = names.setColumns(['Column1', 'Column2', 'Column3'])
    var formValues = [];
    for(var i = 0; i < sheetValues.length; i++)    
    if(sheetValues[i][0] != "")
    formValues[i] = sheetValues[i][0];
    GridList.setChoiceValues(formValues);

Any suggestions would be highly appreciated! Thank you.

Upvotes: 0

Views: 1501

Answers (2)

Cooper
Cooper

Reputation: 64062

Just a few recommendations for you regarding your answer:

function afunc() {
  var form = FormApp.openById('MyId');
  var PtjGridList = form.getItemById(MyItem).asGridItem();
  var ss = SpreadsheetApp.openById("MyGoogleSheet");
  var PtjNombre = ss.getSheetByName("MySheet");
  //var RowValues = PtjNombre.getRange(2, 5, PtjNombre.getMaxRows() - 1).getValues();

The above line should be replaced with the one below and your code will run faster and be more reliable.

  var RowValues = PtjNombre.getRange(2, 5, PtjNombre.getLastRow() - 1).getValues();//replace with this

  var ValuesRow = [];

  //sheetValues is undefined within this function
  for(var i = 0; i<sheetValues.length; i++)
    if(RowValues[i][0] != "")ValuesRow[i] = RowValues[i][0];

It would be better to rewrite the above function in the following way:

  for(var i = 0; i<sheetValues.length; i++) {
    if(RowValues[i][0] != "") {
      ValuesRow[i] = RowValues[i][0];
    }
  }
  PtjGridList.setRows(ValuesRow)
  //var ColumnValues = PtjNombre.getRange(2, 6, PtjNombre.getMaxRows() - 1).getValues();

Using getLastRow() is much better since using getMaxRows() can result in getting a lot of null values at the end of your data and often will result in errors.

  var ColumnValues = PtjNombre.getRange(2, 6, PtjNombre.getLastRow() - 1).getValues();//replace with this

  var ValuesColumn = [];

  //again sheetValues is not defined within this function
  for(var i = 0; i < sheetValues.length; i++)
    if(ColumnValues[i][0] != "")
      ValuesColumn[i] = ColumnValues[i][0];
  //would be better to write like this:

Again sheetValues is not defined in this function and the following rewrite would improve readability greatly.

  for(var i = 0; i < sheetValues.length; i++) {
    if(ColumnValues[i][0] != "") {      
      ValuesColumn[i] = ColumnValues[i][0];
    }
  }

  PtjGridList.setColumns(ValuesColumn)
}

getLastRow()

getMaxRows()

I was not trying to be rude or unkind. Your previous code had some serious problems and you addressed them and got it to run. That's what we like to see. Good work.

Upvotes: 1

Marcelo Meza
Marcelo Meza

Reputation: 73

I found the answer myself: This populates rows and columns in a Multiple Choice Grid form question.

var form = FormApp.openById('MyId');
var PtjGridList = form.getItemById(MyItem).asGridItem();
var ss = SpreadsheetApp.openById("MyGoogleSheet");
var PtjNombre = ss.getSheetByName("MySheet");
var RowValues = PtjNombre.getRange(2, 5, PtjNombre.getMaxRows() - 1).getValues();
var ValuesRow = [];
for(var i = 0; i < sheetValues.length; i++)
if(RowValues[i][0] != "")
ValuesRow[i] = RowValues[i][0];
PtjGridList.setRows(ValuesRow)
var ColumnValues = PtjNombre.getRange(2, 6, PtjNombre.getMaxRows() - 1).getValues();
var ValuesColumn = [];
for(var i = 0; i < sheetValues.length; i++)
if(ColumnValues[i][0] != "")
ValuesColumn[i] = ColumnValues[i][0];
PtjGridList.setColumns(ValuesColumn)

Upvotes: 0

Related Questions