Skip to content

Instantly share code, notes, and snippets.

@gold24park
Created January 11, 2023 06:36
Show Gist options
  • Save gold24park/1668ec37e9ef8fb48fc186b2f1fdae73 to your computer and use it in GitHub Desktop.
Save gold24park/1668ec37e9ef8fb48fc186b2f1fdae73 to your computer and use it in GitHub Desktop.
Record row updated time automatically
// 업데이트 날짜를 적을 Column
var UPDATED_COLUMN = 'B';
// 실제 데이터가 있다고 판단할 Column
// 여기가 비어있으면 업데이트 날짜를 적지 않는다.
var DATA_COLUMN = 'C';
// 시트의 헤더 영역 Row 수
var HEADER_ROW_LENGTH = 7;
function onEdit(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
var updatedCell = sheet.getRange(cell.getRowIndex(), columnLetter2Number(UPDATED_COLUMN));
var dataCell = sheet.getRange(cell.getRowIndex(), columnLetter2Number(DATA_COLUMN));
if (cell.getRowIndex() >= HEADER_ROW_LENGTH && dataCell.getValue().length > 0) {
updatedCell.setValue(new Date()).setNumberFormat("yyyy/MM/DD HH:mm")
}
}
// 1 -> A
function columnNumber2Letter(column) {
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
// A -> 1
function columnLetter2Number(letter) {
var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', i, j, result = 0;
for (i = 0, j = letter.length - 1; i < letter.length; i += 1, j -= 1) {
result += Math.pow(base.length, j) * (base.indexOf(letter[i]) + 1);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment