1

我有一个电子表格,其中包含附有日期和优先级的行动项目。如果项目是优先级 2 并且一周或更长时间,我想创建一个归档行的函数。

这是我的剧本,但它正在发挥作用——有时它移动了错误的优先级,有时它移动了错误的日期,而且它从不移动它应该移动的所有日期。最糟糕的是,它在我的“Action Items”表底部添加了大量空行,并在“Archive”表中创建了空行。

显然有些地方出了问题,但我真的没有看到,即使经过一天的紧张调试。任何帮助都会很有启发性和感激!

function Archiver() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Action Items'); // get the sheet
  var targetsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Archive'); // get the target sheet
  var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1); // get all the rows with dates
  var fValues = columnF.getValues(); // get the values of dates
  var columnE = sheet.getRange(2, 5, sheet.getLastRow()-1, 1); // get all the rows with priorities
  var eValues = columnE.getValues(); // get the values of priorities
  var day = 24*3600*1000 // calculate ms in a day
  var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
  for (var i = 0; i < fValues.length; i++) { // repeat loop
    var priority = eValues[i][0]; // set priority in loop
    var dataday = parseInt(fValues[i][0].getTime()/day); // convert date column into miliseconds
    Logger.log(dataday+" <= " + today-7 + " - " + priority) // my log isn't picking up day
    if (dataday <= today-7 && priority == "P2") { // if an item is more than 7 days old and Priority 2...
      targetsheet.insertRows(2,1)
      // move the entire source row to the second row of target sheet
    var rangeToMove = sheet.getRange(/*startRow*/ i + 2, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ sheet.getMaxColumns());
    rangeToMove.moveTo(targetsheet.getRange("A2"));
          // add date and time of when approved to target row in column E
    targetsheet.getRange("M2").setValue(Date());
    // delete row from source sheet
    sheet.deleteRows(i + 2,1);
    }
  }
        ss.toast("Move along.", "Archiving Complete.");
}
4

1 回答 1

2

我没有调试您的代码,但我最初的想法是在迭代时删除行会扰乱数据的顺序并造成混乱,因此我对其进行了调整以复制行(而不是移动)并将要删除的行号存储在一个数组,以便我们可以在循环外删除它们。我做了一个快速测试,这似乎有效。

function Archiver() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Action Items'); // get the sheet
  var targetsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Archive'); // get the target sheet
  var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1); // get all the rows with dates
  var fValues = columnF.getValues(); // get the values of dates
  var columnE = sheet.getRange(2, 5, sheet.getLastRow()-1, 1); // get all the rows with priorities
  var eValues = columnE.getValues(); // get the values of priorities
  var day = 24*3600*1000 // calculate ms in a day
  var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
  var rowsToDelete = [];
  for (var i = 0; i < fValues.length; i++) { // repeat loop
    var priority = eValues[i][0]; // set priority in loop
    var dataday = parseInt(fValues[i][0].getTime()/day); // convert date column into miliseconds
    Logger.log(dataday+" <= " + (today-7) + " - " + priority) // my log isn't picking up day
    if (dataday <= today-7 && priority == "P2") { // if an item is more than 7 days old and Priority 2...
      targetsheet.insertRows(2,1)
      // move the entire source row to the second row of target sheet
    var rangeToMove = sheet.getRange(/*startRow*/ i + 2, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ sheet.getMaxColumns());
    rangeToMove.copyTo(targetsheet.getRange("A2"));
          // add date and time of when approved to target row in column E
    targetsheet.getRange("M2").setValue(Date());
    // delete row from source sheet
    rowsToDelete.push(i+2);
    }
  }
  rowsToDelete.reverse();
  for (var j = 0; j < rowsToDelete.length; j++) { sheet.deleteRow(rowsToDelete[j]); }
  ss.toast("Move along.", "Archiving Complete.");
} 
于 2013-01-17T07:23:55.443 回答