2014-01-26 89 views
0

我有一個Google表單,其中提問的問題是「是否已經創建了傳單」,如果答案是「否」,那麼我需要收集來自形式提交併在外部工作表中插入新行。 I have a drawing here.在表單提交中插入外部Google表單中的行

紙張#1(the sheet with the form is here) 紙張#2(sheet that will get a new conditional row submit is here

我已經開始了一個腳本來做到這一點插入,但我不具備的知識,完成了劇本。

function onFormSubmit(e) { 
    var timestamp = e.values[0]; 
    var name = e.values[1]; 
    var phone = e.values[2]; 
    var classTitle = e.values[4]; 
    var dueDate = e.values[7]; 

    //when there is field 19 in this sheet answer is No (There has not yet been a flyer created) this is when the insert would happen and the Field 19 answer is not something that needs to be carried to the new row in the external sheet) 
    var formCreated = e.values[19]; 

    var workType = "Flyer" ; 
    var contextNote = "This job was imported from a Org Learning's Class Reservation Form and followup will be required for clarification" ; 
    var destinationSpreadSheetKey = "0Ai_2YLvaQba0dHA2bTdtd0pnTTZQcnRldjJXcjFjZHc"; 
    var destinationSheet = "Work_Orders"; 

    //if the answer to variable formCreated is "Yes", then halt Else continue with script 

有人可以幫我解決這個問題嗎?我有一張圖紙中的字段地圖。

感謝,

戴夫

+1

我們接近Google論壇的解決方案。我會在這裏添加解決方案,爲任何可能有類似需求的人提供答案。 –

回答

0

最後的工作代碼,很好的評價。

/** 
* Copies from separate source ranges and pastes at first empty row on target sheet 
*/ 

function CopyIt() { 
var source_spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); 
var target_spreadsheet = SpreadsheetApp.openById("0Ai_2YLvaQba0dHA2bTdtd0pnTTZQcnRldjJXcjFjZHc"); 


// Get source and target sheets - can be the same or different 
var sourcesheet = source_spreadsheet.getSheetByName("Form Responses"); 
var targetsheet = target_spreadsheet.getSheetByName("Work_Orders"); 

//Get row of last form submission 
var source_last_row = sourcesheet.getLastRow(); 

// Check for answer to Flyer Created? If Yes, end now. If not Yes, i.e. No, continue. 
var check = sourcesheet.getRange("T" + (source_last_row)).getValue(); 
if (check != 'Yes') { 

    // Get the source ranges 
    // TimeStamp 
    var source_range1 = sourcesheet.getRange("A" + (source_last_row)); 

    //Name 
    var source_range2 = sourcesheet.getRange("B" + (source_last_row)); 


    //Phone Number 
    var source_range3 = sourcesheet.getRange("C" + (source_last_row)); 


    //Class Name/Title 
    var source_range4 = sourcesheet.getRange("E" + (source_last_row)); 


    //First Session 
    var source_range5 = sourcesheet.getRange("H" + (source_last_row)); 

    var source_range1_values = source_range1.getValues(); 
    var source_range2_values = source_range2.getValues(); 
    var source_range3_values = source_range3.getValues(); 
    var source_range4_values = source_range4.getValues(); 
    var source_range5_values = source_range5.getValues(); 

    // Get the last row on the target sheet 
    var last_row = targetsheet.getLastRow(); 

    // Set the target ranges on target sheet 
    //TimeStamp 
    var target1 = targetsheet.getRange("A" + (last_row + 1)); 

    //Name 
    var target2 = targetsheet.getRange("H" + (last_row + 1)); 

    //Phone 
    var target3 = targetsheet.getRange("I" + (last_row + 1)); 

    //Your Project Name 
    var target4 = targetsheet.getRange("K" + (last_row + 1)); 

    //Final Due Date 
    var target5 = targetsheet.getRange("M" + (last_row + 1)); 

    //The next two are defined in script 
    var target6 = targetsheet.getRange("O" + (last_row + 1)); 
    var target7 = targetsheet.getRange("Q" + (last_row + 1)); 

    // Put the data from the source sheet into the target sheet, after adding a new row 
    targetsheet.insertRowAfter(last_row); 
    target1.setValues(source_range1_values); 
    target2.setValues(source_range2_values); 
    target3.setValues(source_range3_values); 
    target4.setValues(source_range4_values); 
    target5.setValues(source_range5_values); 

    // Just using the text to put this data, so there is no source range above. 
    target6.setValue('Flyer'); 
    target7.setValue('This job was imported from a Org Learning\'s Class Reservation Form and followup will be required for clarification'); 


} 
}