[英]Google Sheet App Script Timestamp Condition(s)
This is probably really simple but I'm not able to figure this out. 这可能真的很简单,但我无法弄清楚。
I have a timestamp script that i copied from this cool dude 我有一个时间戳脚本,是我从这个帅哥那里复制的
Now, what I want to do is only create a timestamp if a cells in Columns A,B,C are changed or are filled. 现在,我想要做的是仅在列A,B,C一个细胞被改变或充满创建一个时间戳。
/**
* Creates a Date Stamp if a column is edited.
*/
//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = [2,3];
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,1];
// Sheet you are working on
var SHEETNAME = 'Sheet1'
function onEdit(e) {
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() == COLUMNTOCHECK[0,1]) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
I had assumed that adding on a clause to the if statement with the var indexed would fix this.. with changing COLUMNTOCHECK to [2,3,4,5] and so forth. 我以为在if语句上添加一个带有var索引的子句将解决此问题,将COLUMNTOCHECK更改为[2,3,4,5]等。
if( selectedCell.getColumn() == COLUMNTOCHECK[0] || selectedCell.getColumn() == COLUMNTOCHECK[1] )
EDIT. 编辑。
Sorry, my apologies for my bad use of English and unclear request. 抱歉,我对英语使用不当和要求不明确表示歉意。
What I want is the following. 我想要的是以下内容。
so as an example 举个例子
John enters sales data into Cells A2, B2 = Timestamp Created. John将销售数据输入到单元格A2,B2 =已创建时间戳。
John enters data into Cells A2 = No Timestamp created. John将数据输入到单元格A2 =未创建时间戳。
Hopefully that makes sense? 希望这有意义吗?
indexOf()
on the array of COLUMNTOCHECK
to check if one of the columns you're watching was edited. COLUMNTOCHECK
数组上使用indexOf()
来检查您正在查看的列之一是否已被编辑。 COLUMNTOCHECK
and check the values for each column in the edited row. COLUMNTOCHECK
并检查已编辑行中每一列的值。 If each column in that row has a value, then enter the timestamp. Depending on your requirements, you could simplify and make step 3 run faster. 根据您的要求,您可以简化并使第3步运行更快。 If you're only ever going to check columns A & B, you could simply grab that range once (
editedSheet.getRange(editedRow, 1, 1, 2).getValues()
) and then iterate through the returned array of values. 如果仅检查A和B列,则只需获取一次该范围(
editedSheet.getRange(editedRow, 1, 1, 2).getValues()
),然后遍历返回的值数组。
Note also that I removed the offset and am placing the timestamp in column C. Again, I'm not sure of your requirements, but the implementation you had would have overwritten values in column B for edits made in column A. 还要注意,我删除了偏移量并将时间戳记放置在C列中。同样,我不确定您的要求,但是对于在A列中进行的编辑,您的实现将在B列中覆盖值。
function onEdit(e) {
var COLUMN_TO_CHECK = [1,2]; // The columns to check if something is entered. (Columns A & B)
var DATE_TIME_COLUMN = 3; // Where you want the date time stamp (Column C)
var SHEET_NAME = "Sheet1"; // Sheet you are working on
var editedCell = e.range;
var editedSheet = e.range.getSheet();
if (editedSheet.getName() === SHEET_NAME) {
if (COLUMN_TO_CHECK.indexOf(editedCell.getColumn()) != -1) {
var allFilled = true;
var editedRow = editedCell.getRow();
for (var i in COLUMN_TO_CHECK) {
if (editedSheet.getRange(editedRow, COLUMN_TO_CHECK[i]).getValue() == "") {
allFilled = false;
}
}
if (allFilled) {
editedSheet.getRange(editedRow,DATE_TIME_COLUMN).setValue(new Date());
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.