Loops 最后一行写两次
我有一个坏代码,我已经设法得到工作(原来的问题在这里),但循环写最后一行两次,我的编码技能不够好,我不知道为什么…还 有人能解释一下我有/没有包括哪些内容吗Loops 最后一行写两次,loops,google-apps-script,Loops,Google Apps Script,我有一个坏代码,我已经设法得到工作(原来的问题在这里),但循环写最后一行两次,我的编码技能不够好,我不知道为什么…还 有人能解释一下我有/没有包括哪些内容吗 function InsertDataInSheet(e) //Function to insert data into spreadsheet on clicking submit { var app = UiApp.getActiveApplication(); //get number of rows to input va
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+ '].'
}
function InsertDataInSheet(e)//在单击提交时将数据插入电子表格的函数
{
var app=UiApp.getActiveApplication();
//获取要输入的行数
var num=parseInt(e.parameter.table_标记);
var num=num+1;
//设置增量步进
对于(变量i=1;i
之所以两次添加最后一行,是因为循环后重复了以下代码:
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+ '].'
希望有帮助 谢谢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();
}
原始问题没有链接。