Google apps script 如何将两个Google脚本合并到一个中-Google电子表格插入时间取决于列修改

Google apps script 如何将两个Google脚本合并到一个中-Google电子表格插入时间取决于列修改,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我有这两个脚本,如果结构相同,唯一不同的是: 创建日期:仅当第1(A)列被修改时才插入日期 上次更新:仅当somone修改第5列或第6列时插入日期 我认为简单地添加一些其他或空,但我没有足够的专业知识来做这件事。。。如果有人能帮忙,我会非常感激的 脚本1: 函数创建日期(事件){ //脚本创建日期定时 var actSht=event.source.getActiveSheet(); var activeCell=actSht.getActiveCell();//检测activeCell var

我有这两个脚本,如果结构相同,唯一不同的是:

创建日期:仅当第1(A)列被修改时才插入日期
上次更新:仅当somone修改第5列或第6列时插入日期

我认为简单地添加一些其他或空,但我没有足够的专业知识来做这件事。。。如果有人能帮忙,我会非常感激的

脚本1:
函数创建日期(事件){
//脚本创建日期定时
var actSht=event.source.getActiveSheet();
var activeCell=actSht.getActiveCell();//检测activeCell
var column=activeCell.getColumn();//检测activeCell的列
var colNums=[1];//Coulmns,考虑对其进行编辑
if(colNums.indexOf(column)==-1)返回;//如果未考虑列,则返回
var row=activeCell.getRow();//检测ActiveRow
if(行<2)返回;//如果标题行,则返回
var date=Utilities.formatDate(新日期(),“GMT-3”,“dd/MM/yyyy HH:MM”);//函数日期+格式
var r1=event.source.getActiveRange().getRow();
//注意:在“创建日期”列中插入日期
actSht.getRange(r1,7).setValue(日期)
}
脚本2:
函数最后更新(e){
//脚本最后更新时间
var actSht=e.source.getActiveSheet();
var activeCell=actSht.getActiveCell();//检测activeCell
var column=activeCell.getColumn();//检测activeCell的列
var colNums=[5,6];//Coulmns,考虑对其进行编辑
if(colNums.indexOf(column)==-1)返回;//如果未考虑列,则返回
var row=activeCell.getRow();//检测ActiveRow
if(行<2)返回;//如果标题行,则返回
var date=Utilities.formatDate(新日期(),“GMT-3”,“dd/MM/yyyy HH:MM”);//函数日期+格式
var r2=e.source.getActiveRange().getRow();
//注:在第8列中插入日期
actSht.getRange(r2,8).setValue(日期);
}

这两个功能完全相同,但有两个例外:

  • 第一个使用一个元素初始化数组colNums,第二个使用两个元素(第九行)
  • 它们将值输出到不同的列(最后一行,7对8)
  • 一个函数可以轻松完成以下两个任务:

    function setTimeStamp(e){
        // no magic numbers!
        var createdColNums  = [1]; //Columns whose edit is considered a new row
        var updatedColNums  = [5,6]; //Columns whose edit is considered an updated row
        var createdStampColumn = 7;
        var updatedStampColumn = 8;
        var headerRow = 1;
    
        var actSht = e.source.getActiveSheet();
        var activeCell = actSht.getActiveCell(); //Detect the ActiveCell
        var row = activeCell.getRow(); //Detect the active row
        if(row == headerRow)   
            return; //If header row then return
    
        var column = activeCell.getColumn(); // Detect the active column
        var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");
        var columnToStamp;
    
        if(createdColNums.indexOf(column) > -1) 
            columnToStamp = createdStampColumn;
        else if(updatedColNums.indexOf(column) > -1) 
            columnToStamp = updatedStampColumn;
    
        actSht.getRange(row, columnToStamp).setValue(date);
    }
    
    注意:如果将函数命名为onEdit(),并将其放入电子表格的脚本中,它将自动作为触发器调用,并且不需要授权。在这种情况下,请使用

    SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
    
    代替

    e.source.getActiveSheet()
    
    尽管如此,imho更好的方法是检查创建的时间戳所在的值,如果存在值,则设置更新的时间戳。然后,您可以在编辑任何列时获得正确的输出:

    function onEdit() {
        // no magic numbers!
        var createdStampColumn = 7;
        var updatedStampColumn = 8;
        var headerRow = 1;
    
        var actSht = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
        var activeCell = actSht.getActiveCell(); //Detect the ActiveCell
        var row = activeCell.getRow(); //Detect the active row
        if(row == headerRow)   
            return; //If header row then return
    
        var column = activeCell.getColumn(); // Detect the active column
        var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");
        var columnToStamp;
    
        if (actSht.getRange(row, createdStampColumn).getValue() == "")
            columnToStamp = createdStampColumn;
        else
            columnToStamp = updatedStampColumn;
    
        actSht.getRange(row, columnToStamp).setValue(date);
    }
    

    没有真正注意到你的脚本实际上在做什么,这里是两个脚本的“直译/组合”。请注意,我反转了条件以避免从脚本“返回”,因此可以转到第二个条件

    function combined_function(event){
      var actSht = event.source.getActiveSheet();
      var activeCell = actSht.getActiveCell(); //Detec the ActiveCell
      var column = activeCell.getColumn(); // Detect the Column of the ActiveCell
      var colNums  = [1]; //Coulmns, whose edit is considered
      if(colNums.indexOf(column) > -1) { //If column is the one we want then execute
        var row = activeCell.getRow(); //Detect the ActiveRow
        if(row >= 2){
          var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
          var r1 = event.source.getActiveRange().getRow();
          //Note: Insert the Date in Create Date Column
          actSht.getRange(r1, 7).setValue(date)
        }
      };// end of first original function
      colNums  = [5,6]; //Coulmns, whose edit is considered
      if(colNums.indexOf(column) > -1){ // same comment, condition inverted
        var row = activeCell.getRow(); //Detect the ActiveRow
        if(row >= 2){
          var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
          var r2 = e.source.getActiveRange().getRow();
          //Note: Insert the Date in the Column 8
          actSht.getRange(r2, 8).setValue(date);
        }
      }
    }
    
    function onEdit() {
        // no magic numbers!
        var createdStampColumn = 7;
        var updatedStampColumn = 8;
        var headerRow = 1;
    
        var actSht = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
        var activeCell = actSht.getActiveCell(); //Detect the ActiveCell
        var row = activeCell.getRow(); //Detect the active row
        if(row == headerRow)   
            return; //If header row then return
    
        var column = activeCell.getColumn(); // Detect the active column
        var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");
        var columnToStamp;
    
        if (actSht.getRange(row, createdStampColumn).getValue() == "")
            columnToStamp = createdStampColumn;
        else
            columnToStamp = updatedStampColumn;
    
        actSht.getRange(row, columnToStamp).setValue(date);
    }
    
    function combined_function(event){
      var actSht = event.source.getActiveSheet();
      var activeCell = actSht.getActiveCell(); //Detec the ActiveCell
      var column = activeCell.getColumn(); // Detect the Column of the ActiveCell
      var colNums  = [1]; //Coulmns, whose edit is considered
      if(colNums.indexOf(column) > -1) { //If column is the one we want then execute
        var row = activeCell.getRow(); //Detect the ActiveRow
        if(row >= 2){
          var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
          var r1 = event.source.getActiveRange().getRow();
          //Note: Insert the Date in Create Date Column
          actSht.getRange(r1, 7).setValue(date)
        }
      };// end of first original function
      colNums  = [5,6]; //Coulmns, whose edit is considered
      if(colNums.indexOf(column) > -1){ // same comment, condition inverted
        var row = activeCell.getRow(); //Detect the ActiveRow
        if(row >= 2){
          var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
          var r2 = e.source.getActiveRange().getRow();
          //Note: Insert the Date in the Column 8
          actSht.getRange(r2, 8).setValue(date);
        }
      }
    }