谷歌电子表格,我不能在另一张不同的表格上获得第二个存档功能

时间:2015-01-21 11:14:22

标签: google-apps-script google-sheets

这是我到目前为止所做的,它适用于第一张名为In house orders的工作表,但我似乎无法在第二张名为Outsourced orders的工作表上使用Archive功能。我错过了第二个存档功能的重要内容吗?

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

    var columnToSortBy = 10;
    var tableRange = "A:N3"; // What to sort. 

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

            onEdit2(event);    
    }
  } // End definition of onEdit


function onEdit2(passedEvent) {     
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var s = passedEvent.source.getActiveSheet();
    var r = passedEvent.source.getActiveRange();

    if(s.getName() == "In House Orders" && r.getColumn() == 10 && r.getValue()    == "5. Archive") {
        var row = r.getRow();
        var numColumns = s.getLastColumn();
        var targetSheet = ss.getSheetByName("Archive");
        var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
        s.getRange(row, 1, 1, numColumns).moveTo(target);
        s.deleteRow(row);
    }
}    

function onEdit3(passedEvent) {     
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var s = passedEvent.source.getActiveSheet();
    var r = passedEvent.source.getActiveRange();

    if(s.getName() == "Outsourced Orders" && r.getColumn() == 10 &&     r.getValue() == "5. Archive") {
        var row = r.getRow();
        var numColumns = s.getLastColumn();
        var targetSheet = ss.getSheetByName("Archive");
        var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
        s.getRange(row, 1, 1, numColumns).moveTo(target);
        s.deleteRow(row);
    }
}

1 个答案:

答案 0 :(得分:0)

首先,你不是在任何地方调用onEdit3()函数。此外,不需要onEdit2()和onEdit3()函数。您只需添加如下代码:

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

var columnToSortBy = 10;
var tableRange = "A:N3"; // What to sort. 

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

  var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = passedEvent.source.getActiveSheet();
var r = passedEvent.source.getActiveRange();     
  if(s.getName() == "In House Orders" && r.getColumn() == 10 && r.getValue()    == "5. Archive") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Archive");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
}
else if(s.getName() == "Outsourced Orders" && r.getColumn() == 10 &&     r.getValue() == "5. Archive") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Archive");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
}
  //onEdit2(event);    

  }
  } // End definition of onEdit

希望有所帮助!