试图结合两个onEdit脚本(Google表格)

时间:2016-08-11 21:32:24

标签: google-apps-script google-sheets triggers

想知道是否有人可以提供帮助。

我目前有两个需要合并的脚本(用于处理单个电子表格),第一个将已完成的行移动到新工作表;

function onEdit(event) {
  // assumes source data in sheet named Current
  // target sheet of move to named Completed
  // test column with yes/no is col V
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Current" && r.getColumn() == 24 && r.getValue() == "Yes") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Complete");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

,第二个基于值自动排序行;

function onEdit(event){
  var sheet = event.source.getActiveSheet();
  var editedCell = sheet.getActiveCell();

  var columnToSortBy = 14;
  var tableRange = "A3:X150"; // What to sort.

  if(editedCell.getColumn() == columnToSortBy){   
    var range = sheet.getRange(tableRange);
    range.sort( { column : columnToSortBy, ascending: true } );
  }
}

为了将两者结合起来,我想出了这个......

function onEdit(event) {
  myFunction1(event);
  myFunction2();
}

function myFunction1(event) {
  // assumes source data in sheet named Current
  // target sheet of move to named Completed
  // test column with yes/no is col V
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Current" && r.getColumn() == 24 && r.getValue() == "Yes") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Complete");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
}

function myFunction2() {
  var sheet = event.source.getActiveSheet();
  var editedCell = sheet.getActiveCell();

  var columnToSortBy = 14;
  var tableRange = "A3:X150"; // What to sort.

  if(editedCell.getColumn() == columnToSortBy){   
    var range = sheet.getRange(tableRange);
    range.sort( { column : columnToSortBy, ascending: true } );
  }
}
}

第一个脚本有效但第二个脚本没有(不会自动排序),任何人都可以告诉我哪里出错了吗?

链接到电子表格here;

2 个答案:

答案 0 :(得分:3)

目前,myFunction2()位于myFunction1()内。如果您将其移动到它自己的外部函数,onEdit()应该能够调用它。如果您有myFunction1()中需要使用的变量,然后再次在myFunction2()中调用,则您将myFunction2()保留为嵌套函数。

但是,您需要在最后一个括号前的最后一点myFunction1()调用myFunction2()。在这种情况下,您只需要onEdit()来调用myFunction1(),整个代码就会运行(看作onEdit()次来电myFunction1(); myFunction1()来电{{} 1}}

因此,您的两个选项应如下所示:

嵌套的:

myFunction2()

嵌套:

function onEdit(event) {
  myFunction1(event);
  myFunction2();
}

function myFunction1(event) {
  // start of code
  if(condition == met) {
    //operation to follow
  } //closes if statement operations
} //***this is your misplaced bracket***

function myFunction2() {
  //start of code
  if(condition == met){   
    //operation to follow
  }
}

简而言之,您已为myFunction2()设置并定义了所有内容,但尚未正式告知代码执行这些操作。

答案 1 :(得分:-2)

试试这个

function onEdit(event) {
  myFunction1(event);
  myFunction2(event); // you forgotten to pass the event here
}
.
.
.
function myFunction2(event) {  // you forgotten to pass the event here
.
.
.
}