时间触发器未运行

时间:2015-10-19 19:51:46

标签: google-apps-script google-sheets

我的时间触发器不让onEdit运行。如果我删除触发器和编辑Google表单onEdit将起作用。 但是,当我想让onEdit每分钟运行时,它就不会运行。 这是my sheetscript

function setTrigger() {
    ScriptApp.newTrigger("MOVE")
        .timeBased()
        .everyMinutes(1)
        .create();
}

function onOpen() {
    SpreadsheetApp.getUi().createMenu('My Menu')
        .addItem('MOVE', 'onEdit')
        .addToUi()
}




function onEdit() {
  // assumes source data in sheet named Needed
  // target sheet of move to named Acquired
  // test column with yes/no is col 4 or D
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var r = s.getActiveRange();



  if(s.getName() == "Load Board" && r.getColumn() == 3 && r.getValue() == "Closed") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Closed");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }

}

1 个答案:

答案 0 :(得分:1)

根据我对你以前答案的评论的解释,我给你这个代码(编辑我自己以前的答案)。 (虽然我不知道AppSheet,所以你可能会要求更多/不同的。)

以下代码将移动'满足条件的所有行(第3列=='已关闭')。满足条件后,移动'可以通过以下三种方式启动: 手动编辑表格; 点击表单的“我的菜单”; 或者通过触发计时器(触发器本身可以由代码设置或在脚本界面中手动设置)。

请注意,onEdit函数可以简单地调用函数move()而不是自己执行移动的代码(在这种情况下,不需要onEdit中的大多数代码)。我在代码中留下了评论这个效果。(我不知道哪种方法更好。)

function setTrigger() { // Note: 1 this could be set manually; 2) There needs to be a 'move' function which I have created; this trigger could equally be set to trigger onEdit every minute instead and, due to the function call in the onEdit funtion, would effect the same result, as can be seen by the onOpen menu item calling onEdit directly.
  ScriptApp.newTrigger("move")
  .timeBased()
  .everyMinutes(1)
  .create();
}

function move(){ 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Load Board');
  var values = s.getDataRange().getValues();
  var counter = 0; // not part of process, simply here for counting what's happening for inspection if required
  var targetSheet = ss.getSheetByName("Closed");
  for (var i = values.length -1 ; i>=0  ; i--){
    Logger.log('loop number: ' + i + ' gives value in column 3 = ' + values[i][2] );
    if(values[i][2] == "Closed"){
      targetSheet.appendRow(values[i]);
      s.deleteRow(i+1)
      counter++
    }
  }
  if(counter > 0)
    PropertiesService.getScriptProperties().setProperty('MOVE_INDICATOR', counter +' rows moved at ' + new Date()); // merely gives you a log of the last update
}

function onOpen() {
  SpreadsheetApp.getUi().createMenu('My Menu')
  .addItem('MOVE', 'onEdit')
  .addToUi()
}

function onEdit(e){
  if(e === undefined){ // note, just for testing, if you change this line to if(e){   then this remainder of this function is redundant since function move() is called and does a similar job.
    move();
    return
  }
  var ss = e.source;
  Logger.log(ss.getName() ) // You can inspect the logger to see what is happening.
  var s = e.source.getActiveSheet() // active tab in spreadsheet
  Logger.log(s.getName() )
  var r = e.range; // active range}

  if(s.getName() == "Load Board" && r.getColumn() == 3 && r.getValue() == "Closed")
  {
    var row = r.getRow();
    var numColumns = ss.getLastColumn();
    var targetSheet = ss.getSheetByName("Closed");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);}
}

function deleteProps(){ // here just to clear down the PropertiesService
  PropertiesService.getScriptProperties().deleteAllProperties();
}