动态编辑Google表格中的列

德斯蒙德

语境

我正在创建一个股票价格数据数据库。我目前正在使用以下onEdit功能:

function onEdit(e) {
  // Get the spreadsheet that this will apply to, the tabs and columns
  var ss = e.source.getActiveSheet();
  var excludeTabs = ["Summary", "FTSE 250"];
  var excludeColumns = [1,2,12,13,14,15,16]; // the columns to isolate for special reasons
  var excludeCells = ["M1","I1","I2"];
  // What is the criteria for action to be taken? If the terms defined in excludeTabs are met, then move onto the next criteria (indexOf used because we stored it as an array
  if(excludeTabs.indexOf(ss.getName())===-1){
    // The range from the spreadsheet. 
    // Scripts expects an event in a cell
    var cell = e.range;
    // For the entire column of this cell
    var col = cell.getColumn();
    // Within the universe of tabs that this script applies to, we want to exclude the columns in the array
    if(excludeColumns.indexOf(col)===-1)
      // Within the universe of tabs and columns, we want to exclude certain cells
      if(excludeCells.indexOf(cell)===-1)
      // Need to make sure it only applies to formulas
        if(cell.getFormula() !== ""){ 
          var destination = ss.getRange(4, col, ss.getLastRow()-1, 1); 
          cell.copyTo(destination);  
      }//End of the remaining universe of data taking out the exceptions
     }//End Tab criteria
}//End function

这允许在编辑单元格时对某些列进行编辑。到目前为止,它仍然有效,但有一些缺陷。

问题1

有时,当我编辑列第四行上方的单元格时,即使我告诉它从第四行开始,它也会编辑整个列。这是几分钟前发生的,我在一个单元格中告诉它要排除“ I2”以上。我为此编写的代码有什么问题吗?

问题2我尝试为代码创建其他异常,其中对于某些指定范围,它将仅从不同的单元格范围进行编辑。不是每列的第四个单元格,而是第10个单元格。我尝试在下面添加它,var destination = ss.getRange(4, col, ss.getLastRow()-1, 1)但是没有用。我还尝试为不同的单元格位置创建一个单独的onEdit函数,但是它也不起作用。

到目前为止,我一直在使用如下表形式: IFERROR(IFS(C4="Returns","",AND(C4="",C5="",C6="",C7="",C8=""),"",AND(ISNUMBER(C4),ISNUMBER(C5),ISNUMBER(C6),ISNUMBER(C7),ISNUMBER(C8)),COVAR($C4:$C8,'FTSE 250'!J5:J9)),""))

但是,这只是一团糟。有时单元格中有数据,因此它将使上述公式无效。下图是一个示例。

更新我希望onEdit从列的第10行开始并向下拖动,但仅对于该列(这是我将要编辑的该列中的行)。我还希望能够对其他列执行此操作(从不同的行开始自动复制过程)。

  • 这是我要编辑的范围

[我要编辑的范围(从一周的第5天开始)1

更新2

...

 if(!excludeTabs.includes(ss.getName()) &&  
     !excludeColumns.includes(col) &&
     !excludeCells.includes(cell.getA1Notation()) &&
     cell.getFormula() !== ""
    ){
      
    if(col==33){
      var destination = ss.getRange(8, col, ss.getMaxRows()-7, 1);
      cell.copyTo(destination);    
    }

   else if(col===30){
          var destination = ss.getRange(8, col, ss.getMaxRows()-7, 1);
          cell.copyTo(destination); 
        }

    else{
    var destination = ss.getRange(4, col, ss.getMaxRows()-3, 1);
    cell.copyTo(destination);    
      }


    }
马里奥斯

说明:

问题:

  • 您的if语句中没有带方括号的代码。

  • 这里if(excludeCells.indexOf(cell)===-1)有个问题:

    var excludeCells = ["M1","I1","I2"];是一个字符串数组,var cell = e.range;是一个范围对象。您实际上是在比较两种不同的事物(astringrange对象)。

    相反,你要替换if(excludeCells.indexOf(cell)===-1) if(excludeCells.indexOf(cell.getA1Notation())===-1)

改进之处:

  • 不要使用if最后导致一个代码块的if多个语句,而应使用一个具有多个条件的语句。

  • 同样,这个范围getRange(4, col, ss.getLastRow()-1, 1);也没有太大意义。使用它更有意义,ss.getLastRow()-3因为您从开始3

  • excludeCells.indexOf(cell.getA1Notation())===-1您可以像这样使用include()来代替长表达式!excludeCells.includes(cell.getA1Notation())

解:

function onEdit(e) {
  var ss = e.source.getActiveSheet();
  var excludeTabs = ["Summary", "FTSE 250"];
  var excludeColumns = [1,2,12,13,14,15,16]; // the columns to isolate for special reasons
  var excludeCells = ["M1","I1","I2"];
  var cell = e.range;
  var col = cell.getColumn();
  
  if(!excludeTabs.includes(ss.getName()) &&  
     !excludeColumns.includes(col) &&
     !excludeCells.includes(cell.getA1Notation()) &&
     cell.getFormula() !== ""
    ){         
      if(col==33){
       var destination = ss.getRange(10, col, ss.getMaxRows()-9, 1);
       cell.copyTo(destination);    
      }
      else{
       var destination = ss.getRange(4, col, ss.getMaxRows()-3, 1);
       cell.copyTo(destination);    
      }
     }
}

请注意:

  • getLastRow()返回包含内容的最后一行。例如,如果您有10列,而第一列的10最后一行的内容为,55但工作20表末尾的有一个随机值,900假设行将900是工作表的最后一行。请注意这一点,否则您将需要其他方法来获取内容的最后一行。公式也很满足。因此,一直到工作表底部的公式都可以确定getLastRow返回的结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章