我正在嘗試查找以記錄單元格具有特定值的時間量。例如:如果H4 = ABC,然後更改爲XYZ;我需要記錄單元格H4在ABC更改爲XYZ之前的時間。 我一直無法在Google腳本庫中找到定時器功能。 任何幫助,將不勝感激。 以下是表格和當前正在運行的所有腳本的鏈接。 https://docs.google.com/spreadsheets/d/1eqOXVR9_2fe246PejvwUZd-Z1RoDN8OyS9B7Hk8FZRI/edit?usp=sharing記錄單元格值時間
Q
記錄單元格值時間
1
A
回答
0
我不知道GAS中的定時器功能,無論如何,它不會是最佳的讓它一直運行。更好的方法似乎是計算您擁有的時間戳與當前日期之間的單元格更改時間差。
我已經調整了this function了一點,所以它也包括時間差:
function dateDiff(start, end) {
var diff = { years: 0, months: 0, days: 0 };
var timeDiff = end - start;
if (timeDiff > 0) {
diff.years = end.getFullYear() - start.getFullYear();
diff.months = end.getMonth() - start.getMonth();
diff.days = end.getDate() - start.getDate();
diff.hours = end.getHours() - start.getHours();
if (diff.months < 0) {
diff.years--;
diff.months += 12;
}
if (diff.days < 0) {
diff.months = Math.max(0, diff.months - 1);
diff.days += 30;
}
if (diff.hours < 0) {
diff.days = Math.max(0, diff.days - 1);
diff.hours += 24;
}
}
return diff;
};
和你onEdit
功能,現在使用該DateDiff函數:
function onEdit() {
// This section returns if you're not on the SBC/HP spreadsheet.
var sheet = "SBC/HP";
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
// if r is in column h { do the rest }
if(r.getColumn() != 11){
var row = r.getRow();
var previousDate = new Date(s.getRange(row, 11).getValue()); //getting the timestamp of the previous change. Looks like the year should be YYYY, not YY as it is now
var time = new Date();
var timeDiff = dateDiff(previousDate, time); //calculating time difference in a separate function
time = Utilities.formatDate(time, "GMT-05:00", "MM/dd/yy, hh:mm:ss");
s.getRange(row, 12).setValue(timeDiff.years + ' years ' + timeDiff.months + ' months ' + timeDiff.days + ' days ' + timeDiff.hours + ' hours'); //pasting time difference in some format in a separate cell
s.getRange('K' + row.toString()).setValue(time);
}
}
您可以檢查它如何作品here
0
聯合Stackdriver Logging與onEdit()。然後使用Log Filters和/或Log Exporting來提取INFO日誌的時間戳。
function onEdit (e) {
var message = e.range.getA1Notation() + " changed to " + e.value;
console.info({message:message});
}
獲取對項目的腳本編輯器Log ViewerView > Stackdriver Logging
。從這裏您可以篩選出console.info()消息,並從包含Stackdriver Logging消息中的相同e.range.getA1Notation()
的時間戳中獲取時間差。
相關問題
- 1. PHPExcel - 從單元格讀取時間值
- 2. 記錄時記錄時間
- 3. 填充記錄集值的單元格中的空格
- 4. Apache POI時間單元格
- 5. c#linq按單元格返回記錄 - 索引器和值
- 6. 在datagridview單元格值更改後更新數據庫記錄?
- 7. 在Excel中更改單元格值的記錄
- 8. 如何記錄在Excel中編輯單元格的時間花費
- 9. 空白單元格與單元格之間的差值=「」
- 10. 根據不同值標記單元格
- 11. 單擊單元格時如何獲取單元格值?
- 12. VBA從兩個時間之間的單元格中總結值
- 13. 爲什麼UICollectionView在單元格全屏時記錄錯誤?
- 14. 創建excel宏以實時記錄單元格更改內容
- 15. 使uipickerview出現在單元格之間時觸摸單元格
- 16. VBA:在單元格中添加1小時到時間的值
- 17. 記錄最後值的打印時間
- 18. PHP時間記錄
- 19. 當用戶在單元格內時,爲單元格賦值
- 20. 切換默認選中單元格的單元格之間的複選標記
- 21. 表格單元格間距
- 22. UNIX時間記錄時區?
- 23. 在網格中添加記錄的單元格值,並在extjs的記錄的最後一個單元格中獲得結果
- 24. 將一個單元格中的值插入同一記錄中的另一個單元格中
- 25. 將單元格的值複製到另一個單元格以記錄車輛里程數
- 26. POI:2000及以上記錄的單元格格式受損
- 27. 單元測試和記錄
- 28. 當另一個單元格包含特定值時,複製單元格值和單元格註釋
- 29. 記錄活動單元格地址,單擊
- 30. 從j2me的csv文件讀取單個單元格記錄
a-change,對不起,回來這麼晚,但這個工作得很好。我只對時間格式進行了一些更改,只包括日,小時和分鐘。 –