2017-02-17 64 views
0

我試圖在同一行中的其他單元格完成或更改時向列添加時間戳。當其他列更改時向其中一列添加時間戳

如果僅更改一列,但不更改多列,則以下腳本可以正常工作。

function onEdit() { 
    var s = SpreadsheetApp.getActiveSheet(); 
    if(s.getName() == "Allocation Requests") { //checks that we're on the correct sheet 
    var r = s.getActiveCell(); 
    if(r.getColumn() == 7) { //checks the column 
     var nextCell = r.offset(0, 7); 
      nextCell.setValue(new Date()); 
     } 
    } 
} 

我試圖得到它,所以如果列E,F,G或H被更改,列M中的時間戳被更新。由於

回答

-1

也許這將工作

function onEdit() { 
 
    var s = SpreadsheetApp.getActiveSheet(); 
 
    if(s.getName() == "Sheet1") { //checks that we're on the correct sheet 
 
    var r = s.getActiveCell(); 
 
    if(r.getColumn() == 5) { //checks the column 
 
     var nextCell = r.offset(0, 8); 
 
      nextCell.setValue(new Date()); 
 
     } 
 
     if(r.getColumn() == 6) { //checks the column 
 
     var nextCell = r.offset(0, 7); 
 
      nextCell.setValue(new Date()); 
 
     } 
 
     if(r.getColumn() == 7) { //checks the column 
 
     var nextCell = r.offset(0, 6); 
 
      nextCell.setValue(new Date()); 
 
     } 
 
     if(r.getColumn() == 8) { //checks the column 
 
     var nextCell = r.offset(0, 5); 
 
      nextCell.setValue(new Date()); 
 
     } 
 
    } 
 
}

+0

不,已經嘗試過了,它根本沒有添加時間戳。 –

+0

哦改變你的圖紙名稱 - 我把它作爲圖紙1 – OblongMedulla

+0

儘管稍微改變它(以及圖紙名稱)確實有效。謝謝。 –

1

像這樣的事情?

function onEdit() { 
     var s = SpreadsheetApp.getActiveSheet(); 
     if(s.getName() == "Allocation Requests") { //checks that we're on the correct sheet 
     var r = s.getActiveCell(); 
     if(r.getColumn() >= 5 && r.getColumn() <= 8) { //checks the column 
      s.getRange(r.getRow(),13).setValue(new Date()); 
      } 
     } 
    } 
+0

這也適用。謝謝。 –

+1

儘管我現在發現即使在時間戳列中添加/刪除時間戳,也會添加更新的時間戳。 –

+0

的確如此,我改變了,並從或改變。這是一個小改進,因爲在代碼中添加或刪除列不需要爲每列複製粘貼更多代碼 –

相關問題