2015-08-27 52 views
0

我有Google腳本,它將根據Google工作表中的數據創建Google表單。但是,代碼沒有正確檢測每行中的最後一個空單元格。相反,它會將每個單元格寫入該行,即使它是空白的。所以表格總是有十五個可能的選擇的問題,因爲這是表格的範圍。這是一個錯誤的例子form。這是一個鏈接到editable version of the sheet。謝謝。對Google表格腳本進行問題排查:在行中查找第一個空列

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var createForm = [ {name: "Create form", 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][4]); 

for(var i=0;i<numberRows;i++){ 
    var questionType = data[i][0]; 
    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; 
    } 
} 
} 
+0

請[編輯]你的問題包括[MCVE。通過闡明問題,這將有助於Stack Overflow社區和將來可能遇到同樣問題的用戶。 – Mogsdad

回答

0
var getSheetRange =   SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange(); 

這條線是越來越整個片材的範圍內,則使用的是在上述範圍中的下一行:

var numberOfColumnsSheet = getSheetRange.getNumColumns(); 

其中給予的列數爲19,因爲它包括你的第二排。

嘗試:

var numberOfColumnSheet = someRowRange.getNumColumns(); 
0

使用爲CHOICE塊,這是我會怎麼處理它:

else if (questionType=='CHOICE') { 
    // we already have data, no need to retrieve it again 
    var choicesForQuestion = []; 
    for (var j=4; j < numberColumns; j++) 
    if (data[i][j] != "") // only push if there's something to push 
     choicesForQuestion.push(data[i][j]); 
    form.addMultipleChoiceItem() 
    .setTitle(data[i][1]) 
    .setHelpText(data[i][2]) 
    .setChoiceValues(choicesForQuestion) 
    .setRequired(true); 
} 
相關問題