2015-03-25 3 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

+0

К сожалению. Добавлено сейчас. – witham

ответ

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(); 

} 
Смежные вопросы