我想在單元格中以任何格式寫入時間。
如果我寫 1.5,那么當我寫 1:30 時,單元格會顯示 1:30,它會顯示 1:30,但不會顯示 1.5 的那個,如下所示
如果我寫 36.25,那么單元格會顯示 36:15
如果我寫 64,那么單元格會顯示 64:00
我可以使用帶有作業表更改事件的 VBA 在 excel 中執行此操作,但需要如何在 Google 表格中完成。有誰知道這個問題的答案。如果有任何幫助,我將不勝感激。
uj5u.com熱心網友回復:
使用onEdit(e) 簡單的觸發器和簡單的數學運算,如下所示:
/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* @param {Object} e The onEdit() event object.
*/
function onEdit(e) {
if (!e) {
throw new Error(
'Please do not run the onEdit(e) function in the script editor window. '
'It runs automatically when you hand edit the spreadsheet. '
'See https://stackoverflow.com/a/63851123/13045193.'
);
}
timeEntry_(e);
}
function timeEntry_(e) {
if (!Number(e.value)) {
return;
}
e.range.setValue(e.value / 24).setNumberFormat('[h]:mm');
}
有關日期和時間值如何在電子表格中作業的說明,請參閱此答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/529348.html
標籤:谷歌应用脚本谷歌表格
