更新多列时为单元格添加时间戳

时间:2018-01-31 21:08:36

标签: spreadsheet

在尝试更新我的Google电子表格时,我一直在检查多个代码,但在尝试使用多个单元格时,这些代码都没有成功。在我的电子表格中,我有多个标签,当我更新2,3或4列中的行时,我希望它在第5列中输入日期。

感谢您的帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

第1步。

在Google电子表格中,点击“工具”菜单下的“脚本编辑器...”。

第2步。

删除可能存在的任何示例脚本并粘贴以下内容...

// Sets the targetColumn on the edited row to the current date if the 
// edited column in within columnBounds.
// Note: This will only handle single cell editing.

// Columns that need to be monitored for changes. Use CAPITAL letters.
var monitoredColumns = ['B', 'C', 'D'];
// Colum that will receive the date.
var targetColumn = 'E'
// To avoid adding the date in the title row, we need to consider the starting row.
var startingRow = 4

// onEdit() is a reserved function name that will be called every time the sheet will be edited.
function onEdit(e) {
  var range = e.range;

  // Row of the edited cell.
  var row = range.getRow();

  // Column of the edited cell.
  var col = String.fromCharCode(64 + range.getColumn());

  if (row < startingRow) {
    // None of the monitored rows have been edited.
    return;
  }

  if (monitoredColumns.indexOf(col) < 0) {
    // Column B, C or D (2, 3 or 4) was not modified.
    // Do not proceed any further.
    return;
  }

  // Current spreadsheet.
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  // Date cell.
  var dateCell = sheet.getRange(targetColumn + range.getRow());
  // Set it to the current date.
  dateCell.setValue(new Date());
}

第3步

调整monitoredColumnstargetColumnstartingRow

的值

第4步

开始在单元格中输入一些内容。