在Google电子表格

时间:2017-08-18 03:35:02

标签: triggers google-sheets range delete-row

我编写了这个脚本,用作工作表中的onEdit触发器。我们的想法是从工作表中选择一个值,根据某些逻辑将其复制到另一个工作表中,然后删除包含原始值的源行。

运行时,通常会发生复制,但在删除时,复制的值将消失。我注意到解决问题的一种方法是,如果我删除触发器,保存并再次创建它......

我该如何避免这种行为?

function onEdit(e) {
  var range = e.range;
  var entry = range.getSheet();
  var sss = entry.getParent();

  if (sss.getName() != "Weight Tracker")
     return;

  if (entry.getName() != "Entry")
     return;

  Logger.log("CopyData is running...."+range.getCell(1,2).getValue());

  var weight = range.getCell(1,2).getValue();
  Logger.log("weight = "+weight);

  var details = sss.getSheetByName('Details'); 
  var trange = details.getRange(3, 1, 200);
  var data = trange.getValues();

  var today = new Date().setHours(0,0,0,0);

  for(var n=0;n<data.length;n++) {
    var date = new Date(data[n]).setHours(0,0,0,0);
    Logger.log("date = "+date+" =? "+today);

    if(date == today) {
        break
    };
  }

  Logger.log("n = "+n+" today: "+today);

  // n is 0 based, sheet is 1 based + 2 headers = 3, 5 is Jim's weight
  details.getRange(n+3,5).setValue(weight);
  // get rid of the row so next addition arrives to the top row
  Logger.log("deleting row...");
  // for some reason deleting the road removes the value entered...
  range.getSheet().deleteRow(1);
}

0 个答案:

没有答案