我一般都不熟悉脚本编写,因此一直在整理电子表格以供工作。我提供了指向工作表布局图像的链接,减去了与该问题无关的所有内容。
电子表格布局
当前,我具有工作表设置,每次在G列的下拉列表中进行选择时都会设置一个时间戳。下拉选项包括:“进行中”,“已提交批准”和“已批准” 。
我用于时间戳的代码如下:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() == 7 ) { //checks the column
var nextCell = r.offset(0, 1);
var time = new Date();
time = Utilities.formatDate(time, "America/New_York", "MM/dd/yy HH:mm");
nextCell.setValue(time);
};
}
我现在想要做的是在I和J列中创建计数器,因此我可以看到每行选择了“已提交批准”和“已批准”的次数。
因此,最终,每次选择新状态时,我们都会获得一个新的时间戳记(目前可以正常使用)-但是,如果选择了“已提交批准”,那么您将获得时间戳记并在已提交编号中加上+1(列一世)。然后,对于列J中的“已批准”也是如此。
我不知道如何从这里继续。我要添加什么脚本才能根据选择在其他列中获得+1?
在此先感谢您的帮助!
答案 0 :(得分:0)
我认为这比您想象的要容易。因此,我认为,如果选择“已提交批准”,则I列的数字将增加1,如果选择“批准”,则J列的数字将增加1?这只是一个简单的If
语句。如果愿意,也可以使用Switch
。
我在onEdit函数的末尾添加了更多代码。看看是否适合您。
function onEdit() {
var s = SpreadsheetApp.getActiveSheet()
var r = s.getActiveCell()
if( r.getColumn() == 7 ) { //checks the column
var nextCell = r.offset(0, 1)
var time = new Date()
time = Utilities.formatDate(time, "America/New_York", "MM/dd/yy HH:mm")
nextCell.setValue(time)
//-------------------- New code added as below -------------------------------
var colNum = 0
if (r.getValue() == "Submitted to Approval") {
colNum = 2
}
else if (r.getValue() == "Approval") {
colNum = 3
}
if (colNum > 0) {
var editCell = r.offset(0,colNum) // gets the offset selected by r
var number = editCell.getValue() // gets the value, increase it by 1 and set it back
number = number + 1
editCell.setValue(number)
} // colnum > 0
} //r.getColumn() == 7
} // onEdit
当然,colNum是根据工作表的列进行硬编码的,因此不建议这样做。但这是另一回事。
还有一个问题,当G已经处于“已批准”状态,并且再次选择“已批准”时,onEdit不会触发,因为Google工作表确定您没有“更改”任何内容,因此“已批准”列将保持不变。我不知道您的工作表是否具有这种行为,因此仅指出这一点。