我正在研究一个本质上是变更日志的解决方案,我正在使用edit事件触发器来捕获要变更的数据。之后,我需要创建一个单独的工作表,该工作表实际上为每个已更改数据但显示的数据是最新数据的人显示一个单行。因此,如果工作表上已经存在唯一ID,则首先将其删除,然后将新数据移动到工作表中。
我拥有的脚本正在运行,但是由于效率不高,因此输入用户可能会“跳动”程序运行时间。
我能想到的唯一解决方案是将值读入数组,但是由于我是编程世界和应用程序脚本的新手,所以我很肯定有一个我还不知道的解决方案。
欢迎任何想法!
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sName = e.source.getActiveSheet().getSheetName();
var sNameActive = SpreadsheetApp.getActiveSheet();
if(sName == "D_Comp") { // enter the if statement only if changes we made on a sheet not named logging
var value;
var mA1 = e.range.getA1Notation().split(":")[0]; //range of change in A1 notation.
var col = e.range.getColumn(); // column number of where change occured
var row = e.range.getRow(); // row number of where the change occured
var time = new Date().toTimeString(); // time the change was made
var user = e.user;
var oldvalue; // defining the variable that will store the old value within the sheet
oldvalue = e.oldValue; // old value that was in the cell before hand
var aCell = e.source.getRange(mA1);
value = aCell.getValue();
var colhdr = ss.getActiveSheet().getRange(1, col).getValue(); // getting the header value
var Emplid = ss.getActiveSheet().getRange(row, 1).getValue(); // getting the emplid
var GUID = ss.getActiveSheet().getRange(row, 2).getValue(); // getting the GUID
var effdate= ss.getActiveSheet().getRange(row, 28).getValue(); // getting the change date
var Salary = ss.getActiveSheet().getRange(row, 44).getValue(); // get base salary value for workers row
var Location = ss.getActiveSheet().getRange(row, 36).getValue(); // get location value for workers row
var Department = ss.getActiveSheet().getRange(row, 16).getValue(); // get department numerical value for workes row
var DepName = ss.getActiveSheet().getRange(row, 17).getValue(); // get departmner name for workers row
var SOB = ss.getActiveSheet().getRange(row, 48).getValue(); // get phone stipend value for workers row
var Stp_phone = ss.getActiveSheet().getRange(row, 50).getValue(); // get phone stipend value for workers row
var Stp_home = ss.getActiveSheet().getRange(row, 51).getValue(); // get home stipend for workers home
var Chng_rsn = ss.getActiveSheet().getRange(row, 29).getValue();
if(oldvalue == undefined) { // we are replacing any underfined old value with a blank
oldvalue = '';
}
var data = [GUID, time, oldvalue, value, colhdr, user]; // output into table on Logging sheet
e.source.getSheetByName("Logging").appendRow(data);
var comp_sheet = ss.getSheetByName("Comp_Changes") //setting the variable of comp_sheet to the comp_Change_full_rows
var data2 = [Emplid, GUID, Chng_rsn, effdate, Salary, Location, Department, DepName, SOB, Stp_phone, Stp_home]; // populating the array data2
var lastrow = comp_sheet.getLastRow();
//loop to check if the emplid already exists on the comp final change sheet or not
for (var x = 1; x <= lastrow; x++){
var eval_emplid = comp_sheet.getRange(x, 1).getValue();
//if a match is found, delete the old row
if (eval_emplid == Emplid) {
comp_sheet.deleteRow(x);
}
}
comp_sheet.appendRow(data2); // appending the comp full rows sheet with the data in data2
}
}