如何修复我的脚本。缺少的部分是我需要我在主表上的时间戳以显示在我的帮助表上

时间:2017-02-17 16:21:08

标签: javascript google-sheets

我正在寻找帮助来修复我在下面发布的Google表格中的脚本。缺少的部分是我需要来自Heat Help Notifications(主表)的时间戳(NewDate)以显示在我的帮助表上。有什么建议或修正吗?因为脚本正在插入时间戳,所以它不是“打字”的,所以它没有显示在我的帮助表上。

function onEdit(e) {
    // sheet where the cells are protected from updates
    var masterSheetName = "Heat Help Notifications " 
    // sheet where the values are copied for later checking
    var helperSheetName = "Helper" 
    // only take into account edits on or below this row
    var firstDataRow = 1; 
    // only take into account edits on or to the right of this column
    var firstDataColumn = 1; 

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var masterSheet = ss.getActiveSheet();
    if (masterSheet.getName() != masterSheetName) return;

    var masterCell = masterSheet.getActiveCell();
    if (masterCell.getRow() < firstDataRow || 
        masterCell.getColumn() < firstDataColumn) return;

    var helperSheet = ss.getSheetByName(helperSheetName);
    var helperCell = helperSheet.getRange(masterCell.getA1Notation());
    var newValue = masterCell.getValue();
    var oldValue = helperCell.getValue();

    if (oldValue == "") {
        helperCell.setValue(newValue);
    } else {
        masterCell.setValue(oldValue);
    }

    var aCell = e.source.getActiveCell(), col = aCell.getColumn(); 
    if(col ==  1 || col ==  3 || col ==  5 || col ==  7 || col ==  9 || 
       col == 11 || col == 13 || col == 15 || col == 17 || col == 19 || 
       col == 21 || col == 23 || col == 25 || col == 27 || col == 29 || 
       col == 31 || col == 33 || col == 35 || col == 37 || col == 39 || 
       col == 41 || col == 43 || col == 45 || col == 47 || col == 49 || 
       col == 51 || col == 53 || col == 55 || col == 57 || col == 59 || 
       col == 61 || col == 63 || col == 65 || col == 67 || col == 69 || 
       col == 71 || col == 73 || col == 75 || col == 77 || col == 79 || 
       col == 81 || col == 83 || col == 85 || col == 87 || col == 89 || 
       col == 91 || col == 93 || col == 95 || col == 97 || col == 99 || 
       col == 101 || col == 103)  //checks the column
    var nextCell = aCell.offset(0, 1);
    if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date())
    var newDate = Utilities.formatDate(new Date(), "GMT-5", "dd/MM/yyyy kk:mm:ss");
    adjacentCell.setValue(newDate);
}

1 个答案:

答案 0 :(得分:0)

尝试一下,

function onEdit(e) {
  var masterSheetName = "Heat Help Notifications " // sheet where the cells are   protected from updates
  var helperSheetName = "Helper" // sheet where the values are copied for later checking
  var firstDataRow = 1; // only take into account edits on or below this row
  var firstDataColumn = 1; // only take into account edits on or to the right of this column

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var masterSheet = ss.getActiveSheet();
  if (masterSheet.getName() != masterSheetName) return;

  var masterCell = masterSheet.getActiveCell();
  if (masterCell.getRow() < firstDataRow || masterCell.getColumn() <   firstDataColumn) return;

  var helperSheet = ss.getSheetByName(helperSheetName);
  var helperCell = helperSheet.getRange(masterCell.getA1Notation());
  var newValue = masterCell.getValue();
  var oldValue = helperCell.getValue();

  if (oldValue == "") {
   helperCell.setValue(newValue);
  } else {
   masterCell.setValue(oldValue);
  }

  var aCell = e.source.getActiveCell(), col = aCell.getColumn(); 
  if(col == 1 || col == 3 || col == 5 || col == 7 || col == 9 || col == 11 || col == 13 || col == 15 || col == 17 || col == 19 || col == 21 || col == 23 || col == 25 || col == 27 || col == 29 || col == 31 || col == 33 || col == 35 || col == 37 || col == 39 || col == 41 || col == 43 || col == 45 || col == 47 || col == 49 || col == 51 || col == 53 || col == 55 || col == 57 || col == 59 || col == 61 || col == 63 || col == 65 || col == 67 || col == 69 || col == 71 || col == 73 || col == 75 || col == 77 || col == 79 || col == 81 || col == 83 || col == 85 || col == 87 || col == 89 || col == 91 || col == 93 || col == 95 || col == 97 || col == 99 || col == 101 || col == 103)  //checks the column
   var nextCell = aCell.offset(0, 1);
   if( nextCell.getValue() === '' ){ //is empty?
     nextCell.setValue(new Date())
     var newDate = Utilities.formatDate(new Date(), 
  "GMT-5", "dd/MM/yyyy kk:mm:ss");
     // Copied the code from above and changed masterCell to nextCell
     var helperCell = helperSheet.getRange(nextCell.getA1Notation());
     helperCell.setValue(new Date());
   }

}