对Google表格脚本进行故障排除以根据工作表数据生成表单

时间:2015-08-24 04:37:50

标签: google-apps-script google-sheets

我正在尝试使用Google表格脚本根据工作表内的数据生成新的Google表单。我所拥有的代码可以创建一个具有正确标题的新表单,但它不会使用表格中的任何其他信息填充表单:表单描述,问题,帮助文本等。我​​不知道我做错了什么。先感谢您。

Editable Copy of Sheet

以下是代码:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var createForm = [ {name: "Generate", functionName: "CreateFormfromSheet"}];
  ss.addMenu("Generate Test", createForm);
}

function CreateFormfromSheet() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
 var range = ss.getDataRange(); 
 var data = range.getValues();
 var numberRows = range.getNumRows();
 var numberColumns = range.getNumColumns();
 var firstRow = 2;
 var form = FormApp.create(data[0][1]);

 form.setDescription(data[0][2]);

 for(var i=0;i<numberRows;i++){
  var questionType = data[i][1]; 
  if (questionType==''){
     continue;
  }
  else if(questionType=='TEXT'){
   form.addTextItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2])
     .setRequired(true);   
  } 
  else if(questionType=='PARAGRAPH'){
   form.addParagraphTextItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2])
     .setRequired(true);
  }
  else if(questionType=='CHOICE'){
  var rowLength = data[i].length;
  var currentRow = firstRow+i;
  var currentRangeValues = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(currentRow,1,1,rowLength).getValues();
  var getSheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange();
  var numberOfColumnsSheet = getSheetRange.getNumColumns();
  var numberOfOptionsInCurrentRow = numberOfColumnsSheet;
  var lastColumnInRange = String.fromCharCode(64 + (numberOfOptionsInCurrentRow));
  var range_string = 'E' + currentRow + ":" + lastColumnInRange + currentRow;
  var optionsArray = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(range_string).getValues();
  var choicesForQuestion =[];
    for (var j=0;j<optionsArray[0].length;j++){
        choicesForQuestion.push(optionsArray[0][j]);
        }
  form.addMultipleChoiceItem()
    .setTitle(data[i][1]) 
    .setHelpText(data[i][2])
    .setChoiceValues(choicesForQuestion)
    .setRequired(true); 
  }
  else if(questionType=='CHECKBOX'){
  var rowLength = data[i].length;
  var currentRow = firstRow+i;
  var currentRangeValues = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(currentRow,1,1,rowLength).getValues();
  var getSheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange();
  var numberOfColumnsSheet = getSheetRange.getNumColumns();
  var numberOfOptionsInCurrentRow = numberOfColumnsSheet;
  var lastColumnInRange = String.fromCharCode(64 + (numberOfOptionsInCurrentRow));
  var range_string = 'E' + currentRow + ":" + lastColumnInRange + currentRow;
  var optionsArray = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(range_string).getValues();
  var choicesForQuestion =[];
    for (var j=0;j<optionsArray[0].length;j++){
        choicesForQuestion.push(optionsArray[0][j]);
        }
  form.addCheckboxItem()
    .setTitle(data[i][1]) 
    .setHelpText(data[i][2])
    .setChoiceValues(choicesForQuestion)
    .setRequired(true);
  }
  else if(questionType=='LIST'){
  var rowLength = data[i].length;
  var currentRow = firstRow+i;
  var currentRangeValues = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(currentRow,1,1,rowLength).getValues();
  var getSheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange();
  var numberOfColumnsSheet = getSheetRange.getNumColumns();
  var numberOfOptionsInCurrentRow = numberOfColumnsSheet;
  var lastColumnInRange = String.fromCharCode(64 + (numberOfOptionsInCurrentRow));
  var range_string = 'E' + currentRow + ":" + lastColumnInRange + currentRow;
  var optionsArray = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(range_string).getValues();
  var choicesForQuestion =[];
    for (var j=0;j<optionsArray[0].length;j++){
        choicesForQuestion.push(optionsArray[0][j]);
        }
  form.addListItem()
    .setTitle(data[i][1]) 
    .setHelpText(data[i][2])
    .setChoiceValues(choicesForQuestion)
    .setRequired(true);
  }
  else if(questionType=='GRID'){
  var rowLength = data[i].length;
  var currentRow = firstRow+i;
  var currentRangeValues = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(currentRow,1,1,rowLength).getValues();
  var getSheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange();
  var numberOfColumnsSheet = getSheetRange.getNumColumns();
  var numberOfOptionsInCurrentRow = numberOfColumnsSheet;
  var lastColumnInRange = String.fromCharCode(64 + (numberOfOptionsInCurrentRow));
  var range_string = 'E' + currentRow + ":" + lastColumnInRange + currentRow;
  var optionsArray = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(range_string).getValues();
  var rowTitles =[];
    for (var j=0;j<optionsArray[0].length;j++){
        rowTitles.push(optionsArray[0][j]);
        }
  var rowLength = data[i+1].length;
  var currentRow = firstRow+i+1;
  var currentRangeValues = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(currentRow,1,1,rowLength).getValues();
  var getSheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange();
  var numberOfColumnsSheet = getSheetRange.getNumColumns();
  var numberOfOptionsInCurrentRow = numberOfColumnsSheet;
  var lastColumnInRange = String.fromCharCode(64 + (numberOfOptionsInCurrentRow));
  var range_string = 'E' + currentRow + ":" + lastColumnInRange + currentRow;
  var optionsArray = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(range_string).getValues();
  var columnTitles =[];
    for (var j=0;j<optionsArray[0].length;j++){
        columnTitles.push(optionsArray[0][j]);
        }  
  form.addGridItem()
    .setTitle(data[i][1]) 
    .setHelpText(data[i][2])
    .setRows(rowTitles)
    .setColumns(columnTitles)
    .setRequired(true);
  } 
  else if(questionType=='IMAGE'){
   var img = UrlFetchApp.fetch(data[i][3]); 
   form.addImageItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2])
     .setImage(img);
  }
  else if(questionType=='PAGE'){
   form.addPageBreakItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2]);   
  } 
  else if(questionType=='SECTION'){
   form.addSectionHeaderItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2]);   
  }
  else if(questionType=='TIME'){
   form.addTimeItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2]);   
  }
  else{
    continue;
  }
 } 
}

1 个答案:

答案 0 :(得分:0)

问题似乎在data [] []矩阵中的索引上。

描述位于data [0] [4],而不是脚本中的当前值data [0] [2]。

最后,在&#39; for&#39;要创建一个问题,您正在寻找data [i] [1]中的问题类型,但它应该是data [i] [0]。

我已经修改了脚本,它现在应该正常工作。