每次選擇 col8 中的一個單元格時,我都試圖在電子表格中做兩件事。1st 增加 col7 中單元格的值并在行中的第一個空白單元格中設定時間戳。我被困在時間戳上。增加值有效,我可以將時間戳設定為代碼中顯示的特定單元格或作業表的最后一列,但我找不到將其設定在行的第一個空白單元格中的解決方案。
function onSelectionChange(e) {
const as = e.source.getActiveSheet();
const col = e.range.getColumn();
const row = e.range.getRow();
if (as.getName() == 'Sheet1' && col == 8){
const range = as.getRange(row,7);
range.setValue(range.getValue() 1);
e.range.setValue('Present');
}
onEdit(e);
}
function onEdit(e) {
var attendanceSheet = e.source.getActiveSheet();
var row = e.range.getRow();
var col = e.range.getColumn();
if(attendanceSheet.getName() == 'Sheet1' && col == 8) {
if (attendanceSheet.getRange(row, 12).getValue() === '')
attendanceSheet.getRange(row, 12).setValue(new Date());
}
}
uj5u.com熱心網友回復:
恕我直言,onEdit從一個onSelectionChange函式(另一個簡單的可安裝觸發器)呼叫一個函式(一個簡單的可安裝觸發器)是一個非常糟糕的主意,因為在選擇更改發生后可能會立即進行編輯。
如果您希望避免在兩個簡單觸發器上重復時間戳代碼,則將時間戳代碼“提升”到一個函式中并從兩個觸發器中呼叫它。
function onSelectionChange(){
// Some code here
timestamping();
}
function onEdit(){
// Some code here
timestamping();
}
function timestamping() {
// timestamping code here
}
關于在一行中的第一個空白單元格中設定時間戳,使用indexOf您可能會獲得該列,您可能會使用以下內容:
var sheet = e.range.getSheet();
/** Get the edited row values */
var rowValues = sheet.getRange(e.range.rowStart,1,1,sheet.getLastColumn()).getValues()[0];
/** Get the column of first empty cell */
var column = rowValues.indexOf('') 1;
/** First evaluate if there was found and empty cell, if so, add the timestamp, otherwsie do nothing */
if(column > 0) attendanceSheet.getRange(row, column).setValue(new Date());
uj5u.com熱心網友回復:
您可以在該特定行中搜索空單元格:
const vals = ws.getRange(iRow, 1, 1, iColMax).getValues();
for(var iCol=0; iCol<iColMax; iCol ) {
if( vals[0][iCol] == "" ) {
//Logger.log(`Cell in Row ${iRow} Col ${iCol 1} is empty`);
ws.getRange(iRow,iCol 1).setValue(new Date());
break;
}
}
uj5u.com熱心網友回復:
你可以試試這個:
function onSelectionChange(e) {
const as = e.source.getActiveSheet();
const col = e.range.getColumn();
const row = e.range.getRow();
if (as.getName() == 'Sheet1' && col == 8) {
const range = as.getRange(row, 7);
range.setValue(range.getValue() 1);
e.range.setValue('Present');
const array = as.getRange(row, 1, 1, as.getLastColumn() 1).getValues()[0];
const empty_col = array.indexOf('') 1;
as.getRange(row, empty_col).setValue(new Date());
}
}
但坦率地說,我懷疑這是一個好主意。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343980.html
