TimeStamp更新到相邻列

时间:2016-01-27 17:20:34

标签: google-apps-script

我想知道该做什么是可能的以及如何去做。这适用于Google表格。这是我使用的脚本(找到这个脚本并编辑它供我使用):

function onEdit(e) {
var s = e.source.getActiveSheet().getName();
var cols = [5];
if (s !== 'Michele' && s !== 'Janet' && s !== 'Stephanie' || cols.indexOf(e.range.columnStart) ==-1 || !e.value) return;
  e.range.offset(0,1).setValue(Utilities.formatDate(new Date(), "CST", "MM/dd/yyyy HH:mm:ss"));
}

第5列(E)可以选择不同的状态。到目前为止,我已将第5列的TimeStamp更改为其相邻列6(F)。我想弄清楚的是,状态是否再次改变(待定 - >确认 - >已安装 - >等),而不是更新已经是TimeStamped的第6列,它将改为继续到第7列,然后是8,然后是9,等等。

如果需要更多细节,请告诉我。

谢谢你们!

1 个答案:

答案 0 :(得分:0)

我已经测试了这段代码,但它确实有用。它找到E列右侧已经包含值的单元格数,然后计算下一个空列的内容,并将新日期写入下一个空列。

function onEdit(e) {
  var sh = e.source.getActiveSheet();
  var s = sh.getName();
  var lastCol = sh.getLastColumn(); 
  var cols = [5];
  var startCol = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('ColumnE').getColumn();
  //In the spreadsheet, name a range that is in the column you want to start at
  if (s !== 'Michele' && s !== 'Janet' && s !== 'Stephanie' || cols.indexOf(e.range.columnStart) ==-1 || !e.value) return;

  var rowEdited = e.range.getRow(); //Get the row that was edited
  //Start in row edited and column 6, get 1 row and X columns
  var datesInCells = sh.getRange(rowEdited,startCol,1,lastCol - 5).getValues();

  Logger.log('datesInCells: ' + datesInCells);
  Logger.log('datesInCells.length: ' + datesInCells[0].length);

  var numberOfDates = 0;

  for (var i=0;i<datesInCells[0].length;i+=1) {
    if (datesInCells[0][i] !== "") {
      numberOfDates +=1;
    };
  };

  Logger.log('numberOfDates: ' + numberOfDates);

  var colToWriteTo = startCol + numberOfDates;
  var newDate = Utilities.formatDate(new Date(), "CST", "MM/dd/yyyy HH:mm:ss");
  //var theStatus = sh.getRange(rowEdited, 5).getValue();
  var theStatus = e.value;
  var bothStatusAndDate = newDate + " : " + theStatus;

  sh.getRange(rowEdited,colToWriteTo).setValue(bothStatusAndDate);
};

第二版:

function onEdit(e) {
  var sh = e.source.getActiveSheet();
  var s = sh.getName();
  var lastCol = sh.getLastColumn(); 
  var cols = [5];

  if (s !== 'Michele' && s !== 'Janet' && s !== 'Stephanie' || cols.indexOf(e.range.columnStart) ==-1 || !e.value) return;

  var rowEdited = e.range.getRow(); //Get the row that was edited
  //Start in row edited and column 6, get 1 row and X columns
  var datesInCells = sh.getRange(rowEdited,6,1,lastCol - 5).getValues();

  Logger.log('datesInCells: ' + datesInCells);
  Logger.log('datesInCells.length: ' + datesInCells[0].length);

  var numberOfDates = 0;

  for (var i=0;i<datesInCells[0].length;i+=1) {
    if (datesInCells[0][i] !== "") {
      numberOfDates +=1;
    };
  };

  Logger.log('numberOfDates: ' + numberOfDates);
  var newDate = Utilities.formatDate(new Date(), "CST", "MM/dd/yyyy HH:mm:ss");
  if (numberOfDates > 4) {
    var existingValues = sh.getRange(rowEdited,7,1,4).getValues();
    sh.getRange(rowEdited,6,1,4).setValues(existingValues);
    sh.getRange(rowEdited,10).setValue(newDate);
    return;
  };

  var colToWriteTo = 6 + numberOfDates;

  sh.getRange(rowEdited,colToWriteTo).setValue(newDate);
};