Google Spreadsheets附加一个时间戳为

时间:2015-12-01 21:41:00

标签: google-apps-script google-sheets

我发现了这段代码here

我修改它以更好地满足我的需求。我正在使用它作为调度日志。在交货选项卡中,当我将最后一列更改为已交付时,将其复制到已交付的选项卡减去最后一列。并删除旧行。它完成了工作,但我想编辑它,以便它为包含日期戳的复制数据添加一个新列。这样,当我查看交付的标签时,它将显示它的交付日期。

我已经尝试过但我的JavaScript知识还不够好。我在这里和其他网站搜索了几个小时,这些东西可能让我朝着正确的方向前进但无济于事。

    /**
 * Moves row of data to another spreadsheet based on criteria in column 6 to sheet with same name as the value in column 4.
*/

function onEdit(e) {
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
  var ss = e.source;
  var s = e.range.getSheet();
  var r = e.range;

  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = s.getLastColumn();
  var nameCol = s.getLastColumn();

  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();

  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;

  // if our action/status col is changed to ok do stuff
  if (colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
    var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (ss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = ss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);

      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      sourceRange.copyTo(targetRange);
      // ..but we can still delete the row after
      s.deleteRow(rowIndex);
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您应该使用https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate-timezone-format

var timezone = ss.getSpreadsheetTimeZone();
var timestamp = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd HH:mm:ss");
var timecell = targetSheet.getRange(targetSheet.getLastRow(), targetSheet.getLastColumn(), 1, 1);
timecell.setValue(timestamp);

代码应该在你的copyTo()之后,并且需要时间戳的标题才能工作