[英]Google Spreadsheet - add Date to cell depending upon the contents of another cell
I have the following code to check column A for a certain value. 我有以下代码来检查列A的某个值。 (in this case 'Work in progress') The date is added to the adjacent cell in column B.
(在这种情况下为“进行中”)日期被添加到B列中的相邻单元格中。
What I want is that this only happens when the adjacent cell is empty. 我想要的是,仅当相邻单元格为空时才会发生这种情况。 If a date is already in that cell, it needs to skip adding a new date.
如果该单元格中已有日期,则需要跳过添加新日期的操作。
function onEdit(e) {
// Work in progress Begin Date
var colToWatch = 1, colToStamp = 2;
var valueToWatch = "Work in progress";
if (e.range.columnStart === colToWatch && (e.value === valueToWatch || typeof e.value == 'object'))
e.source.getActiveSheet()
.getRange(e.range.rowStart, colToStamp)
.setValue(typeof e.value === 'object' ? null : new Date());
}
Using Apps Script with a Google Spreadsheet you can run some code when a cell is edited, and then check the column and value of the cell, and then either enter the date or not. 将Apps Script与Google Spreadsheet结合使用时,可以在编辑单元格时运行一些代码,然后检查单元格的列和值,然后输入日期或不输入日期。
function onEdit(e) {//The letter "e" is used to get the event object
var adjacentCell,rng,row,sh;//define variables without assigning a value
var colToWatch = 1, colToStamp = 2;
var valueToWatch = "Work in progress";
rng = e.range;
row = rng.getRow();
if (rng.getColumn() !== colToWatch || e.value !== valueToWatch) {return;}
sh = e.source.getActiveSheet();
adjacentCell = sh.getRange(row,colToStamp).getValue();
Logger.log('adjacentCell: ' + adjacentCell)
Logger.log(!adjacentCell)
if (adjacentCell) {return;}//If there is a value in cell then stop
sh.getRange(row, colToStamp)
.setValue(new Date());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.