“上次修改行”脚本生成错误的月份

时间:2013-06-05 20:35:29

标签: google-apps-script

这段代码一直很适合我,但是今天(当我修改了一个单元格时)我注意到脚本生成的日期比实际日期晚一个月(即今天是2013年6月5日,但是该脚本于2013年5月5日生成)。任何人都可以看到可能导致此问题的原因吗?


// * based on the script "insert last modified date" by blisterpeanuts@gmail.com *
// Update cell with the the last modified time of any (single) cell in that row, excluding row 1 and column 1 

function onEdit() {
  var d = new Date();

  // format date nicely
  var month_str = d.getMonth();
  var day_str = d.getUTCDate();
  var year_str = d.getYear().toString().substring(2);


  // create the formatted time and date strings

  var date_str = month_str + '/' + day_str + '/' + year_str;

  // create the message (change this to whatever wording you prefer)
  // note also that rather than all the above, you could just use d.toString() 
  // I didn't because I didn't want it printing the timezone.
  var s = date_str;  

  var active_range = SpreadsheetApp.getActiveRange();

  if (active_range.getHeight() == 1 && active_range.getWidth() == 1 && active_range.getRow != 1 && active_range.getColumn() != 1) {
    var update_row = active_range.getRow().toString();
    var update_cell = "AF" + update_row;
    SpreadsheetApp.getActiveSheet().getRange(update_cell).setValue(s);
  }

}

1 个答案:

答案 0 :(得分:2)

我不认为这对你有效。 Date.getMonth()的documentation表示:

  

getMonth返回的值是0到11之间的整数   对应于1月1日至2月,依此类推。

您需要将月份增加一个。

var month_str = d.getMonth() + 1;

(变量名称month_str也是误导性的,它不是字符串,getMonth()返回一个整数)

相关问题