新手问题
我想对于大多数人来说,这是一个简单的问题,但我无法弄清自己所缺少的内容,并且花了相当长的时间试图弄清楚。
我有一个脚本,应该为不同的列加上时间戳。通过将输入分别放在B,C和G的列中,所有if函数都可以正确地在A,E和F列上加印。
我遇到的一个问题是在选中d列时让E列显示输入。我尝试了多种不同的方法(在代码中已注释掉)。
我不确定我丢失了什么,但是正在运行的代码与未运行的代码之间的区别在于,正在运行的代码使用偏移函数来放置当前时间的日期戳,而正在运行的代码不能正常工作应该从一行向上复制两列的数据。本质上,这意味着在一个示例中,当选中D10列时,应将F9中的时间值复制到E10(自从我的原始帖子以来,我已经更正了这个示例。第一次写时我有点累了)
这是我的代码示例。为了清楚起见,我还附上了电子表格的图片。我怀疑我使用函数错误的问题,但是我没有足够的经验来理解原因。
function onEdit(e){
//CORE VARIABLES
// The columns that trigger datestamps in other columns
var COLUMNB = 2;
var COLUMNC = 3;
var COLUMND = 4;
var COLUMNG = 7;
// Where you want the date time stamp offset from the input location. [row, column]
// DTL = Date Time Location
//Intiates a series of variables to hold the offset values for datestamping the row entry
//Instiates a variable which controls the offset values for column B name entry
//The date stamp currently corresponds to column A and is triggered by selecting a client in column B
var DTLColB = [0,-1];
//Intiates a variable to hold the offset values for datestamping the start time column with the current time
//The date stamp currently corresponds to the time value in column E and the trigger is in column C
var DTLColCCTstart = [0, 2];
//Intiates a variable to hold the offset values for datestamping the start time column with the previous row entry time
//The date stamp currently corresponds to the time value in column E(offset(-1,2))and the trigger is in column D,-,-
//It copies the previous end time into the current start time
var DTLColDLTstart = [0, 1];
//Intiates a variable to hold the offset values for datestamping the current time in the end time col for for a row
//The date stamp currently corresponds to the time value in column F and the trigger is in column G
var DTLColGCTend = [0, -1];
var LastStampCoord = [-1, 2];
//Intiates a variable that controls which sheets this function is active on
var SHEETNAME = 'Cont Log'
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//checks that we're on the correct sheet.
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
//checks the column to ensure it is on the one we want to cause the date to appear.
if( selectedCell.getColumn() == COLUMNB) {
var dateTimeCellB = selectedCell.offset(DTLColB[0],DTLColB[1]);
dateTimeCellB.setValue(new Date());
}
if( selectedCell.getColumn() == COLUMNC || selectedCell.getColumn == true ) {
var dateTimeCellC = selectedCell.offset(DTLColCCTstart[0],DTLColCCTstart[1]);
dateTimeCellC.setValue(new Date());
}
if( selectedCell.getColumn() == COLUMND || selectedCell.getColumn == true) {
var dateTimeCellD = selectedCell.offset(DTLColDLTstart[0],DTLColDLTstart[1]);
// var oldtimevalue = selectedCell.offset(LastStampCoord[0],LastStampCoord[1]);
// var oldtimevalue = range.selectedCell.offset(LastStampCoord[0],LastStampCoord[1]));
//var oldtimevalue = selectedCell.offset(LastStampCoord[0],LastStampCoord[1]);
//var oldtimevalue = selectedCell.getrange(selectedCell.offset(LastStampCoord[0], LastStampCoord[1])).getvalues;
//var oldtimerecord = sheet.getrange(selectedCell.offset(LastStampCoord[0],LastStampCoord[1])).getvalue();
//var oldtimevalue = oldtimerecord.getvalue();
// var LastStampLookup = selectedCell.offset(LastStampCoord[0],LastStampCoord[1]).getvalue();
// var LastStampTime = LastStampLookup.getvalue()
// range1.offset(2, 0).setValue("Order complete");
// dateTimeCellD.offset(LastStampCoord[0], LastStampCoord[1]).setvalue(timelocation);
//dateTimeCellD.setValue(new oldtimevalue.getvalue());
dateTimeCellD.setValue(new date());
}
if( selectedCell.getColumn() == COLUMNG || selectedCell.getColumn == true) {
var dateTimeCellG = selectedCell.offset(DTLColGCTend[0],DTLColGCTend[1]);
dateTimeCellG.setValue(new Date());
}
}
}
[1]: https://i.stack.imgur.com/85DwP.png
答案 0 :(得分:0)
尝试一下:
function onEdit(e){
var sh=e.range.getSheet();
if( sh.getName()=="Cont Log" ) {
var ts=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"E MM/dd/yyyy HH:mm:ss");
if(e.range.columnStart==2) {e.range.offset(0,-1).setValue(ts);}
if(e.range.columnStart==3) {e.range.offset(0,2).setValue(ts);}
if(e.range.columnStart==4) {e.range.offset(0,1).setValue(ts);}
if(e.range.columnStart==7) {e.range.offset(0,-1).setValue(ts);}
}
}
答案 1 :(得分:0)