2015-03-25 73 views
0

我有一個破解的代碼,我已經成功地工作(原始問題在這裏https://stackoverflow.com/questions/29127005/multiple-spreadsheet-rows-from-multiple-forms),但循環寫了最後一行兩次,我的編碼技能不夠好,爲我找出原因...然而!最後一行寫兩次

任何人都可以闡明我所擁有/未包含的內容嗎?

function InsertDataInSheet(e) //Function to insert data into spreadsheet on clicking submit 
{ 

var app = UiApp.getActiveApplication(); 

//get number of rows to input 
    var num = parseInt(e.parameter.table_tag); 
    var num = num+1; 

//set increment step through 
    for (var i = 1; i < num ; i++) { 

//Declare varialbe fields to collect data from 
    var user   = Session.getActiveUser().getEmail(); 
    var date   = e.parameter['DateBox'+i]; 
    var location  = e.parameter['LocationListBox'+i]; 
    var source  = e.parameter['SourceListBox'+i]; 
    var reporter  = e.parameter['ReporterTextBox'+i]; 
    var priority  = e.parameter['PriorityListBox'+i]; 
    var hazard  = e.parameter['HazardListBox'+i]; 
    var details  = e.parameter['DetailsTextBox'+i]; 
    var description = e.parameter['DescriptionTextBox'+i]; 
    var timeStamp = new Date(); 

    //Decide date that this needs to be closed by 
    if (priority === '02 - WITHIN 24-48 HOURS') { 
     var dateTemp = new Date(date); 
     dateTemp.setDate(dateTemp.getDate()+2); 
     var actiondate = dateTemp; 
    } 
    if (priority === '03 - WITHIN 1 WEEK') { 
     var dateTemp = new Date(date); 
     dateTemp.setDate(dateTemp.getDate()+7); 
     var actiondate = dateTemp; 
    } 


     //establish email addresses 
    //Declare correct range to obtain values 
    var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes") 
    //Start with central maintenance department 
    var email00 = LocationSheet.getRange(33,5).getValue() 
    //followed by other emails as they appear 
    var email01 = LocationSheet.getRange(3,5).getValue(); 
    var email02 = LocationSheet.getRange(4,5).getValue(); 
    //declare the correct Depots to check 
    var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes"); 
    var depot01 = LocationSheet.getRange(3,4).getValue(); 
    var depot02 = LocationSheet.getRange(4,4).getValue(); 

    //if source is recorded as '08 - Maitenance Request System', the recipient is maintenance deparment 
    if (source === "08 - Maintenance Request System"){ 
    var recipient = email00; 
    //or depots as listed 
    } else if(location === depot01){ 
    var recipient = email01; 

    } else if(location === depot02){ 
    var recipient = email02; } 

else { 
    //and send an email to the error catch all if no code supplied 
    var recipient = email00; //change as necessary 
    } 

    var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName('LOG'); 
    var lastRow = sheet.getLastRow(); 
    var lrp1 = lastRow+1 
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]); 


//Notification Page 
app = UiApp.getActiveApplication().remove(0); 
    app.createVerticalPanel() 
    .setId('info') 
    .setVisible(true) 
    .setStyleAttribute('left', 10) 
    .setStyleAttribute('top', 10) 
    .setStyleAttribute('zIndex', '1') 
    .setStyleAttribute('position', 'fixed') 
    .setStyleAttribute('background', 'white') 
    .setHeight('400px') 
    .setStyleAttribute('text-align', 'center') 
    .setBorderWidth(1) 
    .setWidth('500px'); 

    app.add(app.createLabel('Your submission has been added to the database & the Site Coordinator will be emailed with notification.')); 
// app.add(app.createLabel('The details you entered were recorded as:')); 
// app.add(app.createLabel('Username: [' +user+ '] Timestamp: [' +timeStamp+ '] Reporter: [' +reporter+ ']')); 
// app.add(app.createLabel('Location: [' +location+ '] Hazard: [' +hazard+ '] Source: [' +source+ ']')); 
// app.add(app.createLabel('You listed the priority as: [' +priority+ '], please be aware this may not be the same priority given by the Site Coordinator responsible.')); 
    app.add(app.createLabel('Please refresh this page to submit more entries')); 
return app.close(); 

    } 

    var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LOG"); 
    var lastRow = sheet.getLastRow(); 
    var lrp1 = lastRow+1 
    //Amend [getRange(lastRow+1, 1, 1, **)] integer to reflet number of headers being written if more added 
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]); 
    var Body = 'A new [' +source+ '] log entry has been recorded at [' +location+ '], listed as [' + hazard+ ']. This form was submitted by [' +user+ '] with the timestamp [' +timeStamp+ '].' 

    } 
+0

原始問題沒有鏈接。 – KRR 2015-03-25 17:13:08

+0

糟糕。現在添加。 – witham 2015-03-26 09:11:44

回答

0

爲什麼它被添加兩次最後一排的原因是因爲下面的代碼是在循環後重復:

var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LOG"); 
    var lastRow = sheet.getLastRow(); 
    var lrp1 = lastRow+1 
    //Amend [getRange(lastRow+1, 1, 1, **)] integer to reflet number of headers being written if more added 
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]); 
    var Body = 'A new [' +source+ '] log entry has been recorded at [' +location+ '], listed as [' + hazard+ ']. This form was submitted by [' +user+ '] with the timestamp [' +timeStamp+ '].' 

希望幫助!

0

感謝KRR,我把它和它一起分類並將「}」之一從通知頁面之後移動到它之前。更正後的代碼是:

var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName('LOG'); 
    var lastRow = sheet.getLastRow(); 
    var lrp1 = lastRow+1 
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]); 

} 
///// TRAIL NOTIFICATION PAGE FROM WITHIN SYSTEM FROM HERE ///// 
app = UiApp.getActiveApplication().remove(0); 
    app.createVerticalPanel() 
    .setId('info') 
    .setVisible(true) 
    .setStyleAttribute('left', 10) 
    .setStyleAttribute('top', 10) 
    .setStyleAttribute('zIndex', '1') 
    .setStyleAttribute('position', 'fixed') 
    .setStyleAttribute('background', 'white') 
    .setHeight('400px') 
    .setStyleAttribute('text-align', 'center') 
    .setBorderWidth(1) 
    .setWidth('500px'); 

    app.add(app.createLabel('Your submission has been added to the database & the Site Coordinator will be emailed with notification.')); 
// app.add(app.createLabel('The details you entered were recorded as:')); 
// app.add(app.createLabel('Username: [' +user+ '] Timestamp: [' +timeStamp+ '] Reporter: [' +reporter+ ']')); 
// app.add(app.createLabel('Location: [' +location+ '] Hazard: [' +hazard+ '] Source: [' +source+ ']')); 
// app.add(app.createLabel('You listed the priority as: [' +priority+ '], please be aware this may not be the same priority given by the Site Coordinator responsible.')); 
    app.add(app.createLabel('Please refresh this page to submit more entries')); 
return app.close(); 

}