不一致的结果将一行从一张纸移到另一张纸上

时间:2014-02-18 00:33:01

标签: google-apps-script google-sheets

以下代码执行不一致。有时它会做它应该做的事情 - 从一个标签/页/表移动一行到另一个。其他时候 - 它移动两行。我很困惑我可能做错了什么。有任何想法吗?我怀疑有一个谷歌bug,但我怎么能以这种或那种方式验证它?

如果这不是寻求这种帮助的合适地方而道歉,如果不是,那么请指出我的位置。

提前谢谢。

我的文件包含onEdit()触发器:

var LIBRARY_VERSION=1.61

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function happyFunTime(e) {
//Logger.log(getCachedUser("happyFunTime at the top"));
  var duplicate=0;
  var msg;
  var debug=true;

  if (debug)
    Logger.log("Inside happyFunTime()");

  duplicate=checkNewEntryForDuplicate(e);
  if (debug)
    Logger.log("duplicate=" + duplicate);

  var sourceSheet = SpreadsheetApp.getActiveSheet();
  var r = sourceSheet.getActiveCell();
  var columnNum = r.getColumn();
  var notesColumnOffset = getNotesColumnOffset();
  var rowNum = r.getRow();
  var sourceSheetName=sourceSheet.getName();

  if (rowNum <=1) {
    Logger.log("rowNum = " + rowNum + " Exiting happyFunTime() at  line 30");
    return;
  }


  if (duplicate) {

    if (debug)
      Logger.log("Duplicate.  Exiting happyFunTime() at  line 38");
    return;
  }

  notesColumnOffset++;
  // set dateCell = the current row at column J
  var notesCell = sourceSheet.getRange(rowNum, notesColumnOffset);
  var tZone="America/Los_Angeles";

  // Format the current date into datetime format
  var dateTime = Utilities.formatDate(new Date(), tZone, "MMM-dd-yyyy h:mm a");


  // set row color as soon as we know the status
  var outCome=setRowColor(rowNum);  
  if (!duplicate) {
    var targetSheetName=outCome[0];    

   if (debug) {
     msg="happyFunTime(): Library_Version: " + LIBRARY_VERSION + " Calling getUserName()"
     myLog(msg);
   }
   var user=getUserName("happyFunTime()"); 


    if (debug) {
      msg="happyFunTime(): Back from Calling getUserName().  Library_Version:"  + LIBRARY_VERSION;
      myLog(msg);
      msg=("happyFunTime(): I am " + user);
      myLog(msg);
    }
    var comment=dateTime + "\nLast Edited By: " + user;

    if (debug) {
      msg=("happyFunTime(): comment= " + comment);
      myLog(msg);
    }
    notesCell.setComment(comment);


    // check to see if sourceSheetName is the same as targetSheetName and if so, do not moveRow.
    if (targetSheetName != sourceSheetName) {
      if (debug)
        Logger.log("happyFunTime(): Getting targetSheetName=" + targetSheetName);
      var obj;
      // if sheet exists
      obj=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(targetSheetName);


      if (obj) {
        if (debug)
          Logger.log("happyFunTime(): Calling moveRow()");
       moveRow(sourceSheetName, targetSheetName, rowNum);

      }
  } // if (targetSheetName != sourceSheetName)
} // if (!duplicate)
if (debug)
    Logger.log("Leaving happyFunTime()");
}

    function moveRow(sourceSheetName, targetSheetName, sourceRowNum) {
    var debug=true;

    if (debug)
      Logger.log("Inside libFuncs.gs: moveRow()");


    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var targetSheet = ss.getSheetByName(targetSheetName);  
    var sourceSheet=ss.getSheetByName(sourceSheetName);
    var sourceRange;
    var msg;

      if (targetSheet == null) return;

      var targetRow = targetSheet.getLastRow() + 1;
      var maxRows=targetSheet.getMaxRows(); //  for debugging


      if (targetRow > targetSheet.getMaxRows())
        targetSheet.insertRowAfter(targetRow - 1);

      if (debug) {
        msg = "targetSheetName=" + targetSheetName + "  rowNum=" + sourceRowNum;
        Logger.log(msg);
       }
        sourceRange=sourceSheet.getRange(sourceRowNum, 1, 1, sourceSheet.getLastColumn());
        sourceRange.copyTo(targetSheet.getRange(targetRow, 1));
        sourceSheet.deleteRow(sourceRowNum);

    if (debug)
      Logger.log("Leaving libFuncs.gs: moveRow()");    

    }

以下是破损运行的执行记录:

[14-02-18 15:28:49:898 PST] Logger.log([happyFunTime(): Calling moveRow(), []]) [0 seconds]
[14-02-18 15:28:49:898 PST] Logger.log([Inside libFuncs.gs: moveRow(), []]) [0 seconds]
[14-02-18 15:28:49:899 PST] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[14-02-18 15:28:50:374 PST] Spreadsheet.getSheetByName([VM]) [0.475 seconds]
[14-02-18 15:28:50:616 PST] Spreadsheet.getSheetByName([Leads]) [0.242 seconds]
[14-02-18 15:28:50:640 PST] Sheet.getLastRow() [0.022 seconds]
[14-02-18 15:28:50:866 PST] Sheet.getMaxRows() [0.225 seconds]
[14-02-18 15:28:50:904 PST] Sheet.getMaxRows() [0.035 seconds]
[14-02-18 15:28:50:904 PST] Logger.log([targetSheetName=VM  rowNum=6, []]) [0 seconds]
[14-02-18 15:28:51:122 PST] Sheet.getLastColumn() [0.217 seconds]
[14-02-18 15:28:51:122 PST] Sheet.getRange([6, 1, 1, 16]) [0 seconds]
[14-02-18 15:28:51:123 PST] Sheet.getRange([93, 1]) [0 seconds]
[14-02-18 15:28:51:873 PST] Range.toString() [0 seconds]
[14-02-18 15:28:51:873 PST] Range.copyTo([Range]) [0.749 seconds]
[14-02-18 15:28:52:131 PST] Sheet.deleteRow([6]) [0.257 seconds]
[14-02-18 15:28:52:132 PST] Logger.log([Leaving libFuncs.gs: moveRow(), []]) [0 seconds]
[14-02-18 15:28:52:132 PST] Logger.log([Leaving happyFunTime(), []]) [0 seconds]
[14-02-18 15:28:52:134 PST] Execution succeeded [11.968 seconds total runtime]

0 个答案:

没有答案